00001 #include "types.h"
00002 #include "stat.h"
00003 #include "user.h"
00004 #include "fs.h"
00005
00006 char*
00007 fmtname(char *path)
00008 {
00009 static char buf[DIRSIZ+1];
00010 char *p;
00011
00012
00013 for(p=path+strlen(path); p >= path && *p != '/'; p--)
00014 ;
00015 p++;
00016
00017
00018 if(strlen(p) >= DIRSIZ)
00019 return p;
00020 memmove(buf, p, strlen(p));
00021 memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
00022 return buf;
00023 }
00024
00025 void
00026 ls(char *path)
00027 {
00028 char buf[512], *p;
00029 int fd;
00030 struct dirent de;
00031 struct stat st;
00032
00033 if((fd = open(path, 0)) < 0){
00034 printf(2, "ls: cannot open %s\n", path);
00035 return;
00036 }
00037
00038 if(fstat(fd, &st) < 0){
00039 printf(2, "ls: cannot stat %s\n", path);
00040 close(fd);
00041 return;
00042 }
00043
00044 switch(st.type){
00045 case T_FILE:
00046 printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size);
00047 break;
00048
00049 case T_DIR:
00050 if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
00051 printf(1, "ls: path too long\n");
00052 break;
00053 }
00054 strcpy(buf, path);
00055 p = buf+strlen(buf);
00056 *p++ = '/';
00057 while(read(fd, &de, sizeof(de)) == sizeof(de)){
00058 if(de.inum == 0)
00059 continue;
00060 memmove(p, de.name, DIRSIZ);
00061 p[DIRSIZ] = 0;
00062 if(stat(buf, &st) < 0){
00063 printf(1, "ls: cannot stat %s\n", buf);
00064 continue;
00065 }
00066 printf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
00067 }
00068 break;
00069 }
00070 close(fd);
00071 }
00072
00073 int
00074 main(int argc, char *argv[])
00075 {
00076 int i;
00077
00078 if(argc < 2){
00079 ls(".");
00080 exit();
00081 }
00082 for(i=1; i<argc; i++)
00083 ls(argv[i]);
00084 exit();
00085 }