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;
|
|
|
|
|
2006-08-23 03:09:24 +02:00
|
|
|
void
|
|
|
|
pname(char *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; (i < DIRSIZ) && (n[i] != '\0') ; i++) {
|
|
|
|
printf(1, "%c", n[i]);
|
|
|
|
}
|
|
|
|
for (; i < DIRSIZ; i++)
|
|
|
|
printf(1, " ");
|
|
|
|
}
|
|
|
|
|
2006-08-12 06:33:50 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
uint off;
|
2006-08-14 16:13:52 +02:00
|
|
|
uint sz;
|
2006-08-12 06:33:50 +02:00
|
|
|
|
2006-08-20 01:41:34 +02:00
|
|
|
if(argc > 2){
|
|
|
|
puts("Usage: ls [dir]\n");
|
2006-08-12 06:33:50 +02:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2006-08-20 01:41:34 +02:00
|
|
|
if (argc == 2) {
|
|
|
|
fd = open(argv[1], 0);
|
|
|
|
if(fd < 0){
|
2006-08-23 03:09:24 +02:00
|
|
|
printf(2, "ls: cannot open %s\n", argv[1]);
|
2006-08-20 01:41:34 +02:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd = open(".", 0);
|
|
|
|
if(fd < 0){
|
|
|
|
printf(2, "ls: cannot open .\n");
|
|
|
|
exit();
|
|
|
|
}
|
2006-08-12 06:33:50 +02:00
|
|
|
}
|
2006-08-20 01:41:34 +02:00
|
|
|
|
2006-08-14 05:00:13 +02:00
|
|
|
if (fstat(fd, &st) < 0) {
|
2006-08-20 01:41:34 +02:00
|
|
|
printf(2, "ls: cannot stat dir\n");
|
2006-08-12 06:33:50 +02:00
|
|
|
exit();
|
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
|
|
|
|
switch (st.st_type) {
|
|
|
|
case T_FILE:
|
|
|
|
pname(argv[1]);
|
|
|
|
printf(1, "%d %d %d\n", st.st_type, st.st_ino, st.st_size);
|
|
|
|
break;
|
|
|
|
case T_DIR:
|
|
|
|
sz = st.st_size;
|
|
|
|
for(off = 0; off < sz; off += sizeof(struct dirent)) {
|
|
|
|
if (read(fd, &dirent, sizeof(struct dirent)) != sizeof(struct dirent)) {
|
|
|
|
printf(1, "ls: read error\n");
|
|
|
|
break;
|
2006-08-14 16:13:52 +02:00
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
if (dirent.inum != 0) {
|
|
|
|
// xxx prepend to name the pathname supplied to ls (e.g. .. in ls ..)
|
|
|
|
if (stat (dirent.name, &st) < 0) {
|
|
|
|
printf(1, "stat: failed %s\n", dirent.name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pname(dirent.name);
|
|
|
|
printf(1, "%d %d %d\n", st.st_type, dirent.inum, st.st_size);
|
2006-08-14 23:22:13 +02:00
|
|
|
}
|
2006-08-14 05:00:13 +02:00
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
break;
|
2006-08-12 06:33:50 +02:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
exit();
|
|
|
|
}
|