xref: /original-bsd/sys/stand/ls.c (revision 3705696b)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)ls.c	8.1 (Berkeley) 06/11/93";
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 
23 main()
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 
46 getfile(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 
62 typedef struct direct	DP;
63 static
64 ls(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 }
86