- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <getopt.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <errno.h>
- void read_dir(char *dirname, char mode);
- void mode_to_letters(int mode, char str[]);
- void show_file_NORMAL(char *file);
- void show_file_ALL(char *file);
- void show_file_PROPERTY(char *file, char mode);
- char *uid_to_name(uid_t uid);
- char *gid_to_name(gid_t gid);
- int main(int argc, char **argv)
- {
- char mode = 0;
- if( argc == 1)
- read_dir(".", mode |= 1);
- int opt;
- struct option longopts[] = {
- {"all", 0, NULL, 'a'},
- {"ALL", 0, NULL, 'a'},
- {0, 0, 0, 0}};
- while((opt = getopt_long(argc, argv, "aAlL", longopts, NULL)) != -1)
- {
- switch(opt)
- {
- case 'a':
- case 'A':
- mode |= 2;
- break;
- case 'l':
- case 'L':
- mode |= 4;
- break;
- case '?':
- printf("未知的命令选项:%c\\n", optopt);
- break;
- }
- }
- if(!mode)
- mode |= 1;
- for(; optind < argc; optind++)
- {
- printf("目录:%s\\n", argv[optind]);
- read_dir(argv[optind], mode);
- }
- exit(0);
- }
- void read_dir(char *dirname, char mode)
- {
- DIR *dir;
- struct dirent *direntp;
- if((dir = opendir(dirname)) == NULL)
- {
- fprintf(stderr, "无法打开%s目录。\\n", dirname);
- exit(1);
- }
- chdir(dirname);
- while((direntp = readdir(dir)) != NULL)
- {
- if(mode & 4)
- show_file_PROPERTY(direntp->d_name, mode);
- else if(mode & 1)
- show_file_NORMAL(direntp->d_name);
- else if(mode & 2)
- show_file_ALL(direntp->d_name);
- }
- printf("\\n");
- closedir(dir);
- }
- void show_file_NORMAL(char *file)
- {
- if(file[0] != '.')
- printf("%-8s", file);
- }
- void show_file_ALL(char *file)
- {
- printf("%-8s", file);
- }
- void show_file_PROPERTY(char *file, char mode)
- {
- if(!(mode & 2) && (file[0] == '.'))
- return;
- char str[10];
- struct stat buf;
- if(stat(file, &buf) == -1)
- {
- perror(file);
- return;
- }
- mode_to_letters(buf.st_mode, str);
- printf("%s ", str);
- printf("%-4d", (int)buf.st_nlink);
- printf("%s ", uid_to_name(buf.st_uid));
- printf("%-8s", gid_to_name(buf.st_gid));
- printf("%8ld ", (long)buf.st_size);
- printf("%.12s ",ctime(&(buf.st_mtime)));
- printf("%s\\n", file);
- }
- #include <grp.h>
- char *gid_to_name(gid_t gid)
- {
- struct group *getgrgid(), *grp_ptr;
- static char numstr[10];
- if((grp_ptr = getgrgid(gid)) == NULL)
- {
- sprintf(numstr, "%d", gid);
- return numstr;
- }
- else
- return grp_ptr->gr_name;
- }
- #include <pwd.h>
- char* uid_to_name(uid_t uid)
- {
- struct passwd *getpwuid(), *pw_ptr;
- static char numstr[10];
- if((pw_ptr = getpwuid(uid)) == NULL)
- {
- sprintf(numstr, "%d", uid);
- return numstr;
- }
- else
- return pw_ptr->pw_name;
- }
- 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';
- }
- //该片段来自于http://www.codesnippet.cn/detail/051220137808.html
来源: http://www.codesnippet.cn/detail/051220137808.html