xref: /netbsd-src/sys/stand/ls.c (revision 8fd1333afaff659b29489e220fc123a4fbb825cf)
1*8fd1333aSchristos /*	$NetBSD: ls.c,v 1.17 2011/09/27 01:08:55 christos Exp $	*/
21564ce55Sperry 
31564ce55Sperry /*-
41564ce55Sperry  * Copyright (c) 1993
51564ce55Sperry  *	The Regents of the University of California.  All rights reserved.
61564ce55Sperry  *
71564ce55Sperry  * Redistribution and use in source and binary forms, with or without
81564ce55Sperry  * modification, are permitted provided that the following conditions
91564ce55Sperry  * are met:
101564ce55Sperry  * 1. Redistributions of source code must retain the above copyright
111564ce55Sperry  *    notice, this list of conditions and the following disclaimer.
121564ce55Sperry  * 2. Redistributions in binary form must reproduce the above copyright
131564ce55Sperry  *    notice, this list of conditions and the following disclaimer in the
141564ce55Sperry  *    documentation and/or other materials provided with the distribution.
15aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
161564ce55Sperry  *    may be used to endorse or promote products derived from this software
171564ce55Sperry  *    without specific prior written permission.
181564ce55Sperry  *
191564ce55Sperry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201564ce55Sperry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211564ce55Sperry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221564ce55Sperry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231564ce55Sperry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241564ce55Sperry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251564ce55Sperry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261564ce55Sperry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271564ce55Sperry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281564ce55Sperry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291564ce55Sperry  * SUCH DAMAGE.
301564ce55Sperry  */
311564ce55Sperry 
321564ce55Sperry #include <sys/cdefs.h>
331564ce55Sperry #ifndef lint
341564ce55Sperry __COPYRIGHT("@(#) Copyright (c) 1993\n\
351564ce55Sperry 	The Regents of the University of California.  All rights reserved.\n");
361564ce55Sperry #endif /* not lint */
371564ce55Sperry 
381564ce55Sperry #ifndef lint
391564ce55Sperry #if 0
401564ce55Sperry static char sccsid[] = "@(#)ls.c	8.1 (Berkeley) 6/11/93";
411564ce55Sperry #else
42*8fd1333aSchristos __RCSID("$NetBSD: ls.c,v 1.17 2011/09/27 01:08:55 christos Exp $");
431564ce55Sperry #endif
441564ce55Sperry #endif /* not lint */
451564ce55Sperry 
461564ce55Sperry #include <sys/param.h>
471564ce55Sperry #include <sys/stat.h>
481564ce55Sperry #include <sys/dirent.h>
491564ce55Sperry #include <ufs/ufs/dir.h>
501564ce55Sperry #include <ufs/ufs/dinode.h>
511564ce55Sperry #include <sys/ttychars.h>
521564ce55Sperry #include <lib/libsa/stand.h>
531564ce55Sperry 
5402cdf4d2Sdsl int main(void);
5502cdf4d2Sdsl static void ls(int);
561564ce55Sperry 
571564ce55Sperry int
main(void)58df7f595eScegger main(void)
591564ce55Sperry {
601564ce55Sperry 	struct stat st;
611564ce55Sperry 	int fd;
621564ce55Sperry 
631564ce55Sperry 	for (;;) {
641564ce55Sperry 		if ((fd = getfile("ls", 0)) == -1)
651564ce55Sperry 			exit();
661564ce55Sperry 
671564ce55Sperry 		if (fstat(fd, &st) == -1) {
681564ce55Sperry 			printf("ls: cannot stat\n");
691564ce55Sperry 			continue;
701564ce55Sperry 		}
711564ce55Sperry 
721564ce55Sperry 		if (!S_ISDIR(st.st_mode)) {
73ef8adf0aSwiz 			printf("ls: not a directory\n");
741564ce55Sperry 			continue;
751564ce55Sperry 		}
761564ce55Sperry 		if (st.st_size == 0) {
77ef8adf0aSwiz 			printf("ls: zero length directory\n");
781564ce55Sperry 			continue;
791564ce55Sperry 		}
801564ce55Sperry 		ls(fd);
811564ce55Sperry 	}
821564ce55Sperry }
831564ce55Sperry 
841564ce55Sperry typedef struct dirent	DP;
851564ce55Sperry static void
ls(register int fd)86454af1c0Sdsl ls(register int fd)
871564ce55Sperry {
881564ce55Sperry 	register int size;
891564ce55Sperry 	register char *dp;
901564ce55Sperry 	char dirbuf[DIRBLKSIZ];
911564ce55Sperry 
921564ce55Sperry 	printf("\ninode\tname\n");
931564ce55Sperry 	while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ)
941564ce55Sperry 		for (dp = dirbuf; (dp < (dirbuf + size)) &&
951564ce55Sperry 		    (dp + ((DP *)dp)->d_reclen) < (dirbuf + size);
961564ce55Sperry 		    dp += ((DP *)dp)->d_reclen) {
971564ce55Sperry 			if (((DP *)dp)->d_fileno == 0)
981564ce55Sperry 				continue;
99*8fd1333aSchristos 			if (((DP *)dp)->d_namlen > NAME_MAX + 1) {
1001564ce55Sperry 				printf("Corrupt file name length!  Run fsck soon!\n");
1011564ce55Sperry 				return;
1021564ce55Sperry 			}
1031564ce55Sperry 			printf("%d\t%s\n", ((DP *)dp)->d_fileno,
1041564ce55Sperry 			    ((DP *)dp)->d_name);
1051564ce55Sperry 		}
1061564ce55Sperry }
107