123231Smckusick /* 234462Sbostic * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 334462Sbostic * All rights reserved. 423231Smckusick * 5*44516Sbostic * %sccs.include.redist.c% 634462Sbostic * 7*44516Sbostic * @(#)ls.c 7.9 (Berkeley) 06/28/90 823231Smckusick */ 9320Sbill 1040501Sroot #include "sys/param.h" 1140501Sroot #include "ufs/dir.h" 12320Sbill #include "saio.h" 1340501Sroot #include "sys/ttychars.h" 14320Sbill 15320Sbill main() 16320Sbill { 1740502Smckusick struct dinode *ip; 1834462Sbostic int fd; 19320Sbill 2037222Skarels for (;;) { 2137222Skarels if ((fd = getfile("ls", 0)) == -1) 2237222Skarels exit(); 2337222Skarels ip = &iob[fd - 3].i_ino; 2440502Smckusick if ((ip->di_mode & IFMT) != IFDIR) { 2537222Skarels printf("ls: not a directory\n"); 2637222Skarels continue; 2737222Skarels } 2840502Smckusick if (ip->di_size == 0) { 2937222Skarels printf("ls: zero length directory\n"); 3037222Skarels continue; 3137222Skarels } 3237222Skarels ls(fd); 3337222Skarels } 34320Sbill } 35320Sbill 3640502Smckusick #define CTRL(x) (x&037) 3740502Smckusick 3837222Skarels getfile(prompt, mode) 3937222Skarels char *prompt; 4037222Skarels int mode; 4137222Skarels { 4237222Skarels int fd; 4337222Skarels char buf[100]; 4437222Skarels 4537222Skarels do { 4637222Skarels printf("%s: ", prompt); 4737222Skarels gets(buf); 4837222Skarels if (buf[0] == CTRL('d') && buf[1] == 0) 4937222Skarels return (-1); 5037222Skarels } while ((fd = open(buf, mode)) <= 0); 5137222Skarels return(fd); 5237222Skarels } 5337222Skarels 5434462Sbostic typedef struct direct DP; 5534462Sbostic static 5634462Sbostic ls(fd) 5734462Sbostic register int fd; 58320Sbill { 5934462Sbostic register int size; 6034462Sbostic register char *dp; 6134462Sbostic char dirbuf[DIRBLKSIZ]; 62320Sbill 6337222Skarels printf("\ninode\tname\n"); 6434462Sbostic while ((size = read(fd, dirbuf, DIRBLKSIZ)) == DIRBLKSIZ) 6534462Sbostic for(dp = dirbuf; (dp < (dirbuf + size)) && 6634462Sbostic (dp + ((DP *)dp)->d_reclen) < (dirbuf + size); 6734462Sbostic dp += ((DP *)dp)->d_reclen) { 6834462Sbostic if (((DP *)dp)->d_ino == 0) 6934462Sbostic continue; 7037222Skarels if (((DP *)dp)->d_namlen > MAXNAMLEN+1) { 7137222Skarels printf("Corrupt file name length! Run fsck soon!\n"); 7237222Skarels return; 7337222Skarels } 7437222Skarels printf("%d\t%s\n", ((DP *)dp)->d_ino, 7537222Skarels ((DP *)dp)->d_name); 76320Sbill } 77320Sbill } 78