/* 实现ll命令,输出指定目录下详细信息 格式:ll -l dirname 目录采用快速排序,按字典顺序排序 */ #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 /* 生成屏蔽字 */ 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 & S_IRUSR) str[1] = ‘r‘; if (mode & S_IWUSR) str[2] = ‘w‘; if (mode & S_IXUSR) str[3] = ‘x‘; if (mode & S_IRGRP) str[4] = ‘r‘; if (mode & S_IWGRP) str[5] = ‘w‘; if (mode & 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] = ‘\0‘; } /* 字符串交换 */ void exchange(char *s1, char *s2) { char buf[20]; strcpy(buf, s1); strcpy(s1, s2); strcpy(s2, buf); } /* 字符串排序 */ 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); } /* 通过getpwuid函数,由用户id获得用户名 */ 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; } /* 由组id获得组名 */ char* get_groupname(gid_t gid) { struct group *ptr; if ((ptr = getgrgid(gid)) == NULL) { fprintf(stderr, "get groupname error"); exit(1); } return ptr->gr_name; } /* 时间格式转换 */ void timetrans(time_t *tim, char* s) { 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, "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]); p = 0; while(filename[m][p++] != ‘\0‘) ; strcpy(&filename[m][p-1], dir->d_name); m++; } sort(filename, 0, --m); for (n = 0;n <= m;n++) if (stat(filename[n], &buf) == -1) printf("stat error\n"); else { mode_to_letters(buf.st_mode, 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); }
总结:
在快速排序处耽误了时间
二维数组做函数形参,第2维不能为空
/* ls命令 格式: 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); }
原文:http://my.oschina.net/u/2313065/blog/474642