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)
$(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
cc -o mkfs mkfs.c
fs.img : mkfs userfs usertests echo cat README init sh ls
./mkfs fs.img 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 mkdir rm
-include *.d

10
ls.c
View File

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

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 "stat.h"
#include "user.h"
static void
putc(int fd, char c)
@ -63,6 +64,9 @@ printf(int fd, char *fmt, ...)
putc(fd, *s);
s++;
}
} else if(c == 'c'){
putc(fd, *ap);
ap++;
} else if(c == '%'){
putc(fd, c);
} 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 "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
char *args[100];
void parse(char buf[]);
int
main(void)
@ -18,8 +20,7 @@ main(void)
continue;
pid = fork();
if(pid == 0){
args[0] = buf;
args[1] = 0;
parse(buf);
exec(buf, args);
printf(1, "%s: not found\n", buf);
exit();
@ -28,3 +29,21 @@ main(void)
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 name[3];
char *echo_args[] = { "echo", "hello", "goodbye", 0 };
char *cat_args[] = { "cat", "README", 0 };
char *cat_args[] = { "cat", "readme", 0 };
int
main(void)