-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
241 lines (208 loc) · 4.1 KB
/
ls.c
File metadata and controls
241 lines (208 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* **************************
* The ls command of shell
* *************************/
/* *************************
* support 3 kinds of ls command
* 1.ls
* 2.ls -a,
* 3.ls -l
* 4.othre params, add in later
* **************************/
#include "main.h"
void show_attr(char *name)
{
struct stat buf;
struct passwd *pwd;
struct group *grp;
char type ;
char permission[9];
int i = 0 ;
memset(permission,'-',9*sizeof(char));
if(!stat(name,&buf))
{
// get the type of file
if(S_ISLNK(buf.st_mode))
type = 'l';
else if(S_ISREG(buf.st_mode))
type = '-';
else if(S_ISDIR(buf.st_mode))
type = 'd';
else if(S_ISCHR(buf.st_mode))
type = 'c';
else if(S_ISBLK(buf.st_mode))
type = 'b';
else if(S_ISFIFO(buf.st_mode))
type = 'p';
else if(S_ISSOCK(buf.st_mode))
type = 's';
// get the permission of file
if(buf.st_mode & S_IRUSR)
permission[0] = 'r';
if(buf.st_mode & S_IWUSR)
permission[1] = 'w';
if(buf.st_mode & S_IXUSR)
permission[2] = 'x';
if(buf.st_mode & S_IRGRP)
permission[3] = 'r';
if(buf.st_mode & S_IWGRP)
permission[4] = 'w';
if(buf.st_mode & S_IXGRP)
permission[5] = 'x';
if(buf.st_mode & S_IROTH)
permission[6] = 'r';
if(buf.st_mode & S_IWOTH)
permission[7] = 'w';
if(buf.st_mode & S_IXOTH)
permission[8] = 'x';
// get the user name and group name
pwd = getpwuid(buf.st_uid);
grp = getgrgid(buf.st_gid);
if(NULL == pwd)
{
printf("pw is null \n");
exit(1);
}
if(NULL == grp)
{
printf("grp is null \n");
exit(1);
}
// show file type
printf("%c",type);
// show permission of usr, grout and other
while(i<9)
{
printf("%c",permission[i]);
i++;
}
// show the count of link
printf("%2d ",buf.st_nlink);
// show the user name and group name
printf("%-4s",pwd->pw_name);
printf("%-4s",grp->gr_name);
// show the size of file
printf( "%6ld ",buf.st_size);
// show the time of file
printf("%.12s",ctime(&buf.st_mtime)+4); //+ 4 skip the weekday ,12s don't show year info
// show the name of file
printf(" %s\n",name);
}
else
{
printf("can't get the state of %s \n",name);
exit(1);
}
}
void sort_name(char name[PATH_SIZE][PATH_SIZE],int len)
{
char str[PATH_SIZE];
int i,j;
int flag;
memset(str,'\0',PATH_SIZE*sizeof(char) );
flag = 1;
for(i=0; i<len && flag; ++i)
{
flag = 0;
for(j=len-1; j> i; --j)
{
if(0 < strcmp(name[j],name[j-1]))
{
strcpy(str,name[j]);
strcpy(name[j],name[j-1]);
strcpy(name[j-1],str);
flag = 1;
}
}
}
}
int print(char *str)
{
char path[PATH_SIZE];
char name[PATH_SIZE][PATH_SIZE];
char *p;
DIR * dir;
struct dirent *dir_info;
int i =0;
memset(path,'\0',PATH_SIZE*sizeof(char) );
memset(name,'\0',PATH_SIZE*sizeof(char) );
if(NULL == (p=getcwd(path,PATH_SIZE)) )
{
printf("can't get the current directory \n");
return 1;
}
if(NULL == (dir=opendir(path)) )
{
printf("can't open the directory %s \n",path);
return 1;
}
while(NULL != (dir_info=readdir(dir)) )
strcpy(name[i++],dir_info->d_name);
closedir(dir);
sort_name(name,i);
// for ls command
if( !strcmp(str,".") )
{
i -= 2; // can't show . and ..in result
while(--i>=0)
printf("%s \n",name[i]);
}
// for ls -a command
if( !strcmp(str,"a") )
{
while(--i>=0)
printf("%s \n",name[i]);
//printf("%d :%s \n",i,name[i]);
}
// for ls -l command
if( !strcmp(str,"l") )
{
i -= 2; // can't show . and ..in result
while(--i>=0)
show_attr(name[i]);
}
return 0;
}
int lss(const char *p)
{
char *start;
char *end;
int res;
int i= 0;
char *cmd[]=
{
"ls",
"ls -a",
"ls -l",
//"ls -al",
};
start = strstr(p,"ls");
if(!start )
{
printf("can't support this format \n");
return 1;
}
end = strchr(p,'\n');
*end = '\0'; // make a normal string by \0
for(i=0; i<sizeof(cmd)/sizeof(char*); ++i)
{
if( !strcmp(p,cmd[i]) )
{
switch(i)
{
case 0: res = print(".");
break;
case 1: res = print("a");
break;
case 2: res = print("l");
break;
case 3:
break;
default:
break;
}
}
}
if(res != 0)
printf("can't support %s command ,please check again \n",p);
return res;
}