2006-08-12 04:33:50 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "stat.h"
|
|
|
|
#include "user.h"
|
|
|
|
#include "fs.h"
|
|
|
|
|
2006-08-23 01:09:24 +00:00
|
|
|
void
|
|
|
|
pname(char *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
for(i = 0; (i < DIRSIZ) && (n[i] != '\0') ; i++) {
|
2006-08-23 01:09:24 +00:00
|
|
|
printf(1, "%c", n[i]);
|
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
for(; i < DIRSIZ; i++)
|
2006-08-23 01:09:24 +00:00
|
|
|
printf(1, " ");
|
|
|
|
}
|
|
|
|
|
2006-08-12 04:33:50 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2007-08-22 05:57:39 +00:00
|
|
|
char buf[512], *p;
|
2006-08-12 04:33:50 +00:00
|
|
|
int fd;
|
2007-08-22 05:57:39 +00:00
|
|
|
uint off, sz;
|
|
|
|
struct dirent de;
|
|
|
|
struct stat st;
|
2006-08-12 04:33:50 +00:00
|
|
|
|
2006-08-19 23:41:34 +00:00
|
|
|
if(argc > 2){
|
|
|
|
puts("Usage: ls [dir]\n");
|
2006-08-12 04:33:50 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
if(argc == 2) {
|
2006-08-19 23:41:34 +00:00
|
|
|
fd = open(argv[1], 0);
|
|
|
|
if(fd < 0){
|
2006-08-23 01:09:24 +00:00
|
|
|
printf(2, "ls: cannot open %s\n", argv[1]);
|
2006-08-19 23:41:34 +00:00
|
|
|
exit();
|
2006-09-06 17:27:19 +00:00
|
|
|
}
|
2006-08-19 23:41:34 +00:00
|
|
|
} else {
|
|
|
|
fd = open(".", 0);
|
|
|
|
if(fd < 0){
|
|
|
|
printf(2, "ls: cannot open .\n");
|
|
|
|
exit();
|
|
|
|
}
|
2006-08-12 04:33:50 +00:00
|
|
|
}
|
2006-08-19 23:41:34 +00:00
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
if(fstat(fd, &st) < 0) {
|
2006-08-19 23:41:34 +00:00
|
|
|
printf(2, "ls: cannot stat dir\n");
|
2006-08-12 04:33:50 +00:00
|
|
|
exit();
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2006-09-07 13:08:23 +00:00
|
|
|
switch(st.type) {
|
2006-08-23 01:09:24 +00:00
|
|
|
case T_FILE:
|
|
|
|
pname(argv[1]);
|
2006-09-07 13:08:23 +00:00
|
|
|
printf(1, "%d %d %d\n", st.type, st.ino, st.size);
|
2006-08-23 01:09:24 +00:00
|
|
|
break;
|
|
|
|
case T_DIR:
|
2006-09-07 13:08:23 +00:00
|
|
|
sz = st.size;
|
2007-08-22 05:57:39 +00:00
|
|
|
for(off = 0; off < sz; off += sizeof(de)) {
|
|
|
|
if(read(fd, &de, sizeof(de)) != sizeof(de)) {
|
2006-09-06 17:04:06 +00:00
|
|
|
printf(1, "ls: read error\n");
|
|
|
|
break;
|
2006-08-14 14:13:52 +00:00
|
|
|
}
|
2007-08-22 05:57:39 +00:00
|
|
|
if(de.inum != 0) {
|
2007-08-08 09:17:09 +00:00
|
|
|
p = buf;
|
|
|
|
if(argc == 2) {
|
|
|
|
strcpy(p, argv[1]);
|
|
|
|
p += strlen(p);
|
|
|
|
if(*(p-1) != '/')
|
|
|
|
*p++ = '/';
|
|
|
|
}
|
2007-08-22 05:57:39 +00:00
|
|
|
memmove(p, de.name, DIRSIZ);
|
|
|
|
p[DIRSIZ] = 0;
|
2007-08-08 09:17:09 +00:00
|
|
|
if(stat(buf, &st) < 0) {
|
2007-08-22 05:57:39 +00:00
|
|
|
printf(1, "stat: failed %s\n", de.name);
|
2006-09-06 17:04:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-22 05:57:39 +00:00
|
|
|
pname(de.name);
|
|
|
|
printf(1, "%d %d %d\n", st.type, de.inum, st.size);
|
2006-08-14 21:22:13 +00:00
|
|
|
}
|
2006-08-14 03:00:13 +00:00
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
break;
|
2006-08-12 04:33:50 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
exit();
|
|
|
|
}
|