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
|
#include "stat.h"
/* ls [-l] [dir]: list a directory (default: current).
* -l adds the type, link count and size - the terminal is 40 columns, so the
* long form is "d 1 256 name", not the BSD/GNU sprawl. */
static void pad(unsigned int n, unsigned char width) {
unsigned char d = 1;
unsigned int v = n;
while (v >= 10) {
v = v / 10;
d++;
}
while (d++ < width) putc(' ');
putu(n);
}
void main(void) {
char *argv[4];
char name[16];
char path[40];
struct gstat st;
unsigned char argc = argv_parse(argv, 4);
unsigned char lng = hasflag(argv, argc, 'l');
unsigned char i, j, k;
char *dir = 0;
for (i = 0; i < argc; i++)
if (argv[i][0] != '-') {
dir = argv[i];
break;
}
if (opendir(dir ? dir : ".")) {
puts("ls: no such directory");
nl();
sexit(1);
}
for (i = 0; flist(i, name); i++) {
name[15] = 0;
if (!lng) {
puts(name);
nl();
continue;
}
/* stat wants a path, and flist only hands back the leaf name */
j = 0;
if (dir) {
for (k = 0; dir[k] && j < 30; k++) path[j++] = dir[k];
if (j && path[j - 1] != '/') path[j++] = '/';
}
for (k = 0; name[k] && j < 38; k++) path[j++] = name[k];
path[j] = 0;
if (!gstat(path, &st)) {
puts("? ");
puts(name);
nl();
continue;
}
putc(st.type == ST_DIR ? 'd' : '-');
putc(' ');
pad(st.nlink, 2);
putc(' ');
pad(st.size, 5);
putc(' ');
puts(name);
nl();
}
}
|