main.c 文件
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <glob.h>
5 #include <error.h>
6 #include <dirent.h>
7 #include "myls.h"
8
9 void stat_attbute(struct stat *buf);
10 int is_not_dir(char *name);
11
12 int main(int argc, char **argv)
13 {
14 int ch = 0;
15 struct stat buf;
16 char path[1024];
17 DIR *dp = NULL;
18 struct dirent * ret;
19
20 char *optstring = "-lia";
21
22 while(ch != -1)
23 {
24 ch = getopt(argc, argv, optstring);
25
26 if(argv[1] == NULL && ch == -1)
27 {
28 ch = 'm';
29 strcpy(path, ".");
30 }
31 else
32 if(ch == 1)
33
34 strcpy(path,argv[optind - 1]);
35
36 dp = opendir(path);
37
38
39 switch(ch)
40 {
41
42 case 'l':
43 while(1)
44 {
45 ret = readdir(dp);
46 if(ret == NULL)
47 {
48 if(ret)
49 {
50 perror("readdir()");
51 closedir(dp);
52 }
53 break;
54 }
55 if(!is_not_dir(ret->d_name))
56 continue;
57
58 if(stat(ret->d_name, &buf))
59 {
60 perror("stat()");
61 closedir(dp);
62 }
63 stat_attbute(&buf);
64 printf("%s\n",ret->d_name);
65 }
66 break;
67 case 'i':
68 while(1)
69 {
70 ret = readdir(dp);
71 if(ret == NULL)
72 {
73 if(ret)
74 {
75 perror("readdir()");
76 closedir(dp);
77 }
78 break;
79 }
80 if(!is_not_dir(ret->d_name))
81 continue;
82 if(stat(ret->d_name, &buf))
83 {
84 perror("stat()");
85 closedir(dp);
86 return -1;
87 }
88 stat_ino(&buf);
89 printf("%s\n",ret->d_name);
90 }
91 break;
92
93 case 'a':
94 while(1)
95 {
96 ret = readdir(dp);
97 if(ret == NULL)
98 {
99 if(ret)
100 {
101 perror("readdir()");
102 closedir(dp);
103 }
104 break;
105 }
106 printf("s ",ret->d_name);
107 }
108 break;
109
110 case 'm':
111 while(1)
112 {
113 ret = readdir(dp);
114 if(ret == NULL)
115 {
116 if(ret)
117 {
118 perror("readdir()");
119 closedir(dp);
120 }
121 break;
122 }
123 if(!is_not_dir(ret->d_name))
124 continue;
125 printf(" %s",ret->d_name);
126 }
127 ch = -1;
128 break;
129
130 case '?':
131 printf("沒(méi)有此選項(xiàng)%s\n",argv[optind - 1]);
132 break;
133 default:
134 break;
135 }
136
137 }
138
139
140
141 return 0;
142 }
143
144 void stat_attbute(struct stat *buf)
145 {
146
147 stat_file(buf);//文件類(lèi)型
148 stat_mode(buf);//文件權(quán)限
149 stat_link(buf);//hard link
150 stat_usr(buf);//文件用戶(hù)名
151 stat_gup(buf);//文件組用戶(hù)名
152 stat_byte(buf);//文件字節(jié)大小
153 stat_time(buf);//文件最后保存時(shí)間
154
155 }
156
157 int is_not_dir(char *name)
158 {
159
160 if(name[0] == '.')
161 return 0;
162
163 return 1;
164 }
165
166
myls.h文件
1 #ifndef __MYLS_H
2 #define __MYLS_H
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <grp.h>
9 #include <pwd.h>
10 void stat_file(struct stat *buf);//文件類(lèi)型
11 void stat_mode(struct stat *buf);//文件權(quán)限
12 void stat_link(struct stat *buf);//hard link
13 void stat_usr(struct stat *buf);//文件用戶(hù)名
14 void stat_gup(struct stat *buf);//文件組用戶(hù)名
15 void stat_byte(struct stat *buf);//文件字節(jié)大小
16 void stat_time(struct stat *buf);//文件最后保存時(shí)間
17 void stat_ino(struct stat *buf);//ino
18 #endif
myls.c文件
1 #include <stdio.h>
2 #include "myls.h"
3 void stat_file(struct stat *buf)//文件類(lèi)型
4 {
5
6 switch(buf->st_mode & S_IFMT)
7 {
8 case S_IFIFO :
9 printf("p");
10 break;
11
12 case S_IFCHR :
13 printf("c");
14 break;
15
16 case S_IFDIR :
17 printf("d");
18 break;
19
20 case S_IFBLK :
21 printf("b");
22 break;
23 case S_IFREG :
24 printf("-");
25 break;
26 case S_IFLNK :
27 printf("l");
28 break;
29 case S_IFSOCK :
30 printf("s");
31 break;
32 }
33
34
35 }
36 void stat_mode(struct stat *buf)//文件權(quán)限
37 {
38 if(buf->st_mode & S_IRUSR)
39 printf("r");
40 else
41 putchar('-');
42
43 if(buf->st_mode & S_IWUSR)
44 printf("w");
45 else
46 putchar('-');
47
48 if(buf->st_mode & S_IXUSR)
49 printf("x");
50 else
51 putchar('-');
52
53 if(buf->st_mode & S_IRGRP)
54 printf("r");
55 else
56 putchar('-');
57
58 if(buf->st_mode & S_IWGRP)
59 printf("w");
60 else
61 putchar('-');
62
63 if(buf->st_mode & S_IXGRP)
64 printf("x");
65 else
66 putchar('-');
67
68 if(buf->st_mode & S_IROTH)
69 printf("r");
70 else
71 putchar('-');
72
73 if(buf->st_mode & S_IWOTH)
74 printf("w");
75 else
76 putchar('-');
77
78 if(buf->st_mode & S_IWOTH)
79 printf("x");
80 else
81 putchar('-');
82 putchar(' ');
83 }
84 void stat_link(struct stat *buf)//hard link
85 {
86 printf("%ld ", buf->st_nlink);
87 }
88 void stat_usr(struct stat *buf)//文件用戶(hù)名
89 {
90 struct passwd *pwd = NULL;
91
92 pwd = getpwuid(buf->st_uid);
93
94 printf("%s ",pwd->pw_name);
95
96 }
97 void stat_gup(struct stat *buf)//文件組用戶(hù)名
98 {
99 struct group *gnam = NULL;
100
101 gnam = getgrgid(buf->st_gid);
102
103 printf("%s ",gnam->gr_name);
104
105 }
106 void stat_byte(struct stat *buf)//文件字節(jié)大小
107 {
108 printf("%ld ",buf->st_size);
109
110 }
111 void stat_time(struct stat *buf)//文件最后保存時(shí)間
112 {
113 struct tm *times;
114 char buff[1024];
115
116 times = localtime(&buf->st_mtim.tv_sec);
117
118 strftime(buff, 1024, "%m月 %d %H:%M", times);
119 printf("%s ",buff);
120 }
121 void stat_ino(struct stat *buf)//ino
122 {
123 printf("%ld ",buf->st_ino);
124 }
~
|
|
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》