Ladies and gentlemen, crossing, the last time we were talking about DIY cat command example, this time we say the example is: theDIY ls command continued . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
We in the previous chapter back to DIY the LS command, time is not long, I believe we still have the impression. Today we are a supplement to the previous chapter, the main is to extend the LS command, so that it can support the L parameter on the original basis.
We have added support for the L parameter, the following is the specific operation process , please refer to:
- 1. Use the stat function to get the properties of the file from the file;
- 2. According to the attributes of the St_mode value to determine the type of file, such as directories, links, pipelines, etc.;
- 3. Extract the permissions of the file from the St_mode value of the attribute (RWX), and extract the method to use the bitwise operation and operation;
- 4. Use the Getpwuid function to obtain the user name of the file based on the St_uid value of the attribute;
- 5. Use the Getgrgid function to get the group name of the file based on the St_gid value of the attribute;
- 6. Use the CTime function to calculate the modified date and time of the file based on the St_mtime value of the attribute;
- 7. According to the LS-L format, adjust the display format, the values in steps 2 through 6 are output to the screen.
Below is our DIY code, please refer to:
voidShow_attr (Char*name) {structStat buf;structpasswd *pwd;structGroup *grp;Chartype;Charpermission[9];inti =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 namePWD = 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 the file printf("%s\n", name); }Else{printf("can ' t get the state of%s \ n", name);Exit(1); }}
Let's make some notes about the above code: stat is a struct type, and members of that type include details about the file, such as the UID we use in the code. We can take advantage of these properties to get the details of the file.
For example, we use macros such as S_isdir and combine the St_mode property to determine whether the type of file is a directory or other type (normal file, link, pipeline, etc.). We can also use macros such as S_IRUSR and combine St_mode to determine the file's three set of permission values (rwxrw-r–).
Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use. In addition, the function needs to be used in conjunction with MAIN.C and Main.h in the previous chapter.
The following is the results of the operation of the program , please refer to, and with the system LS-L command running results compared
|->Ls- L //Enter the LS-L command at the shell prompt-RW- R--R-- 1Talk8 Talk8774Dec - A: -Cat.C-RW- R--R-- 1Talk8 Talk8596Dec - the: -Cd.C-RW- R--R-- 1Talk8 Talk8955DecTen -: - Date.C-RW- R--R-- 1Talk8 Talk84326Dec - at: toLs.C-RW- R--R-- 1Talk8 Talk81139Dec - at:GenevaMain.C-RW- R--R-- 1Talk8 Talk8611Dec - at: GenevaMain.H-RW- R--R-- 1Talk8 Talk8 theDecTen -: -Pwd.C-RWXR-XR- x 1Talk8 Talk812916Dec - at: toS|->Exit//Quit DIY shell
Everyone crossing, the example of the DIY ls command is here. I want to know what the following example, and listen to tell.
Talk C Chestnut Bar (77th: C-language instance--diy ls command cont.)