2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/fs.h"
|
2006-08-12 04:33:50 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
char*
|
|
|
|
fmtname(char *path)
|
2006-08-23 01:09:24 +00:00
|
|
|
{
|
2007-08-24 20:03:40 +00:00
|
|
|
static char buf[DIRSIZ+1];
|
|
|
|
char *p;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
// Find first character after last slash.
|
|
|
|
for(p=path+strlen(path); p >= path && *p != '/'; p--)
|
|
|
|
;
|
|
|
|
p++;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
// Return blank-padded name.
|
|
|
|
if(strlen(p) >= DIRSIZ)
|
|
|
|
return p;
|
|
|
|
memmove(buf, p, strlen(p));
|
|
|
|
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
|
|
|
|
return buf;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
void
|
|
|
|
ls(char *path)
|
2006-08-12 04:33:50 +00:00
|
|
|
{
|
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
|
|
|
struct dirent de;
|
|
|
|
struct stat st;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
if((fd = open(path, 0)) < 0){
|
|
|
|
printf(2, "ls: cannot open %s\n", path);
|
|
|
|
return;
|
2006-08-12 04:33:50 +00:00
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
if(fstat(fd, &st) < 0){
|
|
|
|
printf(2, "ls: cannot stat %s\n", path);
|
|
|
|
close(fd);
|
|
|
|
return;
|
2006-08-12 04:33:50 +00:00
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
switch(st.type){
|
2006-08-23 01:09:24 +00:00
|
|
|
case T_FILE:
|
2019-07-04 12:54:23 +00:00
|
|
|
printf(1, "%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
|
2006-08-23 01:09:24 +00:00
|
|
|
break;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2006-08-23 01:09:24 +00:00
|
|
|
case T_DIR:
|
2007-08-24 20:03:40 +00:00
|
|
|
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
|
|
|
|
printf(1, "ls: path too long\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
strcpy(buf, path);
|
|
|
|
p = buf+strlen(buf);
|
|
|
|
*p++ = '/';
|
|
|
|
while(read(fd, &de, sizeof(de)) == sizeof(de)){
|
|
|
|
if(de.inum == 0)
|
|
|
|
continue;
|
|
|
|
memmove(p, de.name, DIRSIZ);
|
|
|
|
p[DIRSIZ] = 0;
|
|
|
|
if(stat(buf, &st) < 0){
|
|
|
|
printf(1, "ls: cannot stat %s\n", buf);
|
|
|
|
continue;
|
2006-08-14 21:22:13 +00:00
|
|
|
}
|
2007-08-24 20:03:40 +00:00
|
|
|
printf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
|
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);
|
2007-08-24 20:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(argc < 2){
|
|
|
|
ls(".");
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
for(i=1; i<argc; i++)
|
|
|
|
ls(argv[i]);
|
2006-08-12 04:33:50 +00:00
|
|
|
exit();
|
|
|
|
}
|