This commit is contained in:
rsc 2007-08-22 05:57:39 +00:00
parent 6bf2e5c898
commit 3dcf889c1b

26
ls.c
View file

@ -3,10 +3,6 @@
#include "user.h" #include "user.h"
#include "fs.h" #include "fs.h"
char buf[512];
struct stat st;
struct dirent dirent;
void void
pname(char *n) pname(char *n)
{ {
@ -22,10 +18,11 @@ pname(char *n)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char buf[512], *p;
int fd; int fd;
uint off; uint off, sz;
uint sz; struct dirent de;
char *p; struct stat st;
if(argc > 2){ if(argc > 2){
puts("Usage: ls [dir]\n"); puts("Usage: ls [dir]\n");
@ -58,12 +55,12 @@ main(int argc, char *argv[])
break; break;
case T_DIR: case T_DIR:
sz = st.size; sz = st.size;
for(off = 0; off < sz; off += sizeof(struct dirent)) { for(off = 0; off < sz; off += sizeof(de)) {
if(read(fd, &dirent, sizeof dirent) != sizeof dirent) { if(read(fd, &de, sizeof(de)) != sizeof(de)) {
printf(1, "ls: read error\n"); printf(1, "ls: read error\n");
break; break;
} }
if(dirent.inum != 0) { if(de.inum != 0) {
p = buf; p = buf;
if(argc == 2) { if(argc == 2) {
strcpy(p, argv[1]); strcpy(p, argv[1]);
@ -71,13 +68,14 @@ main(int argc, char *argv[])
if(*(p-1) != '/') if(*(p-1) != '/')
*p++ = '/'; *p++ = '/';
} }
strcpy(p, dirent.name); memmove(p, de.name, DIRSIZ);
p[DIRSIZ] = 0;
if(stat(buf, &st) < 0) { if(stat(buf, &st) < 0) {
printf(1, "stat: failed %s\n", dirent.name); printf(1, "stat: failed %s\n", de.name);
continue; continue;
} }
pname(dirent.name); pname(de.name);
printf(1, "%d %d %d\n", st.type, dirent.inum, st.size); printf(1, "%d %d %d\n", st.type, de.inum, st.size);
} }
} }
break; break;