Ubuntu下实现ls -l:i:a:h:
getopt.c
不同的选项参数调用不同的函数:
1 #include<stdio.h> 2 #include<unistd.h> 3 #include"stat.h" 4 int main(int argc,char *argv[]) 5 { 6 char *optstring="-l:i:a:h:"; 7 int c; 8 while(1) 9 { 10 c=getopt(argc,argv,optstring); 11 if(c==-1) 12 break; 13 switch(c) 14 { 15 case 'l': 16 listl(optarg); 17 break; 18 case 'i': 19 listi(optarg); 20 break; 21 case 'a': 22 lista(optarg); 23 break; 24 case 'h': 25 listh(optarg); 26 break; 27 case 'r': 28 printf("识别到可选参数的选项-e\n"); 29 if(optarg !=NULL) 30 printf("参数:%s\n",optarg); 31 break; 32 case '?': 33 printf("不认识此选项%s\n",argv[optind-1]); 34 break; 35 case 1:
36 printf("非选项参数%s\n",argv[optind-1]); 37 break; 38 default : 39 break; 40 } 41 } 42 }
stat.c
四个选项功能函数实现:
1 #include"stat.h" 2 void listl(char *path) 3 { 4 struct stat pstat; 5 glob_t pglob; 6 char buf[BUFFSIZE]={}; 7 if(lstat(path,&pstat)<0) 8 { 9 perror("lstat()"); 10 return; 11 } 12 if(!S_ISDIR(pstat.st_mode)) 13 { 14 statjudge(path); 15 return; 16 } 17 strcpy(buf,path); 18 strcat(buf,"/*"); 19 glob(buf,0,NULL,&pglob); 20 // memset(buf,'\0',BUFFSIZE); 21 for(int i=0;i<pglob.gl_pathc;i++) 22 { 23 statjudge((pglob.gl_pathv)[i]); 24 } 25 globfree(&pglob); 26 27 28 29 } 30 void listi(char *path) 31 { 32 struct stat pstat; 33 glob_t pglob; 34 char buf[BUFFSIZE]={}; 35 if(lstat(path,&pstat)<0) 36 { 37 perror("lstat()"); 38 return; 39 } 40 if(!S_ISDIR(pstat.st_mode)) 41 { 42 printf("%ld %s\n",pstat.st_ino,path); 43 return; 44 } 45 strcpy(buf,path); 46 strcat(buf,"/*"); 47 glob(buf,0,NULL,&pglob); 48 // memset(buf,'\0',BUFFSIZE); 49 for(int i=0;i<pglob.gl_pathc;i++) 50 { 51 if(lstat(((pglob.gl_pathv)[i]),&pstat)<0) 52 { 53 perror("lstat()"); 54 return; 55 } 56 char *o=strrchr((pglob.gl_pathv)[i],'/'); 57 printf("%ld %s\n",pstat.st_ino,++o); 58 59 } 60 globfree(&pglob); 61 } 62 void lista(char *path) 63 { 64 struct stat pstat; 65 glob_t pglob; 66 char buf[BUFFSIZE]={}; 67 if(lstat(path,&pstat)<0) 68 { 69 perror("lstat()"); 70 return; 71 } 72 if(!S_ISDIR(pstat.st_mode)) 73 { 74 printf("%s ",path); 75 return; 76 } 77 DIR *dp=opendir(path); 78 struct dirent *rd=NULL; 79 while(1) 80 { 81 rd=readdir(dp); 82 if(rd==NULL) 83 { 84 if(errno) 85 { 86 perror("readdir()"); 87 closedir(dp); 88 return; 89 } 90 break; 91 } 92 printf("%s ",rd->d_name); 93 94 } 95 closedir(dp); 96 printf("\n"); 97 return; 98 } 99 void listh(char *path) 100 { 101 struct stat pstat; 102 // char *p=NULL; 103 glob_t pglob; 104 char buf[BUFFSIZE]={};
105 if(lstat(path,&pstat)<0) 106 { 107 perror("lstat()"); 108 return; 109 } 110 if(!S_ISDIR(pstat.st_mode)) 111 { 112 printf("%s ",path); 113 return; 114 } 115 DIR *dp=opendir(path); 116 struct dirent *rd=NULL; 117 while(1) 118 { 119 rd=readdir(dp); 120 if(rd==NULL) 121 { 122 if(errno) 123 { 124 perror("readdir()"); 125 closedir(dp); 126 return; 127 } 128 break; 129 } 130 if((rd->d_name)[0]=='.') 131 continue; 132 printf("%s ",rd->d_name); 133 134 } 135 closedir(dp); 136 printf("\n"); 137 138 139 140 } 141 void statjudge(char *path) 142 { 143 144 struct stat mystat; 145 if(stat(path,&mystat)==-1) 146 { 147 perror("stat()"); 148 return ; 149 } 150 switch(mystat.st_mode & S_IFMT)
151 { 152 case S_IFSOCK: 153 printf("s"); 154 break; 155 case S_IFLNK: 156 printf("l"); 157 break; 158 case S_IFREG: 159 printf("-"); 160 break; 161 case S_IFBLK: 162 printf("b"); 163 break; 164 case S_IFDIR: 165 printf("d"); 166 break; 167 case S_IFCHR: 168 printf("c"); 169 break; 170 case S_IFIFO: 171 printf("p"); 172 break; 173 fefault : 174 break; 175 } 176 // printf("\n"); 177 if(mystat.st_mode & S_IRUSR) 178 putchar('r'); 179 else 180 putchar('-'); 181 if(mystat.st_mode & S_IWUSR) 182 putchar('w'); 183 else 184 putchar('-'); 185 if(mystat.st_mode & S_IXUSR) 186 if(mystat.st_mode & S_ISUID) 187 putchar('s'); 188 else 189 putchar('x'); 190 else 191 putchar('-'); 192 if(mystat.st_mode & S_IRGRP) 193 putchar('r'); 194 else 195 putchar('-'); 196 if(mystat.st_mode & S_IWGRP) 197 putchar('w'); 198 else 199 putchar('-');
200 if(mystat.st_mode & S_IXGRP) 201 if(mystat.st_mode & S_ISGID) 202 putchar('s'); 203 else 204 putchar('x'); 205 else 206 putchar('-'); 207 if(mystat.st_mode & S_IROTH) 208 putchar('r'); 209 else 210 putchar('-'); 211 if(mystat.st_mode & S_IWOTH) 212 putchar('w'); 213 else 214 putchar('-'); 215 if(mystat.st_mode & S_IXOTH) 216 if(mystat.st_mode & S_ISVTX) 217 putchar('t'); 218 else 219 putchar('x'); 220 else 221 putchar('-'); 222 223 printf(" %ld",mystat.st_nlink); 224 225 struct passwd *pwd=NULL;//文件拥有者 226 pwd = getpwuid(mystat.st_uid); 227 printf(" %s ",pwd->pw_name); 228 229 struct group *grp=NULL;//文件所属组 230 grp=getgrgid(mystat.st_gid); 231 printf(" %s ",grp->gr_name); 232 233 printf(" %ld ",mystat.st_size);//总字节个数 234 235 236 237 //获取文件时间 atime mtime ctime 238 struct tm *tmp=NULL; 239 tmp=localtime(&mystat.st_mtim.tv_sec); 240 //if error 241 char buf[BUFSIZE]={}; 242 strftime(buf,BUFSIZE,"%m月 %d %H:%M ",tmp); 243 printf("%s ",buf);
245 //文件名 246 char *p=strrchr(path,'/'); 247 printf("%s ",++p); 248 249 putchar('\n'); 250 return ; 251 }
stat.h
1 #ifndef __STAT_H_ 2 #define __STAT_H_ 3 #include<stdio.h> 4 #include<sys/types.h> 5 #include<sys/stat.h> 6 #include<unistd.h> 7 #include<pwd.h> 8 #include<grp.h> 9 #include<errno.h> 10 #include<time.h> 11 #include<dirent.h> 12 #include<glob.h> 13 #include<string.h> 14 #define BUFSIZE 100 15 #define BUFFSIZE 1024 16 17 void statjudge(char *path); 18 void listl(char *path); 19 void listi(char *path); 20 void lista(char *path); 21 void listh(char *path); 22 23 24 #endif
makefile:
1 main : getopt.o stat.o 2 gcc getopt.o stat.o -o main 3 clean : 4 rm -rf *.o main
运行结果:
. 当前目录
zj@zj-pc:~/4.dir/3-13.c/homework1$ ./main -l . -rw-rw-r-- 1 zj zj 695 03月 15 15:57 getopt.c -rw-rw-r-- 1 zj zj 2656 03月 15 15:57 getopt.o -rwxrwxr-x 1 zj zj 14112 03月 15 15:57 main -rw-rw-r-- 1 root zj 78 03月 14 22:12 makefile -rw-rw-r-- 1 zj zj 3865 03月 15 12:17 stat.c -rw-rw-r-- 1 root zj 414 03月 15 11:57 stat.h -rw-rw-r-- 1 zj zj 7536 03月 15 15:57 stat.o
zj@zj-pc:~/4.dir/3-13.c/homework1$ ./main -i . 131948 getopt.c 131381 getopt.o 131951 main 132187 makefile 131957 stat.c 132121 stat.h 131950 stat.o
zj@zj-pc:~/4.dir/3-13.c/homework1$ ./main -a . getopt.o . makefile stat.c main stat.o stat.h .. getopt.c zj@zj-pc:~/4.dir/3-13.c/homework1$ ./main -h . getopt.o makefile stat.c main stat.o stat.h getopt.c