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