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