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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)ls.c 7.3 (Berkeley) 05/24/88 13 */ 14 15 #include "param.h" 16 #include "inode.h" 17 #include "dir.h" 18 #include "fs.h" 19 #include "saio.h" 20 21 main() 22 { 23 struct inode *ip; 24 int fd; 25 26 fd = getfile("ls", 0); 27 ip = &iob[fd - 3].i_ino; 28 if ((ip->i_mode & IFMT) != IFDIR) 29 _stop("ls: not a directory"); 30 if (ip->i_size == 0) 31 _stop("ls: zero length directory"); 32 ls(fd); 33 } 34 35 typedef struct direct DP; 36 static 37 ls(fd) 38 register int fd; 39 { 40 register int size; 41 register char *dp; 42 char dirbuf[DIRBLKSIZ]; 43 44 printf("\nname->inode\n"); 45 while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) 46 for(dp = dirbuf; (dp < (dirbuf + size)) && 47 (dp + ((DP *)dp)->d_reclen) < (dirbuf + size); 48 dp += ((DP *)dp)->d_reclen) { 49 if (((DP *)dp)->d_ino == 0) 50 continue; 51 if (((DP *)dp)->d_reclen > DIRSIZ(((DP *)dp))) 52 continue; 53 if (((DP *)dp)->d_namlen > MAXNAMLEN+1) 54 _stop("Corrupt file name length! Run fsck soon!\n"); 55 printf("%s->%d\n", ((DP *)dp)->d_name, 56 ((DP *)dp)->d_ino); 57 } 58 } 59