diff options
author | kaashoek <kaashoek> | 2006-08-12 04:33:50 +0000 |
---|---|---|
committer | kaashoek <kaashoek> | 2006-08-12 04:33:50 +0000 |
commit | 1f544842ceb5af73b1f2b13222d72dd4ad7cd08a (patch) | |
tree | 49ad1096c0ad43c591c493166ff752a3e7cfd7cd /ls.c | |
parent | 0633b9715e106ac97fafcf3a68c06da1f0cf873a (diff) | |
download | xv6-labs-1f544842ceb5af73b1f2b13222d72dd4ad7cd08a.tar.gz xv6-labs-1f544842ceb5af73b1f2b13222d72dd4ad7cd08a.tar.bz2 xv6-labs-1f544842ceb5af73b1f2b13222d72dd4ad7cd08a.zip |
fstat
primitive ls
Diffstat (limited to 'ls.c')
-rw-r--r-- | ls.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -0,0 +1,43 @@ +#include "types.h" +#include "stat.h" +#include "user.h" +#include "fs.h" + +char buf[512]; +struct stat stat; +struct dirent dirent; + +int +main(int argc, char *argv[]) +{ + int fd; + uint off; + + if(argc > 1){ + puts("Usage: ls\n"); + exit(); + } + + fd = open(".", 0); + if(fd < 0){ + printf(2, "ls: cannot open .\n"); + exit(); + } + if (fstat(fd, &stat) < 0) { + printf(2, "ls: cannot open .\n"); + exit(); + } + if (stat.st_type != T_DIR) { + printf(2, "ls: . is not a dir\n"); + } + for(off = 0; off < stat.st_size; off += sizeof(struct dirent)) { + if (read(fd, &dirent, sizeof(struct dirent)) != sizeof(struct dirent)) { + printf(2, "ls: read error\n"); + exit(); + } + printf(1, "%s\n", dirent.name); + } + close(fd); + + exit(); +} |