i/o redirection in sh
better parsing of sh commands (copied from jos sh) cat: read from 1 if no args sbrk system call, but untested getpid system call moved locks in keyboard intr, but why do we get intr w. null characters from keyboard?
This commit is contained in:
parent
f18ab5c04e
commit
8b58e81077
15 changed files with 433 additions and 74 deletions
57
ls.c
57
ls.c
|
|
@ -7,13 +7,24 @@ char buf[512];
|
|||
struct stat st;
|
||||
struct dirent dirent;
|
||||
|
||||
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, " ");
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
uint off;
|
||||
uint sz;
|
||||
int i;
|
||||
|
||||
if(argc > 2){
|
||||
puts("Usage: ls [dir]\n");
|
||||
|
|
@ -23,7 +34,7 @@ main(int argc, char *argv[])
|
|||
if (argc == 2) {
|
||||
fd = open(argv[1], 0);
|
||||
if(fd < 0){
|
||||
printf(2, "ls: cannot open dir %s\n", argv[1]);
|
||||
printf(2, "ls: cannot open %s\n", argv[1]);
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
|
|
@ -38,31 +49,31 @@ main(int argc, char *argv[])
|
|||
printf(2, "ls: cannot stat dir\n");
|
||||
exit();
|
||||
}
|
||||
if (st.st_type != T_DIR) {
|
||||
printf(2, "ls: dir is not a directory\n");
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
for (i = 0; i < DIRSIZ; i++) {
|
||||
if (dirent.name[i] != '\0')
|
||||
printf(1, "%c", dirent.name[i]);
|
||||
else
|
||||
printf(1, " ");
|
||||
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);
|
||||
}
|
||||
printf(1, "%d %d %d\n", st.st_type, dirent.inum, st.st_size);
|
||||
}
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
exit();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue