xref: /original-bsd/sys/stand.att/ls.c (revision e4dd4c49)
1 /*
2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)ls.c	7.8 (Berkeley) 04/04/90
18  */
19 
20 #include "sys/param.h"
21 #include "ufs/dir.h"
22 #include "saio.h"
23 #include "sys/ttychars.h"
24 
25 main()
26 {
27 	struct dinode *ip;
28 	int fd;
29 
30 	for (;;) {
31 		if ((fd = getfile("ls", 0)) == -1)
32 			exit();
33 		ip = &iob[fd - 3].i_ino;
34 		if ((ip->di_mode & IFMT) != IFDIR) {
35 			printf("ls: not a directory\n");
36 			continue;
37 		}
38 		if (ip->di_size == 0) {
39 			printf("ls: zero length directory\n");
40 			continue;
41 		}
42 		ls(fd);
43 	}
44 }
45 
46 #define CTRL(x)	(x&037)
47 
48 getfile(prompt, mode)
49 	char *prompt;
50 	int mode;
51 {
52 	int fd;
53 	char buf[100];
54 
55 	do {
56 		printf("%s: ", prompt);
57 		gets(buf);
58 		if (buf[0] == CTRL('d') && buf[1] == 0)
59 			return (-1);
60 	} while ((fd = open(buf, mode)) <= 0);
61 	return(fd);
62 }
63 
64 typedef struct direct	DP;
65 static
66 ls(fd)
67 	register int fd;
68 {
69 	register int size;
70 	register char *dp;
71 	char dirbuf[DIRBLKSIZ];
72 
73 	printf("\ninode\tname\n");
74 	while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ)
75 		for(dp = dirbuf; (dp < (dirbuf + size)) &&
76 		    (dp + ((DP *)dp)->d_reclen) < (dirbuf + size);
77 		    dp += ((DP *)dp)->d_reclen) {
78 			if (((DP *)dp)->d_ino == 0)
79 				continue;
80 			if (((DP *)dp)->d_namlen > MAXNAMLEN+1) {
81 				printf("Corrupt file name length!  Run fsck soon!\n");
82 				return;
83 			}
84 			printf("%d\t%s\n", ((DP *)dp)->d_ino,
85 			    ((DP *)dp)->d_name);
86 		}
87 }
88