xref: /original-bsd/sys/tahoe/stand/ls.c (revision 2301fdfb)
1 /*	ls.c	1.1	86/01/12	*/
2 /*	ls.c	6.1	83/07/29	*/
3 
4 #include "param.h"
5 #include "inode.h"
6 #include "dir.h"
7 #include "fs.h"
8 
9 #include "saio.h"
10 
11 char line[100];
12 
13 main()
14 {
15 	int io;
16 	register char	*ptr;
17 	struct inode	*ip;
18 
19 	for(;;) {
20 		line[0] = 0;
21 		printf("\n\nls: ");
22 		gets(line);
23 		/* scan to end of line */
24 		for(ptr=line; *ptr; ptr++)
25 			;
26 		/* check to see if a file was specified */
27 		if(ptr == line) {
28 			printf("usage: dev(unit,0)/directory\n");
29 			continue;
30 		}
31 		/* do one correction first so the raw dev is not opened */
32 		if(*(--ptr) == ')') {
33 			*(++ptr) = '/';
34 			*(++ptr) = '.';
35 			*(++ptr) = (char)0;
36 		}
37 		if(*ptr == '/') {
38 			*(++ptr) = '.';
39 			*(++ptr) = (char)0;
40 		}
41 		if((io = open(line, 0)) >= 0)
42 			break;
43 	}
44 	if((io >= NFILES+3) || (io < 3))
45 		_stop("open returned corrupt file index!");
46 	ip = &iob[io-3].i_ino;
47 	if ((ip->i_mode & IFMT) != IFDIR) {
48 		printf("%s is not a directory!\n", line);
49 		_stop("");
50 	}
51 	if (ip->i_size == 0) {
52 		printf("%s is a zero length directory!\n", line);
53 		_stop("");
54 	}
55 
56 	ls(io);
57 }
58 
59 ls(io)
60 register io;
61 {
62 
63 	register int	i, size;
64 	register char	*dp;
65 	static char	dirbuf[DIRBLKSIZ];
66 
67 	printf ("\nInode -> Name\n");
68 	while ((size = read(io, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) {
69 		for(dp = dirbuf; (dp < (dirbuf + size)) &&
70 		    (dp + ((struct direct *)dp)->d_reclen) < (dirbuf + size);
71 		    dp += ((struct direct *)dp)->d_reclen) {
72 			if(((struct direct *)dp)->d_ino == 0)
73 				continue;
74 			if(((struct direct *)dp)->d_reclen >
75 			    DIRSIZ(((struct direct *)dp)))
76 				continue;
77 			if(((struct direct *)dp)->d_namlen > MAXNAMLEN+1)
78 				_stop("Corrupt file name length!  Run fsck soon!\n");
79 			printf("%s -> %d\n", ((struct direct *)dp)->d_name,
80 			    ((struct direct *)dp)->d_ino);
81 		}
82 	}
83 }
84