user-level programs: mkdir and rm

shell parses arguments (very simplistic)
readme version of README (sh doesn't deal with capital characters)
printf recognizes %c
nicer output format for ls
This commit is contained in:
kaashoek 2006-08-14 21:22:13 +00:00
parent bdb6643303
commit d7b3b802f4
8 changed files with 91 additions and 9 deletions

View file

@ -91,11 +91,19 @@ ls : ls.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o ls ls.o $(ULIB) $(LD) -N -e main -Ttext 0 -o ls ls.o $(ULIB)
$(OBJDUMP) -S ls > ls.asm $(OBJDUMP) -S ls > ls.asm
mkdir : mkdir.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o mkdir mkdir.o $(ULIB)
$(OBJDUMP) -S mkdir > mkdir.asm
rm : rm.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o rm rm.o $(ULIB)
$(OBJDUMP) -S rm > rm.asm
mkfs : mkfs.c fs.h mkfs : mkfs.c fs.h
cc -o mkfs mkfs.c cc -o mkfs mkfs.c
fs.img : mkfs userfs usertests echo cat README init sh ls fs.img : mkfs userfs usertests echo cat readme init sh ls mkdir rm
./mkfs fs.img userfs usertests echo cat README init sh ls ./mkfs fs.img userfs usertests echo cat readme init sh ls mkdir rm
-include *.d -include *.d

10
ls.c
View file

@ -13,6 +13,7 @@ main(int argc, char *argv[])
int fd; int fd;
uint off; uint off;
uint sz; uint sz;
int i;
if(argc > 1){ if(argc > 1){
puts("Usage: ls\n"); puts("Usage: ls\n");
@ -42,8 +43,13 @@ main(int argc, char *argv[])
printf(1, "stat: failed\n"); printf(1, "stat: failed\n");
break; break;
} }
printf(1, "%s t %d ino %d sz %d\n", dirent.name, st.st_type, for (i = 0; i < DIRSIZ; i++) {
dirent.inum, st.st_size); if (dirent.name[i] != '\0')
printf(1, "%c", dirent.name[i]);
else
printf(1, " ");
}
printf(1, "%d %d %d\n", st.st_type, dirent.inum, st.st_size);
} }
} }
close(fd); close(fd);

22
mkdir.c Normal file
View file

@ -0,0 +1,22 @@
#include "types.h"
#include "stat.h"
#include "user.h"
main(int argc, char *argv[])
{
int i;
if(argc < 2){
printf(2, "Usage: mkdir files...\n");
exit();
}
for(i = 1; i < argc; i++){
if (mkdir(argv[i]) < 0) {
printf(2, "mkdir: %s failed to create\n", argv[i]);
break;
}
}
exit();
}

View file

@ -1,5 +1,6 @@
#include "user.h"
#include "types.h" #include "types.h"
#include "stat.h"
#include "user.h"
static void static void
putc(int fd, char c) putc(int fd, char c)
@ -63,6 +64,9 @@ printf(int fd, char *fmt, ...)
putc(fd, *s); putc(fd, *s);
s++; s++;
} }
} else if(c == 'c'){
putc(fd, *ap);
ap++;
} else if(c == '%'){ } else if(c == '%'){
putc(fd, c); putc(fd, c);
} else { } else {

1
readme Normal file
View file

@ -0,0 +1 @@
This is the content of file README.

22
rm.c Normal file
View file

@ -0,0 +1,22 @@
#include "types.h"
#include "stat.h"
#include "user.h"
main(int argc, char *argv[])
{
int i;
if(argc < 2){
printf(2, "Usage: rm files...\n");
exit();
}
for(i = 1; i < argc; i++){
if (unlink(argv[i]) < 0) {
printf(2, "mkdir: %s failed to create\n", argv[i]);
break;
}
}
exit();
}

25
sh.c
View file

@ -1,9 +1,11 @@
#include "user.h"
#include "types.h" #include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h" #include "fs.h"
#include "fcntl.h" #include "fcntl.h"
char *args[100]; char *args[100];
void parse(char buf[]);
int int
main(void) main(void)
@ -18,8 +20,7 @@ main(void)
continue; continue;
pid = fork(); pid = fork();
if(pid == 0){ if(pid == 0){
args[0] = buf; parse(buf);
args[1] = 0;
exec(buf, args); exec(buf, args);
printf(1, "%s: not found\n", buf); printf(1, "%s: not found\n", buf);
exit(); exit();
@ -28,3 +29,21 @@ main(void)
wait(); wait();
} }
} }
void
parse(char buf[])
{
int j = 1;
int i;
args[0] = buf;
for (i = 0; buf[i] != '\0'; i++) {
if (buf[i] == ' ') {
buf[i] = '\0';
args[j++] = buf + i+1;
if (j >= 100) {
printf(2, "too many args\n");
exit();
}
}
}
}

View file

@ -9,7 +9,7 @@
char buf[2000]; char buf[2000];
char name[3]; char name[3];
char *echo_args[] = { "echo", "hello", "goodbye", 0 }; char *echo_args[] = { "echo", "hello", "goodbye", 0 };
char *cat_args[] = { "cat", "README", 0 }; char *cat_args[] = { "cat", "readme", 0 };
int int
main(void) main(void)