Recently, I was reading advanced programming in the Unix environment. I started with an electronic version. I thought reading this on my computer was really not very comfortable, and I bought one for saving my living expenses, I spent $67.6, which is a little expensive. My girlfriend on page 758 said it was too thick! I hope it can play its role.
It seems that for UNIX files, we found that UNIX file permissions are really good, but they are also very annoying. After reading them, we almost forgot about them soon. alas, it seems I can't understand it. I have to prepare a new book and read another one!
Although not reading well, it does not prevent me from writing.ProgramIn this chapter, there is a program that seems to be about file traversal, and I think it is quite interesting, so I also want to write it in imitation. I think there should be many bad habits. When we accept a project or a problem, we always like to go online and find out if there are similar or similar items. Due to abnormal network development, many problems or projects can be directly searched, and even more, code is available. I don't know whether this is good or not. I am also similar. Many problems are found on the Internet, includingCode!
However, I always do not agree with this practice! Especially when you see his source code, it is easy to leave no room for thinking, and then your final code is similar to him. The same idea, the same code. However, if you don't go online, it's not very suitable. After all, a person cannot understand everything and can provide a platform for people to brainstorm, so how can we face this situation? I 'd like someone to tell me.
Including my own program, I basically read the book and then wrote it, but I worked very hard to think about it during the writing process, from the beginning to the end, I tried very hard to think about the logic of the program, but I have always seen the program. Many places are very similar ......
Below is the code. Haha, let's get a word! I really don't know the correctness of the code. It seems like it is quite correct, especially when the root directory is input, it takes a lot of time to run .... haha, you can try it out. You can talk about any bugs...
/* This program is important to use some I/O operations in UNIX * such as stat, s_isdir opendir readdir and other functions * to complete statistics on a series of files * nreg indicates a common file * NDIR indicates a directory file * nchar indicates a character file * nblk indicates a block file * nslink indicates a symbolic connection * nsock indicates socket * nfifo indicates pipeline **/# include "headfile. H "# include" test. H "# define unsigned int uint; # define Max 256 uint nreg = 0; uint NDIR = 0; uint nchar = 0; uint nblk = 0; uint nslink = 0; uint nsock = 0; uint nfifo = 0; int dopath (char * pathname); I NT classify (char * pathname, struct stat * Buf, int flag); int main () {char dir [Max]; printf ("Please input your pathname! \ N "); scanf (" % s ", DIR); If (dopath (DIR) =-1) {printf (" error! \ N ");} printf (" nreg is % u \ n ", nreg); printf (" Ndir is % u \ n ", NDIR ); printf ("nchar is % u \ n", nchar); printf ("nblk is % u \ n", nblk); printf ("nslink is % u \ n ", nslink); printf ("nsock is % u \ n", nsock); printf ("nfifo is % u \ n", nfifo); Return 0 ;} /* dopath is a recursive function * 1. determine whether the pathname can be stat * 2. determine whether it is a file rather than a path * 3. determine that dopath * 4 is recursively called if it is a forward. here is a PTR pointer which always points to the position '\ 0' of pathname * 5. the last one is the assignment of PTR because it is necessary to restore the site, otherwise the previous operation will be carried in pathn */INT dopath (char * pathname) {int ret = 0; struct stat Buf; dir * dir = NULL; struct dirent * dent = NULL; char * PTR = NULL; char flag = 0; return_val_if_fail (pathname); If (lstat (pathname, & BUF) =-1) {fprintf (stderr, "lstat is fail, this is name is % s, and this error is % s \ n ", pathname, strerror (errno); Return classify (pathname, & Buf, s_file_ns );} if (s_isdir (BUF. st_mode) = 0) {return classify (Pat Hname, & Buf, s_file);} classify (pathname, & Buf, s_dir); PTR = pathname + strlen (pathname ); if (* (PTR-1 )! = '/') {* PTR ++ = '/'; flag = 1;} * PTR = 0; // printf ("% s \ n", pathname ); if (DIR = opendir (pathname) = NULL) {fprintf (stderr, "opendir is fail, this is name is % s, and this error is % s \ n ", pathname, strerror (errno); Return classify (pathname, & Buf, s_file_nr );} while (dent = readdir (DIR ))! = NULL) {If (strcmp (dent-> d_name ,". ") = 0 | strcmp (dent-> d_name ,".. ") = 0) {continue;} else {strcpy (PTR, dent-> d_name); If (ret = dopath (pathname ))! = 0) {return-1 ;}}if (FLAG) PTR [-1] = 0; elseptr [0] = 0; closedir (DIR); return ret ;} int classify (char * pathname, struct stat * Buf, int flag) {return_val_if_fail (pathname) Switch (FLAG) {Case s_file: {Switch (s_ifmt & Buf-> st_mode) {Case s_ifreg: nreg ++; return 0; Case s_ifchr: nchar ++; return 0; Case s_ifblk: nblk ++; return 0; Case s_iflnk: nslink ++; return 0; Case s_ififo: nfifo ++; return 0; Case S_ifsock: nsock ++; return 0; default: printf ("you create a new file type! \ N "); Return-1 ;}} case s_dir: NDIR ++; return 0; Case s_file_nr: printf (" s_file_nr \ n "); Return-1; Case s_file_ns: printf ("s_file_ns \ n"); Return-1; default: printf ("this file is not know! \ N "); Return-1 ;}}
Headfile. h
# Ifndef head_h # define head_h # include <stdio. h> # include <stdlib. h> # include <fcntl. h> # include <sys/types. h> # include <sys/STAT. h> # include <dirent. h> # include <string. h> # include <errno. h> # define s_file 1 # define s_dir 2 # define s_file_nr 3 # define s_file_ns 4 # endif
Test. h
# Ifndef test_h # define return_if_fail (thiz) if (! (Thiz) \ printf ("% s, % d warning:" # thiz "failed. \ n ",\__ func __,__ line _); return; # define return_val_if_fail (thiz) if (! (Thiz) \ {printf ("% s, % d warning:" # thiz "failed. \ n ", \ _ FUNC __,__ line _); Return-1 ;}# endif