This source file includes following definitions.
- fmtname
- ls
- main
1 #include "kernel/types.h"
2 #include "kernel/stat.h"
3 #include "user/user.h"
4 #include "kernel/fs.h"
5
6 char*
7 fmtname(char *path)
8 {
9 static char buf[DIRSIZ+1];
10 char *p;
11
12
13 for(p=path+strlen(path); p >= path && *p != '/'; p--)
14 ;
15 p++;
16
17
18 if(strlen(p) >= DIRSIZ)
19 return p;
20 memmove(buf, p, strlen(p));
21 memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
22 return buf;
23 }
24
25 void
26 ls(char *path)
27 {
28 char buf[512], *p;
29 int fd;
30 struct dirent de;
31 struct stat st;
32
33 if((fd = open(path, 0)) < 0){
34 fprintf(2, "ls: cannot open %s\n", path);
35 return;
36 }
37
38 if(fstat(fd, &st) < 0){
39 fprintf(2, "ls: cannot stat %s\n", path);
40 close(fd);
41 return;
42 }
43
44 switch(st.type){
45 case T_DEVICE:
46 case T_FILE:
47 printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
48 break;
49
50 case T_DIR:
51 if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
52 printf("ls: path too long\n");
53 break;
54 }
55 strcpy(buf, path);
56 p = buf+strlen(buf);
57 *p++ = '/';
58 while(read(fd, &de, sizeof(de)) == sizeof(de)){
59 if(de.inum == 0)
60 continue;
61 memmove(p, de.name, DIRSIZ);
62 p[DIRSIZ] = 0;
63 if(stat(buf, &st) < 0){
64 printf("ls: cannot stat %s\n", buf);
65 continue;
66 }
67 printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
68 }
69 break;
70 }
71 close(fd);
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77 int i;
78
79 if(argc < 2){
80 ls(".");
81 exit(0);
82 }
83 for(i=1; i<argc; i++)
84 ls(argv[i]);
85 exit(0);
86 }