-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathls.c
153 lines (130 loc) · 3.32 KB
/
ls.c
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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
#include "pwd.h"
#include "grp.h"
char*
fmtname(char *path)
{
static char buf[DIRSIZ+1];
char *p;
// Find first character after last slash.
for(p=path+strlen(path); p >= path && *p != '/'; p--)
;
p++;
// Return blank-padded name.
if(strlen(p) >= DIRSIZ)
return p;
memmove(buf, p, strlen(p));
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
return buf;
}
char permissions_string[11];
char*
get_permissions_string(uint mode) {
if (S_ISLNK(mode)) permissions_string[0] = 'l';
else if (S_ISREG(mode)) permissions_string[0] = '-';
else if (S_ISBLK(mode)) permissions_string[0] = 'b';
else if (S_ISDIR(mode)) permissions_string[0] = 'd';
else if (S_ISCHR(mode)) permissions_string[0] = 'c';
else if (S_ISFIFO(mode)) permissions_string[0] = 'p';
else if (S_ISSOCK(mode)) permissions_string[0] = 's';
permissions_string[1] = (S_IRUSR & mode) ? 'r' : '-';
permissions_string[2] = (S_IWUSR & mode) ? 'w' : '-';
permissions_string[3] = (S_IXUSR & mode) ? 'x' : '-';
if (S_ISUID & mode) {
if (S_IXUSR & mode) {
permissions_string[3] = 's';
} else {
permissions_string[3] = 'S';
}
}
permissions_string[4] = (S_IRGRP & mode) ? 'r' : '-';
permissions_string[5] = (S_IWGRP & mode) ? 'w' : '-';
permissions_string[6] = (S_IXGRP & mode) ? 'x' : '-';
if (S_ISGID & mode) {
if (S_IXGRP & mode) {
permissions_string[6] = 's';
} else {
permissions_string[6] = 'S';
}
}
permissions_string[7] = (S_IROTH & mode) ? 'r' : '-';
permissions_string[8] = (S_IWOTH & mode) ? 'w' : '-';
permissions_string[9] = (S_IXOTH & mode) ? 'x' : '-';
if (S_ISVTX & mode) {
if (S_IXOTH & mode) {
permissions_string[9] = 't';
} else {
permissions_string[9] = 'T';
}
}
permissions_string[10] = 0;
return permissions_string;
}
void
print_file_info(char* path, struct stat* stat)
{
printf(1, "%s %2d ", get_permissions_string(stat->mode), stat->nlink);
struct passwd* passwd = getpwuid(stat->uid);
if (passwd) printf(1, "%s ", passwd->pw_name);
else printf(1, "%d ", stat->uid);
struct group* group = getgrgid(stat->gid);
if (group) printf(1, "%s ", group->gr_name);
else printf(1, "%d ", stat->gid);
printf(1, "%5d %3d %s\n", stat->size, stat->ino, fmtname(path));
}
void
ls(char *path)
{
char buf[512], *p;
int fd;
struct dirent de;
struct stat st;
if((fd = open(path, O_NONBLOCK)) < 0){
printf(2, "ls: cannot open %s\n", path);
return;
}
if(fstat(fd, &st) < 0){
printf(2, "ls: cannot stat %s\n", path);
close(fd);
return;
}
if(S_ISREG(st.mode)){
print_file_info(path, &st);
}
if(S_ISDIR(st.mode)){
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
printf(1, "ls: path too long\n");
}
strcpy(buf, path);
p = buf+strlen(buf);
*p++ = '/';
while(read(fd, &de, sizeof(de)) == sizeof(de)){
if(de.inum == 0)
continue;
memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if(stat(buf, &st) < 0){
printf(1, "ls: cannot stat %s\n", buf);
continue;
}
print_file_info(buf, &st);
}
}
close(fd);
}
int
main(int argc, char *argv[])
{
int i;
if(argc < 2){
ls(".");
exit();
}
for(i=1; i<argc; i++)
ls(argv[i]);
exit();
}