Simple LS implementation, first, we need to traverse the parameters directory under the various files, and then according to the nature of the file, read the permissions of the file, user group, user name, size, the last time to access, and then according to the name of the order displayed.
The specific function declarations are as follows:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <sys/stat.h>5#include <fcntl.h>6#include <unistd.h>7#include <dirent.h>8#include <sys/types.h>9#include <pwd.h>Ten#include <grp.h> One#include <time.h> A #defineCNT 256 - intfile_name (DIR *FP,Char*path,Charname[][cnt]); - voidStr_sort (CharNAME[][CNT],intCNT); the voidMode_to_char (mode_t mode,Char*buf); - Char*time_change (Char*Time ); - voidShowCharNAME[][CNT],intCNT);
View Code
Directory traversal, we need to know the number of files read in the directory, so we need to return the corresponding int value.
The traversal of the directory is implemented as follows:
1 intfile_name (DIR *FP,Char*path,Charname[][cnt])2 {3 intCNT =0;4 structDirent *p;5 while((P = readdir (fp))! =NULL)6 {7 if(STRNCMP (P->d_name,".",1) ==0|| STRNCMP (P->d_name,"..",2) ==0)8 Continue;9 strcpy (name[cnt], path);TenStrcat (name[cnt],"/"); OneStrcat (name[cnt], p->d_name); ACNT + +; - } - Closedir (FP); the returnCNT; -}
View Code
Then we need to understand the permissions of the file, and the file permissions are stored in the corresponding parameter char *buf.
The interpretation of file permissions is implemented as follows:
1 voidMode_to_char (mode_t mode,Char*buf)2 {3memset (BUF,'-',Ten);4 if(S_isdir (mode))5buf[0] ='D';6 if(Mode &s_irusr)7buf[1] ='R';8 if(Mode &s_iwusr)9buf[2] ='W';Ten if(Mode &s_ixusr) Onebuf[3] ='x'; A if(Mode &s_irgrp) -buf[4] ='R'; - if(Mode &s_iwgrp) thebuf[5] ='W'; - if(Mode &s_ixgrp) -buf[6] ='x'; - if(Mode &S_iroth) +buf[7] ='R'; - if(Mode &S_iwoth) +buf[8] ='W'; A if(Mode &S_ixoth) atbuf[9] ='x'; -}
View Code
To be expected, the time display does not need to be so precise, so we should shorten the time accuracy appropriately.
The display of time is implemented as follows:
1 Char*time_change (Char*Time )2 {3 intindex = strlen (time)-1;4 for(; Time[index]! =':'; Index--);5Time[index] =' /';6 returnTime +4;7}
View Code
Then we need to sort by the dictionary order according to the file name.
The implementation of the sort is as follows:
1 voidStr_sort (CharNAME[][CNT],intCNT)2 {3 intindex, POS;4 Charstr[cnt];5 for(pos =1; POS < CNT; POS + +)6 {7 strcpy (str, name[pos]);8 for(index = pos-1; Index >=0; Index--)9 if(strcmp (Name[index], str) >0)Tenstrcpy (Name[index +1], Name[index]); One Else A Break; -strcpy (Name[index +1], str); - } the}
View Code
Finally, we are writing a simple show () function to display the information for each file.
The show function is implemented as follows:
1 voidShowCharNAME[][CNT],intCNT)2 {3 intindex;4 Charmode[Ten];5 Char*str;6 structstat buf;7 for(index =0; Index < CNT; Index + +)8 {9memset (&buf,0,sizeof(BUF));Ten if(Stat (Name[index], &buf) = =-1) One { Aprintf"Stat error!! \ n"); -Exit1); - } the Mode_to_char (buf.st_mode, mode); -str = CTime (&buf.st_atime); -str =Time_change (str); - inti; + for(i = strlen (Name[index])-1; Name[index][i]! ='/'; I--); -i++; +printf"%10s.%2d%5s%5s%5d%13s%s\n", mode, Buf.st_nlink, Getpwuid (buf.st_uid)->pw_name, Getgrgid (buf.st_gid)->gr_name, Buf.st_size, str, name[ Index] +i); A } at}
View Code
Here's what to note:
Getpwuid () is not returning the username we want, we need a variable--pw_name in the struct, and the same getgrid () should be converted accordingly.
The test code is as follows:
1#include"head.h"2 intMainintargcChar*argv[])3 {4DIR *FP;5 Charname[cnt][cnt];6 intCNT;7fp = Opendir (argv[1]);8 if(fp = =NULL)9 {Tenprintf"Opendir error!! \ n"); OneExit1); A } -CNT = file_name (FP, argv[1], name); - str_sort (name, CNT); the Show (name, CNT); - return 0; -}
View Code
Simple implementation of Linux basic--ls features