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