-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.c
executable file
·94 lines (85 loc) · 1.95 KB
/
tls.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
#include <stdio.h>
#include <strings.h>
#include <comp421/yalnix.h>
#include <comp421/iolib.h>
#include <comp421/filesystem.h>
/*
* Works like "ls".
*
* Uncomment #define below to test symbolic links too.
*/
/* #define TEST_SYMLINKS */
int
main(int argc, char **argv)
{
int fd;
int nch;
struct dir_entry entry;
char *name;
struct Stat sb;
char buffer[DIRNAMELEN+1];
#ifdef TEST_SYMLINKS
char link[MAXPATHNAMELEN+1];
#endif
char typechar;
name = (argc > 1) ? argv[1] : ".";
if (ChDir(name) == ERROR) {
fprintf(stderr, "Can't ChDir to %s\n", name);
Shutdown();
Exit(1);
}
if ((fd = Open(".")) == ERROR) {
fprintf(stderr, "Can't Open . in %s\n", name);
Shutdown();
Exit(1);
}
while (1) {
nch = Read(fd, (char *)&entry, sizeof(entry));
if (nch == 0)
break;
else if (nch < 0) {
fprintf(stderr, "ERROR Reading from directory\n");
Shutdown();
Exit(1);
} else if (nch != sizeof(entry)) {
fprintf(stderr, "Read wrong byte count %d\n", nch);
Shutdown();
Exit(1);
}
if (entry.inum == 0) continue;
bcopy(entry.name, buffer, DIRNAMELEN);
buffer[DIRNAMELEN] = '\0';
/* was LinkStat, when supporting symlinks ??? */
if (Stat(buffer, &sb) == ERROR) {
fprintf(stderr, "Can't LinkStat %s\n", buffer);
Shutdown();
Exit(1);
}
switch (sb.type) {
case INODE_REGULAR: typechar = ' '; break;
case INODE_DIRECTORY: typechar = 'd'; break;
#ifdef TEST_SYMLINKS
case INODE_SYMLINK: typechar = 's'; break;
#endif
default: typechar = '?'; break;
}
printf("%4d %c %3d %5d %s",
entry.inum, typechar, sb.nlink, sb.size, buffer);
#ifdef TEST_SYMLINKS
if (sb.type == INODE_SYMLINK) {
nch = ReadLink(buffer, link, sizeof(link));
if (nch == ERROR) {
fprintf(stderr, "Can't ReadLink %s\n",
buffer);
Shutdown();
Exit(1);
}
link[nch] = '\0';
printf(" -> %s\n", link);
}
else
#endif
printf("\n");
}
Shutdown();
}