123231Smckusick /* 234462Sbostic * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 334462Sbostic * All rights reserved. 423231Smckusick * 544516Sbostic * %sccs.include.redist.c% 634462Sbostic * 7*60328Smckusick * @(#)ls.c 7.11 (Berkeley) 05/24/93 823231Smckusick */ 9320Sbill 1056509Sbostic #include <sys/param.h> 1156509Sbostic #include <sys/ttychars.h> 12320Sbill 1356509Sbostic #include <ufs/ufs/dir.h> 14*60328Smckusick #include <stand.att/saio.h> 1556509Sbostic 16320Sbill main() 17320Sbill { 1840502Smckusick struct dinode *ip; 1934462Sbostic int fd; 20320Sbill 2137222Skarels for (;;) { 2237222Skarels if ((fd = getfile("ls", 0)) == -1) 2337222Skarels exit(); 2437222Skarels ip = &iob[fd - 3].i_ino; 2540502Smckusick if ((ip->di_mode & IFMT) != IFDIR) { 2637222Skarels printf("ls: not a directory\n"); 2737222Skarels continue; 2837222Skarels } 2940502Smckusick if (ip->di_size == 0) { 3037222Skarels printf("ls: zero length directory\n"); 3137222Skarels continue; 3237222Skarels } 3337222Skarels ls(fd); 3437222Skarels } 35320Sbill } 36320Sbill 3740502Smckusick #define CTRL(x) (x&037) 3840502Smckusick 3937222Skarels getfile(prompt, mode) 4037222Skarels char *prompt; 4137222Skarels int mode; 4237222Skarels { 4337222Skarels int fd; 4437222Skarels char buf[100]; 4537222Skarels 4637222Skarels do { 4737222Skarels printf("%s: ", prompt); 4837222Skarels gets(buf); 4937222Skarels if (buf[0] == CTRL('d') && buf[1] == 0) 5037222Skarels return (-1); 5137222Skarels } while ((fd = open(buf, mode)) <= 0); 5237222Skarels return(fd); 5337222Skarels } 5437222Skarels 5534462Sbostic typedef struct direct DP; 5634462Sbostic static 5734462Sbostic ls(fd) 5834462Sbostic register int fd; 59320Sbill { 6034462Sbostic register int size; 6134462Sbostic register char *dp; 6234462Sbostic char dirbuf[DIRBLKSIZ]; 63320Sbill 6437222Skarels printf("\ninode\tname\n"); 6534462Sbostic while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) 6634462Sbostic for(dp = dirbuf; (dp < (dirbuf + size)) && 6734462Sbostic (dp + ((DP *)dp)->d_reclen) < (dirbuf + size); 6834462Sbostic dp += ((DP *)dp)->d_reclen) { 6934462Sbostic if (((DP *)dp)->d_ino == 0) 7034462Sbostic continue; 7137222Skarels if (((DP *)dp)->d_namlen > MAXNAMLEN+1) { 7237222Skarels printf("Corrupt file name length! Run fsck soon!\n"); 7337222Skarels return; 7437222Skarels } 7537222Skarels printf("%d\t%s\n", ((DP *)dp)->d_ino, 7637222Skarels ((DP *)dp)->d_name); 77320Sbill } 78320Sbill } 79