By xxx0624
Done:
ls
ls -a
ls -l
ls /tmp
ls -R
ls -t
FileName color
FileName output
1 /* 2 By xxx0624 3 Done: 4 ls 5 ls -a 6 ls -l 7 ls /tmp 8 ls -R 9 ls -t 10 FileName color 11 FileName output 12 */ 13 14 #include<stdio.h> 15 #include<string.h> 16 #include<stdlib.h> 17 #include<sys/types.h> 18 #include<sys/stat.h> 19 #include<sys/ioctl.h> 20 #include<dirent.h> 21 #include<pwd.h> 22 #include<grp.h> 23 #include<unistd.h> 24 /*head file*/ 25 26 27 #define LenOfName 256 28 #define maxN 1005 29 #define maxM 505 30 #define maxL 105 31 #define LenOfPath 256<<4 32 #define LS 0 //ls 33 #define LS_A 1 //ls -a 34 #define LS_L 2 //ls -l 35 #define LS_TMP 3 //ls /tmp 36 #define LS_R 4 //ls -R 37 #define LS_T 5 //ls -t 38 /*define file*/ 39 40 41 void do_ls( int,char [] ); 42 void dostat( char * ); 43 void show_file_info( char *,struct stat * ); 44 void mode_to_letters( int,char [] ); 45 char *uid_to_name( uid_t ); 46 char *gid_to_name( gid_t ); 47 void getcolor( char * ); 48 int get_file_type( char * ); 49 int get_modify_time( char * ); 50 void getWidth(); 51 void display_R( char * ); 52 int cmp1( const void * ,const void * ); 53 int cmp2( const void * ,const void * ); 54 int cmp3( const void * ,const void * ); 55 56 57 struct outputFile{ 58 char FileName[ LenOfName ]; 59 int modify_time ; 60 int file_type ; 61 }Output[ maxN ],OutputPoint[ maxM ],Temp[ maxN+maxM ]; 62 int colormode,foreground,background; 63 int terminalWidth ; 64 65 66 void dostat( char *filename ){ 67 struct stat info; 68 if( stat( filename,&info )==-1 ){ 69 perror( filename ); 70 } 71 else{ 72 getcolor( filename ); 73 show_file_info( filename,&info ); 74 } 75 return ; 76 } 77 /*get file info*/ 78 79 80 void mode_to_letters( int mode,char str[] ){ 81 strcpy( str,"----------" ); 82 83 if( S_ISDIR( mode ) ) str[0] = 'd'; 84 if( S_ISCHR( mode ) ) str[0] = 'c'; 85 if( S_ISBLK( mode ) ) str[0] = 'b'; 86 87 if( mode&S_IRUSR ) str[1] = 'r'; 88 if( mode&S_IWUSR ) str[2] = 'w'; 89 if( mode&S_IXUSR ) str[3] = 'x'; 90 91 if( mode&S_IRGRP ) str[4] = 'r'; 92 if( mode&S_IWGRP ) str[5] = 'w'; 93 if( mode&S_IXGRP ) str[6] = 'x'; 94 95 if( mode&S_IROTH ) str[7] = 'r'; 96 if( mode&S_IWOTH ) str[8] = 'w'; 97 if( mode&S_IXOTH ) str[9] = 'x'; 98 99 return ; 100 } 101 102 103 char *uid_to_name( uid_t uid ){ 104 struct passwd *pw_ptr; 105 static char numstr[ 10 ]; 106 if( (pw_ptr = getpwuid( uid ) )==NULL ){ 107 sprintf(numstr,"%d",uid); 108 return numstr; 109 } 110 else{ 111 return pw_ptr->pw_name; 112 } 113 } 114 char *gid_to_name( gid_t gid ){ 115 struct group *grp_ptr; 116 static char numstr[ 10 ]; 117 if( (grp_ptr = getgrgid( gid ) )==NULL ){ 118 sprintf(numstr,"%d",gid); 119 return numstr; 120 } 121 else{ 122 return grp_ptr->gr_name; 123 } 124 } 125 /*get userid & groupid*/ 126 127 128 int get_file_type( char *filename ){ 129 struct stat info; 130 stat( filename,&info ); 131 int file_type = 0; 132 file_type = info.st_mode & S_IFMT; 133 return file_type; 134 } 135 /*get file type*/ 136 137 138 int get_modify_time( char *filename ){ 139 struct stat info; 140 stat( filename,&info ); 141 int modify_time = 0; 142 modify_time = info.st_mtime; 143 return modify_time; 144 } 145 /*get file last modify time*/ 146 147 148 int isadir(char *str) 149 { 150 struct stat info; 151 152 return ( lstat(str,&info) != -1 && S_ISDIR(info.st_mode) ); 153 } 154 /*judge the file is or not a dir*/ 155 156 157 void display_R( char *filename ){ 158 char fullpath[ LenOfPath ]; 159 struct outputFile Output2[ maxN ]; 160 getcolor( filename ); 161 printf("