xref: /original-bsd/sys/stand.att/ls.c (revision 3e556167)
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.6 (Berkeley) 03/15/90
18  */
19 
20 #include "sys/param.h"
21 #include "sys/time.h"
22 #include "sys/vnode.h"
23 #include "ufs/inode.h"
24 #include "ufs/fs.h"
25 #include "ufs/dir.h"
26 #include "saio.h"
27 #include "sys/ttychars.h"
28 
29 main()
30 {
31 	struct inode *ip;
32 	int fd;
33 
34 	for (;;) {
35 		if ((fd = getfile("ls", 0)) == -1)
36 			exit();
37 		ip = &iob[fd - 3].i_ino;
38 		if ((ip->i_mode & IFMT) != IFDIR) {
39 			printf("ls: not a directory\n");
40 			continue;
41 		}
42 		if (ip->i_size == 0) {
43 			printf("ls: zero length directory\n");
44 			continue;
45 		}
46 		ls(fd);
47 	}
48 }
49 
50 getfile(prompt, mode)
51 	char *prompt;
52 	int mode;
53 {
54 	int fd;
55 	char buf[100];
56 
57 	do {
58 		printf("%s: ", prompt);
59 		gets(buf);
60 		if (buf[0] == CTRL('d') && buf[1] == 0)
61 			return (-1);
62 	} while ((fd = open(buf, mode)) <= 0);
63 	return(fd);
64 }
65 
66 typedef struct direct	DP;
67 static
68 ls(fd)
69 	register int fd;
70 {
71 	register int size;
72 	register char *dp;
73 	char dirbuf[DIRBLKSIZ];
74 
75 	printf("\ninode\tname\n");
76 	while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ)
77 		for(dp = dirbuf; (dp < (dirbuf + size)) &&
78 		    (dp + ((DP *)dp)->d_reclen) < (dirbuf + size);
79 		    dp += ((DP *)dp)->d_reclen) {
80 			if (((DP *)dp)->d_ino == 0)
81 				continue;
82 			if (((DP *)dp)->d_namlen > MAXNAMLEN+1) {
83 				printf("Corrupt file name length!  Run fsck soon!\n");
84 				return;
85 			}
86 			printf("%d\t%s\n", ((DP *)dp)->d_ino,
87 			    ((DP *)dp)->d_name);
88 		}
89 }
90