基本实现了明令ls -l的功能,能显示列表,但还有如下缺陷:结果没有排序,不能显示总大小,不能显示其他目录下的文件列表。
程序中使用了掩码的技术来实现数值到文件类型、文件权限列表字符串的转换,使用命令getpwuid(uid_t uid)将用户id转换为名子,使用getgrgid(gid_t gid)将组id转换为组名。
下面是代码:
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <string.h>
6
7 void do_ls(char[]);
8 void dostat(char *);
9 void show_file_info(char *filename, struct stat *buf);
10 void mode_to_letters(int, char[]);
11 char *uid_to_name(uid_t);
12 char *gid_to_name(gid_t);
13
14 main(int ac, char *av[])
15 {
16 if (ac == 1)
17 do_ls(".");
18 else
19 while(--ac)
20 {
21 printf("%s: \n", *++av);
22 do_ls(*av);
23 }
24 }
25
26 void do_ls(char dirname[])
27 {
28 /*
29 * list files in directory called dirname
30 */
31
32 DIR *dir_ptr; //the directory
33 struct dirent *direntp; //each entry
34
35 if( (dir_ptr = opendir(dirname)) == NULL)
36 fprintf(stderr, "ls1:cannot open %s\n", dirname);
37 else
38 {
39 while ( (direntp = readdir(dir_ptr)) != NULL)
40 dostat(direntp->d_name);
41
42 closedir(dir_ptr);
43 }
44
45 }
46
47
48 void dostat(char *filename)
49 {
50 struct stat info;
51 if (stat(filename, &info) == -1)
52 perror(filename);
53 else
54 show_file_info(filename, &info);
55 }
56
57 void show_file_info(char *filename, struct stat *buf)
58 {
59 /*
60 * display some info stat in a name = value formant
61 */
62 char *uid_to_name(uid_t);
63 char * ctime(time_t *);
64 char * gid_to_name(gid_t), filemode();
65 //void mode_to_letters();
66 char modestr[11];
67
68 mode_to_letters(buf->st_mode, modestr);
69
70 printf("%s", modestr);
71 printf("%4d", buf->st_nlink);
72 printf("%-8s", uid_to_name(buf->st_uid));
73 printf("%-8s", gid_to_name(buf->st_gid));
74 printf("%8ld", (long)buf->st_size);
75 //printf("modtime:%d\n",buf->st_mtime);
76 printf("%.12s",4+ctime(&buf->st_mtime));
77 printf("%s\n", filename);
78
79 }
80
81 /*
82 * utility functions
83 */
84
85 void mode_to_letters(int mode, char str[])
86 {
87 strcpy(str, "----------");
88
89 if(S_ISDIR(mode)) str[0] = 'd';
90 if(S_ISCHR(mode)) str[0] = 'c';
91 if(S_ISBLK(mode)) str[0] = 'b';
92
93 if(mode & S_IRUSR) str[1] = 'r';
94 if(mode & S_IWUSR) str[2] = 'w';
95 if(mode & S_IXUSR) str[3] = 'x';
96
97 if(mode & S_IRGRP) str[4] = 'r';
98 if(mode & S_IWGRP) str[5] = 'w';
99 if(mode & S_IXGRP) str[6] = 'x';
100
101 if(mode & S_IROTH) str[7] = 'r';
102 if(mode & S_IWOTH) str[8] = 'w';
103 if(mode & S_IXOTH) str[9] = 'x';
104 }
105
106 #include <pwd.h>
107
108 char *uid_to_name(uid_t uid)
109 /*
110 * returns pointer to username associated with uid, uses getpw()
111 */
112 {
113 struct passwd *pw_ptr;
114 static char numstr[10];
115
116 if( (pw_ptr = getpwuid(uid)) == NULL)
117 {
118 printf(numstr, "%d", uid);
119 return numstr;
120 }
121 else
122 return pw_ptr->pw_name;
123 }
124
125 #include <grp.h>
126
127 char *gid_to_name(gid_t gid)
128 {
129 struct group *grp_ptr;
130 static char numstr[10];
131
132 if ((grp_ptr = getgrgid(gid)) == NULL)
133 {
134 sprintf(numstr, "%d", gid);
135 return numstr;
136 }
137 else
138 return grp_ptr->gr_name;
139 }
140
141
争取早日将其他功能实现。