xv6-cs450/ls.c

51 lines
911 B
C
Raw Normal View History

2006-08-12 06:33:50 +02:00
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
char buf[512];
2006-08-14 05:00:13 +02:00
struct stat st;
2006-08-12 06:33:50 +02:00
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();
}
2006-08-14 05:00:13 +02:00
if (fstat(fd, &st) < 0) {
2006-08-12 06:33:50 +02:00
printf(2, "ls: cannot open .\n");
exit();
}
2006-08-14 05:00:13 +02:00
if (st.st_type != T_DIR) {
2006-08-12 06:33:50 +02:00
printf(2, "ls: . is not a dir\n");
}
2006-08-14 05:00:13 +02:00
cprintf("size %d\n", st.st_size);
for(off = 0; off < st.st_size; off += sizeof(struct dirent)) {
2006-08-12 06:33:50 +02:00
if (read(fd, &dirent, sizeof(struct dirent)) != sizeof(struct dirent)) {
printf(2, "ls: read error\n");
exit();
}
2006-08-14 05:00:13 +02:00
if (dirent.inum != 0) {
if (stat (dirent.name, &st) < 0)
printf(2, "stat: failed\n");
printf(1, "%s t %d\n", dirent.name, st.st_type);
}
2006-08-12 06:33:50 +02:00
}
close(fd);
exit();
}