Use the C language to calculate the ratio of various files in a folder.

Source: Internet
Author: User

Use the C language to calculate the ratio of various files in a folder.

The program list 4-7 in "Advanced Programming in UNIX environment" describes how to recursively count files under a directory! After reading its code at the beginning, I thought it was too boring to write it on the book! For this reason, I also wrote a blog post. This is the blog address: in linux, use the C language to recursively view all the files in a directory [CSDN ]!

Today, I am doing an after-school question in "Advanced Programming for Unix environments". I can see question 4.11. Here I provide a new idea for implementing this program, that is, every time I read a directory, go to this directory through the chdir function, and then read the files in this directory through the opendir function and readdir function. Then, analyze the files one by one. If the directory is used, perform recursive calls. If it is not a directory, count the file and return immediately! In this way, after all the files in the directory are analyzed one by one, a chdir ("..") is run and returned to the directory at the upper level. The specific implementation code is as follows:

  

1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 # include <errno. h> 5 # include <linux/limits. h> 6 # include <unistd. h> 7 # include <sys/types. h> 8 # include <sys/stat. h> 9 # include <dirent. h> 10 11 // declaration of all functions 12 typedef int MyFunc (const char *, const struct stat *, int); 13 static MyFunc myfunc; // define the function 14 static int myftw (const char *, MyFunc *) for processing files; 15 static int dopath (MyFunc *); 16 17 // define global variables 18 static char * fullpath; // variable 19 static long sock_c, lnk_c, reg_c, blk_c, dir_c, chr_c, 1_o_c, total_c; // count the number of various file types 20 21 // The macro 22 # define FTW_F 1 to be defined in the myfunc function // the file type is file 23 # define FTW_D 2 // the file type is directory 24 # define FTW_NS 3 // a file cannot be stat 25 # define FTW_ND 4 // a directory cannot be read 26 int main (int argc, char * argv []) 27 {28 if (argc! = 2) 29 {30 printf ("Usage: % s pathname \ n", argv [0] + 2); 31 exit (EXIT_FAILURE ); 32} 33 myftw (argv [1], myfunc); 34 total_c = sock_c + lnk_c + reg_c + blk_c + dir_c + chr_c + 1_o_c; 35 if (0 = total_c) 36 {37 total_c = 1; 38} 39 printf ("socket files = % 7ld, % 5.2f % \ n", sock_c, sock_c * 100.0/total_c ); 40 printf ("link files = % 7ld, % 5.2f % \ n", lnk_c, lnk_c * 100.0/total_c); 41 printf ("regular files = % 7ld, % 5.2f % \ n ", reg _ C, reg_c * 100.0/total_c); 42 printf ("block files = % 7ld, % 5.2f % \ n", blk_c, blk_c * 100.0/total_c ); 43 printf ("directory files = % 7ld, % 5.2f % \ n", dir_c, dir_c * 100.0/total_c); 44 printf ("character files = % 7ld, % 5.2f % \ n ", chr_c, chr_c * 100.0/total_c); 45 printf (" FIFO files = % 7ld, % 5.2f % \ n ", fifo_c, export o_c * 100.0/total_c); 46 printf ("total files = % 7ld, % 5.2f % \ n", total_c, total_c * 100.0/total_c); 47 48 return 0; 49} 50 static int myftw (const char * pathname, MyFunc * pmyfunc) 51 {52 int ret; 53 54 fullpath = (char *) malloc (sizeof (char) * PATH_MAX); 55 strcpy (fullpath, pathname); 56 ret = dopath (myfunc); 57 free (fullpath); 58 59 return ret; 60} 61 static int dopath (MyFunc * pmyfunc) 62 {63 int ret; 64 struct stat statbuf; 65 char * ptr; 66 DIR * dp; 67 struct dirent * dirp; 68 69 if (-1 = lstat (fullpath, & sta Tbuf) 70 {71 ret = pmyfunc (fullpath, & statbuf, FTW_NS); 72 return ret; 73} 74 if (S_ISDIR (statbuf. st_mode )! = 1) 75 {76 ret = pmyfunc (fullpath, & statbuf, FTW_F); 77 return ret; 78} 79 80 // make the directory file ++ 81 if (0! = (Ret = pmyfunc (fullpath, & statbuf, FTW_D) 82 return ret; 83 84 // if it is a directory file, enter this directory 85 if (-1 = chdir (fullpath )) 86 {87 printf ("% s [chdir] % s \ n", fullpath, strerror (errno); 88 ret =-1; 89 return ret; 90} 91 92 // open the current directory 93 if (NULL = (dp = opendir (". ") 94 {95 ret = pmyfunc (fullpath, & statbuf, FTW_ND); 96 return ret; 97} 98 while (NULL! = (Dirp = readdir (dp) 99 {100 // ignore. and .. file (dot) 101 if (0 = strcmp (dirp-> d_name ,". ") | 0 = strcmp (dirp-> d_name ,".. ") 102 continue; 103 memset (fullpath, 0, PATH_MAX); 104 strcpy (fullpath, dirp-> d_name); 105 106 if (0! = (Ret = dopath (myfunc) // recursive 107 break; 108} 109 chdir (".. "); // set the current directory to the upper-level directory 110 // judge the closing file 111 if (-1 = closedir (dp )) 112 {113 printf ("% s \ nError: % s", fullpath, strerror (errno); 114} 115 116 return ret; 117} 118 static int myfunc (const char * pathname, const struct stat * statptr, int type) 119 {120 switch (type) 121 {122 case FTW_F: 123 switch (statptr-> st_mode & S_IFMT) 124 {125 case S_IFSOCK: sock_c ++; Break; 126 case S_IFLNK: lnk_c ++; break; 127 case S_IFREG: reg_c ++; break; 128 case S_IFBLK: blk_c ++; break; 129 case S_IFCHR: chr_c ++; break; 130 case S_IFIFO: Export o_c ++; break; 131 case S_IFDIR: 132 printf ("Error: the directory file % s should not appear here! \ N \ nError: % s \ n ", pathname, strerror (errno); 133 break; 134} 135 break; 136 case FTW_D: 137 dir_c ++; break; 138 case FTW_ND: 139 printf ("the directory % s \ nError: % s \ n", pathname, strerror (errno) cannot be opened); 140 break; 141 case FTW_NS: 142 printf ("file % s \ nError: % s \ n", pathname, strerror (errno); 143 break; 144} 145 return 0; 146}

I did not write this code myself, But I modified it based on the code given in W. Richard Steven s! Thank you for providing such excellent books to us! This is the running result of this program,

The first line is specially set by me. The root is the name of a folder and belongs to the root user. Therefore, I cannot read it here and an error will be reported! The following is a comparison between the improved program and the program in the original book. It is found that the efficiency has actually improved a lot!

The program of descend_hierarchy_p is the program given in the book. Every read is the name of the absolute read path! The descend_hierarchy_ch command is used to access the folder every time a directory is hit, and then read it again. In this way, the name of the relative path is read each time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.