cleanup
This commit is contained in:
parent
6bf2e5c898
commit
3dcf889c1b
26
ls.c
26
ls.c
|
@ -3,10 +3,6 @@
|
|||
#include "user.h"
|
||||
#include "fs.h"
|
||||
|
||||
char buf[512];
|
||||
struct stat st;
|
||||
struct dirent dirent;
|
||||
|
||||
void
|
||||
pname(char *n)
|
||||
{
|
||||
|
@ -22,10 +18,11 @@ pname(char *n)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char buf[512], *p;
|
||||
int fd;
|
||||
uint off;
|
||||
uint sz;
|
||||
char *p;
|
||||
uint off, sz;
|
||||
struct dirent de;
|
||||
struct stat st;
|
||||
|
||||
if(argc > 2){
|
||||
puts("Usage: ls [dir]\n");
|
||||
|
@ -58,12 +55,12 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
case T_DIR:
|
||||
sz = st.size;
|
||||
for(off = 0; off < sz; off += sizeof(struct dirent)) {
|
||||
if(read(fd, &dirent, sizeof dirent) != sizeof dirent) {
|
||||
for(off = 0; off < sz; off += sizeof(de)) {
|
||||
if(read(fd, &de, sizeof(de)) != sizeof(de)) {
|
||||
printf(1, "ls: read error\n");
|
||||
break;
|
||||
}
|
||||
if(dirent.inum != 0) {
|
||||
if(de.inum != 0) {
|
||||
p = buf;
|
||||
if(argc == 2) {
|
||||
strcpy(p, argv[1]);
|
||||
|
@ -71,13 +68,14 @@ main(int argc, char *argv[])
|
|||
if(*(p-1) != '/')
|
||||
*p++ = '/';
|
||||
}
|
||||
strcpy(p, dirent.name);
|
||||
memmove(p, de.name, DIRSIZ);
|
||||
p[DIRSIZ] = 0;
|
||||
if(stat(buf, &st) < 0) {
|
||||
printf(1, "stat: failed %s\n", dirent.name);
|
||||
printf(1, "stat: failed %s\n", de.name);
|
||||
continue;
|
||||
}
|
||||
pname(dirent.name);
|
||||
printf(1, "%d %d %d\n", st.type, dirent.inum, st.size);
|
||||
pname(de.name);
|
||||
printf(1, "%d %d %d\n", st.type, de.inum, st.size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Reference in a new issue