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
|
#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');
char dir[32];
unsigned char i, j, k, havedir = 0;
/* Copy the directory argument onto the stack before anything else: argv
points into the command line at $A000, and gstat()'s request block is a
static - which lives at $A000 too. The first stat would otherwise eat
the argument, and every later entry would stat a garbage path. */
for (i = 0; i < argc; i++)
if (argv[i][0] != '-') {
for (j = 0; argv[i][j] && j < 31; j++) dir[j] = argv[i][j];
dir[j] = 0;
havedir = 1;
break;
}
if (!havedir) {
dir[0] = '.';
dir[1] = 0;
}
if (opendir(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 (havedir && !(dir[0] == '.' && dir[1] == 0)) {
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();
}
}
|