Talk C chestnuts together (77th back: C language instance -- DIY ls command continued)
Hello, everyone. We talked about the DIY cat command in the last time. The example here is:DIY ls command continued. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
We had a DIY ls command in the previous chapter, and it was not long. I believe you are still impressed. Today we are adding to the previous chapter, mainly to extend the ls command so that it can support the l parameter on the basis of the original.
We have added support for the l parameter,The procedure is as follows:For more information, see:
1. use the stat function to obtain the attributes of a file from the file. 2. determine the file type based on the st_mode value in the attribute, such as directories, links, and pipelines. 3. extract the File Permission (rwx) from the st_mode value of the attribute. The extracted method is bitwise operation and operation. 4. use the getpwuid function to obtain the user name (user name) of the object based on the st_uid value of the attribute. 5. use the getgrgid function to obtain the group name of the object based on the st_gid value of the attribute. 6. the ctime function is used to calculate the file modification date and time based on the st_mtime value of the attribute. 7. adjust the display format based on the ls-l format and output the values from step 2 to 6 to the screen.
The following is our DIY Code. For more information, see:
void show_attr(char *name){ struct stat buf; struct passwd *pwd; struct group *grp; char type ; char permission[9]; int i = 0 ; memset(permission,'-',9*sizeof(char)); if(!stat(name,&buf)) { // get the type of file if(S_ISLNK(buf.st_mode)) type = 'l'; else if(S_ISREG(buf.st_mode)) type = '-'; else if(S_ISDIR(buf.st_mode)) type = 'd'; else if(S_ISCHR(buf.st_mode)) type = 'c'; else if(S_ISBLK(buf.st_mode)) type = 'b'; else if(S_ISFIFO(buf.st_mode)) type = 'p'; else if(S_ISSOCK(buf.st_mode)) type = 's'; // get the permission of file if(buf.st_mode & S_IRUSR) permission[0] = 'r'; if(buf.st_mode & S_IWUSR) permission[1] = 'w'; if(buf.st_mode & S_IXUSR) permission[2] = 'x'; if(buf.st_mode & S_IRGRP) permission[3] = 'r'; if(buf.st_mode & S_IWGRP) permission[4] = 'w'; if(buf.st_mode & S_IXGRP) permission[5] = 'x'; if(buf.st_mode & S_IROTH) permission[6] = 'r'; if(buf.st_mode & S_IWOTH) permission[7] = 'w'; if(buf.st_mode & S_IXOTH) permission[8] = 'x'; // get the user name and group name pwd = getpwuid(buf.st_uid); grp = getgrgid(buf.st_gid); if(NULL == pwd) { printf("pw is null \n"); exit(1); } if(NULL == grp) { printf("grp is null \n"); exit(1); } // show file type printf("%c",type); // show permission of usr, grout and other while(i<9) { printf("%c",permission[i]); i++; } // show the count of link printf("%2d ",buf.st_nlink); // show the user name and group name printf("%-4s",pwd->pw_name); printf("%-4s",grp->gr_name); // show the size of file printf( "%6ld ",buf.st_size); // show the time of file printf("%.12s",ctime(&buf.st_mtime)+4);//+ 4 skip the weekday,12s don't show year info // show the name of file printf(" %s\n",name); } else { printf("can't get the state of %s \n",name); exit(1); }}
Let's explain the above Code: stat is a struct type, which contains detailed information about the file, such as the uid we use in the code. We can make full use of these attributes to obtain detailed information about the file.
For example, we use macros such as S_ISDIR and the st_mode attribute to determine whether the file type is a directory or other type (common files, links, pipelines, etc ). We can also use macros such as S_IRUSR and st_mode to judge the three sets of permission values (rwxrw-r-) of the file -).
The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it. In addition, this function must be used with main. c and main. h in the previous chapter.
Below isProgram running resultFor more information, see and compare it with the running results of the ls-l command in the system.
|-> Ls-l // enter the ls-l command-rw-r -- 1 talk8 talk8 774 Dec 16 cat at the shell prompt. c-rw-r -- 1 talk8 talk8 596 Dec 13 cd. c-rw-r -- 1 talk8 talk8 955 Dec 10 20:59 date. c-rw-r -- 1 talk8 talk8 4326 Dec 17 ls. c-rw-r -- 1 talk8 talk8 1139 Dec 17 23:03 main. c-rw-r -- 1 talk8 talk8 611 Dec 17 23:02 main. h-rw-r -- 1 talk8 talk8 256 Dec 10 20:59 pwd. c-rwxr-xr-x 1 talk8 talk8 12916 Dec 17 05 s |-> exit // exit DIY shell
Let's talk about the example of DIY ls commands. I want to know what examples will be provided later, and I will try again.