xref: /original-bsd/sys/stand.att/ls.c (revision 2da92b42)
1 /*
2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ls.c	7.9 (Berkeley) 06/28/90
8  */
9 
10 #include "sys/param.h"
11 #include "ufs/dir.h"
12 #include "saio.h"
13 #include "sys/ttychars.h"
14 
15 main()
16 {
17 	struct dinode *ip;
18 	int fd;
19 
20 	for (;;) {
21 		if ((fd = getfile("ls", 0)) == -1)
22 			exit();
23 		ip = &iob[fd - 3].i_ino;
24 		if ((ip->di_mode & IFMT) != IFDIR) {
25 			printf("ls: not a directory\n");
26 			continue;
27 		}
28 		if (ip->di_size == 0) {
29 			printf("ls: zero length directory\n");
30 			continue;
31 		}
32 		ls(fd);
33 	}
34 }
35 
36 #define CTRL(x)	(x&037)
37 
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);
52 }
53 
54 typedef struct direct	DP;
55 static
56 ls(fd)
57 	register int fd;
58 {
59 	register int size;
60 	register char *dp;
61 	char dirbuf[DIRBLKSIZ];
62 
63 	printf("\ninode\tname\n");
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;
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);
76 		}
77 }
78