xref: /csrg-svn/sys/stand.att/ls.c (revision 40502)
123231Smckusick /*
234462Sbostic  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
334462Sbostic  * All rights reserved.
423231Smckusick  *
534462Sbostic  * Redistribution and use in source and binary forms are permitted
634871Sbostic  * provided that the above copyright notice and this paragraph are
734871Sbostic  * duplicated in all such forms and that any documentation,
834871Sbostic  * advertising materials, and other materials related to such
934871Sbostic  * distribution and use acknowledge that the software was developed
1034871Sbostic  * by the University of California, Berkeley.  The name of the
1134871Sbostic  * University may not be used to endorse or promote products derived
1234871Sbostic  * from this software without specific prior written permission.
1334871Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434871Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534871Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634462Sbostic  *
17*40502Smckusick  *	@(#)ls.c	7.7 (Berkeley) 03/15/90
1823231Smckusick  */
19320Sbill 
2040501Sroot #include "sys/param.h"
21*40502Smckusick #include "ufs/dinode.h"
2240501Sroot #include "ufs/fs.h"
2340501Sroot #include "ufs/dir.h"
24320Sbill #include "saio.h"
2540501Sroot #include "sys/ttychars.h"
26320Sbill 
27320Sbill main()
28320Sbill {
29*40502Smckusick 	struct dinode *ip;
3034462Sbostic 	int fd;
31320Sbill 
3237222Skarels 	for (;;) {
3337222Skarels 		if ((fd = getfile("ls", 0)) == -1)
3437222Skarels 			exit();
3537222Skarels 		ip = &iob[fd - 3].i_ino;
36*40502Smckusick 		if ((ip->di_mode & IFMT) != IFDIR) {
3737222Skarels 			printf("ls: not a directory\n");
3837222Skarels 			continue;
3937222Skarels 		}
40*40502Smckusick 		if (ip->di_size == 0) {
4137222Skarels 			printf("ls: zero length directory\n");
4237222Skarels 			continue;
4337222Skarels 		}
4437222Skarels 		ls(fd);
4537222Skarels 	}
46320Sbill }
47320Sbill 
48*40502Smckusick #define CTRL(x)	(x&037)
49*40502Smckusick 
5037222Skarels getfile(prompt, mode)
5137222Skarels 	char *prompt;
5237222Skarels 	int mode;
5337222Skarels {
5437222Skarels 	int fd;
5537222Skarels 	char buf[100];
5637222Skarels 
5737222Skarels 	do {
5837222Skarels 		printf("%s: ", prompt);
5937222Skarels 		gets(buf);
6037222Skarels 		if (buf[0] == CTRL('d') && buf[1] == 0)
6137222Skarels 			return (-1);
6237222Skarels 	} while ((fd = open(buf, mode)) <= 0);
6337222Skarels 	return(fd);
6437222Skarels }
6537222Skarels 
6634462Sbostic typedef struct direct	DP;
6734462Sbostic static
6834462Sbostic ls(fd)
6934462Sbostic 	register int fd;
70320Sbill {
7134462Sbostic 	register int size;
7234462Sbostic 	register char *dp;
7334462Sbostic 	char dirbuf[DIRBLKSIZ];
74320Sbill 
7537222Skarels 	printf("\ninode\tname\n");
7634462Sbostic 	while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ)
7734462Sbostic 		for(dp = dirbuf; (dp < (dirbuf + size)) &&
7834462Sbostic 		    (dp + ((DP *)dp)->d_reclen) < (dirbuf + size);
7934462Sbostic 		    dp += ((DP *)dp)->d_reclen) {
8034462Sbostic 			if (((DP *)dp)->d_ino == 0)
8134462Sbostic 				continue;
8237222Skarels 			if (((DP *)dp)->d_namlen > MAXNAMLEN+1) {
8337222Skarels 				printf("Corrupt file name length!  Run fsck soon!\n");
8437222Skarels 				return;
8537222Skarels 			}
8637222Skarels 			printf("%d\t%s\n", ((DP *)dp)->d_ino,
8737222Skarels 			    ((DP *)dp)->d_name);
88320Sbill 		}
89320Sbill }
90