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