Commit | Line | Data |
---|---|---|
73bc81fc | 1 | /* |
3b8545c8 KB |
2 | * Copyright (c) 1982, 1986, 1988 Regents of the University of California. |
3 | * All rights reserved. | |
73bc81fc | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
3b8545c8 | 6 | * |
dbf0c423 | 7 | * @(#)ls.c 7.9 (Berkeley) %G% |
73bc81fc | 8 | */ |
a34cad54 | 9 | |
68b8c38b | 10 | #include "sys/param.h" |
68b8c38b | 11 | #include "ufs/dir.h" |
a34cad54 | 12 | #include "saio.h" |
68b8c38b | 13 | #include "sys/ttychars.h" |
a34cad54 | 14 | |
a34cad54 BJ |
15 | main() |
16 | { | |
ed632c38 | 17 | struct dinode *ip; |
3b8545c8 | 18 | int fd; |
a34cad54 | 19 | |
75e1e436 MK |
20 | for (;;) { |
21 | if ((fd = getfile("ls", 0)) == -1) | |
22 | exit(); | |
23 | ip = &iob[fd - 3].i_ino; | |
ed632c38 | 24 | if ((ip->di_mode & IFMT) != IFDIR) { |
75e1e436 MK |
25 | printf("ls: not a directory\n"); |
26 | continue; | |
27 | } | |
ed632c38 | 28 | if (ip->di_size == 0) { |
75e1e436 MK |
29 | printf("ls: zero length directory\n"); |
30 | continue; | |
31 | } | |
32 | ls(fd); | |
33 | } | |
34 | } | |
35 | ||
ed632c38 KM |
36 | #define CTRL(x) (x&037) |
37 | ||
75e1e436 MK |
38 | getfile(prompt, mode) |
39 | char *prompt; | |
40 | int mode; | |
41 | { | |
42 | int fd; | |
43 | char buf[100]; | |
44 | ||
45 | do { | |
46 | printf("%s: ", prompt); | |
47 | gets(buf); | |
48 | if (buf[0] == CTRL('d') && buf[1] == 0) | |
49 | return (-1); | |
50 | } while ((fd = open(buf, mode)) <= 0); | |
51 | return(fd); | |
a34cad54 BJ |
52 | } |
53 | ||
3b8545c8 KB |
54 | typedef struct direct DP; |
55 | static | |
56 | ls(fd) | |
57 | register int fd; | |
a34cad54 | 58 | { |
3b8545c8 KB |
59 | register int size; |
60 | register char *dp; | |
61 | char dirbuf[DIRBLKSIZ]; | |
a34cad54 | 62 | |
75e1e436 | 63 | printf("\ninode\tname\n"); |
3b8545c8 KB |
64 | while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) |
65 | for(dp = dirbuf; (dp < (dirbuf + size)) && | |
66 | (dp + ((DP *)dp)->d_reclen) < (dirbuf + size); | |
67 | dp += ((DP *)dp)->d_reclen) { | |
68 | if (((DP *)dp)->d_ino == 0) | |
69 | continue; | |
75e1e436 MK |
70 | if (((DP *)dp)->d_namlen > MAXNAMLEN+1) { |
71 | printf("Corrupt file name length! Run fsck soon!\n"); | |
72 | return; | |
73 | } | |
74 | printf("%d\t%s\n", ((DP *)dp)->d_ino, | |
75 | ((DP *)dp)->d_name); | |
a34cad54 | 76 | } |
a34cad54 | 77 | } |