/* implementation ll command, output specified directory under Details format:ll -l dirname Table of contents in quick order, sorted by dictionary */#include <stdio.h> #include <dirent.h> #include < stdlib.h> #include <sys/stat.h> #include <string.h> #include <time.h> #include <grp.h> #include <pwd.h> #define NUMLINE 40#define NUMCHAR 20/* Generate Shield character */void mode_to_letters (int mode, char str[]) { strcpy (str, "----------"); if (S_isdir (mode)) str[0] = ' d '; if (S_ISCHR (mode)) str[0] = ' C '; if (S_ISBLK (mode)) str[0] = ' B '; if ( MODE&NBSP;&&NBSP;S_IRUSR) str[1] = ' R '; if (mode & &NBSP;S_IWUSR) str[2] = ' W '; if (mode & s_ixusr) str[3] = ' x '; if (MODE&NBSP;&&NBSP;S_IRGRP) str[4] = ' R '; if (MODE&NBSP;&&NBSP;S_IWGRP) str[5] = ' W '; if (MODE&NBSP;&&NBSP;S_IXGRP) str[6] = ' x '; if (Mode & s_iroth) str[7] = ' R '; if (Mode & s_iwoth) str[8] = ' W '; if (mode & s_ixoth) str[9] = ' x '; str[10] = ' + ';} /* string Exchange */void exchange (CHAR&NBSP;*S1,&NBSP;CHAR&NBSP;*S2) { char buf[20]; strcpy (BUF,&NBSP;S1); strcpy (S1, s2); strcpy (S2,&NBSP;BUF);} /* string Sort */void sort (char filename[][numchar], int beg, int end) { int n = beg + 1; int m = end; if (beg >= end) return; while ( n < m) if (strcmp (Filename[n], filename[beg]) > 0) while (n < m) if (filename[m] < Filename[beg]) { exchange (filename[m--], filename[n++]); break; } else m--; else n++; if (strcmp (filename[n], Filename[beg] > 0) exchange (filename[--n], Filename[beg]); else exchange (filename[n ], filename[beg]); sort (filename, 0, n-1); sort ( Filename, n+1, end);}/* through the Getpwuid function, the user ID is obtained by the username */char* get_username (uid_t uid) { struct passwd *ptr; if ((Ptr=getpwuid (UID)) == null) { fprintf (stderr, "Get username error"); exit (1); } return ptr->pw_name;} /* Get Group name */char* get_groupname (gid_t gid) { struct by group ID group *ptr; if (Ptr = getgrgid (GID)) == null) { fprintf (stderr, "Get groupname error"); exit (1); } Return ptr->gr_name;} /* Time format Conversion */void timetrans (time_t *tim, char* s) {&NBsp; struct tm *temp; temp = localtime (Tim); strftime (s, 20, "%m %e %r", temp);} Int main (int argc, char* argv[]) { dir* dr; struct dirent* dir; char filename[NUMLINE][NUMCHAR]; struct stat buf; int n, m, p; char s1[11]; char tim[20]; struct tm * temp; if (argc < 3) { fprintf (stderr, "Ls02:missing operand"); exit (1 ); } if (strcmp (argv[1], "-L")) { fprintf (STDERR,&NBSP; " Ls02: operand:%s is wrong ", argv[1]); exit ( 1); } dr = opendir (argv[2]); if (Dr == null) { perror (argv[2]); exit (1); } m = 0; while ((Dir = readdir (DR) != null) { if (strcpy (filename[m], argv[2]) == null) { perror ("Strcpy error"); exit (1); } strcpy (filename[m], argv[2]); &nBsp; p = 0; while (filename[m][p++] != ' ; strcpy ') (&filename[m][p-1], dir->d _name); m++; } Sort (filename, 0, --m); for (n = 0;n <= m;n++) if (Stat (FILENAME[N],&NBSP;&BUF) == - 1) printf ("stat error\n"); else { mode_to_letters (BUF.ST_MODE,&NBSP;S1); timetrans (&buf.st_mtime, tim); printf ("%s %4d %s ", s1, buf.st_nlink, get_username (buf.st_uid)); printf ("%s %7d %s %s\n", get_ GroupName (Buf.st_gid), (int) buf.st_size, tim, &filename[n][p-1]); } closedir (DR);}
Summarize:
Delayed the time in the fast sort place
Two-dimensional arrays do function parameters, 2nd dimension cannot be empty
/* ls Command format: ls dirname*/#include <stdio.h># include <dirent.h> #include <stdlib.h>int main (int argc, char* argv[] ) { dir* dr; struct dirent* dir; if (argc == 1) { fprintf (stderr, "Ls01:missing operand"); exit (1); } dr = opendir (Argv[1]); if (dr == null) { perror (argv[1]); exit (1); } while (dir = Readdir (DR) != null) //if (Dir->d_name[0] != '. ') printf ("%s ", dir->d_name); printf ("\ n"); closedir (DR);}
Simple ll command implemented under Linux