Complete reformat
This commit is contained in:
parent
b1a34398e9
commit
d6793bf093
60 changed files with 1952 additions and 1995 deletions
10
user/cat.c
10
user/cat.c
|
@ -10,12 +10,12 @@ cat(int fd)
|
|||
int n;
|
||||
|
||||
while((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
if (write(1, buf, n) != n) {
|
||||
if(write(1, buf, n) != n) {
|
||||
fprintf(2, "cat: write error\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if(n < 0){
|
||||
if(n < 0) {
|
||||
fprintf(2, "cat: read error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -26,13 +26,13 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int fd, i;
|
||||
|
||||
if(argc <= 1){
|
||||
if(argc <= 1) {
|
||||
cat(0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
for(i = 1; i < argc; i++) {
|
||||
if((fd = open(argv[i], 0)) < 0) {
|
||||
fprintf(2, "cat: cannot open %s\n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
|
|
|
@ -7,9 +7,9 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int i;
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
for(i = 1; i < argc; i++) {
|
||||
write(1, argv[i], strlen(argv[i]));
|
||||
if(i + 1 < argc){
|
||||
if(i + 1 < argc) {
|
||||
write(1, " ", 1);
|
||||
} else {
|
||||
write(1, "\n", 1);
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
#define N 1000
|
||||
#define N 1000
|
||||
|
||||
void
|
||||
print(const char *s)
|
||||
|
@ -20,7 +20,7 @@ forktest(void)
|
|||
|
||||
print("fork test\n");
|
||||
|
||||
for(n=0; n<N; n++){
|
||||
for(n = 0; n < N; n++) {
|
||||
pid = fork();
|
||||
if(pid < 0)
|
||||
break;
|
||||
|
@ -28,19 +28,19 @@ forktest(void)
|
|||
exit(0);
|
||||
}
|
||||
|
||||
if(n == N){
|
||||
if(n == N) {
|
||||
print("fork claimed to work N times!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(; n > 0; n--){
|
||||
if(wait(0) < 0){
|
||||
for(; n > 0; n--) {
|
||||
if(wait(0) < 0) {
|
||||
print("wait stopped early\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(wait(0) != -1){
|
||||
if(wait(0) != -1) {
|
||||
print("wait got too many\n");
|
||||
exit(1);
|
||||
}
|
||||
|
|
53
user/grep.c
53
user/grep.c
|
@ -5,28 +5,28 @@
|
|||
#include "user/user.h"
|
||||
|
||||
char buf[1024];
|
||||
int match(char*, char*);
|
||||
int match(char *, char *);
|
||||
|
||||
void
|
||||
grep(char *pattern, int fd)
|
||||
{
|
||||
int n, m;
|
||||
int n, m;
|
||||
char *p, *q;
|
||||
|
||||
m = 0;
|
||||
while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){
|
||||
while((n = read(fd, buf + m, sizeof(buf) - m - 1)) > 0) {
|
||||
m += n;
|
||||
buf[m] = '\0';
|
||||
p = buf;
|
||||
while((q = strchr(p, '\n')) != 0){
|
||||
while((q = strchr(p, '\n')) != 0) {
|
||||
*q = 0;
|
||||
if(match(pattern, p)){
|
||||
if(match(pattern, p)) {
|
||||
*q = '\n';
|
||||
write(1, p, q+1 - p);
|
||||
write(1, p, q + 1 - p);
|
||||
}
|
||||
p = q+1;
|
||||
p = q + 1;
|
||||
}
|
||||
if(m > 0){
|
||||
if(m > 0) {
|
||||
m -= p - buf;
|
||||
memmove(buf, p, m);
|
||||
}
|
||||
|
@ -36,22 +36,22 @@ grep(char *pattern, int fd)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
int fd, i;
|
||||
char *pattern;
|
||||
|
||||
if(argc <= 1){
|
||||
if(argc <= 1) {
|
||||
fprintf(2, "usage: grep pattern [file ...]\n");
|
||||
exit(1);
|
||||
}
|
||||
pattern = argv[1];
|
||||
|
||||
if(argc <= 2){
|
||||
if(argc <= 2) {
|
||||
grep(pattern, 0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for(i = 2; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
for(i = 2; i < argc; i++) {
|
||||
if((fd = open(argv[i], 0)) < 0) {
|
||||
printf("grep: cannot open %s\n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -65,42 +65,43 @@ main(int argc, char *argv[])
|
|||
// The Practice of Programming, Chapter 9, or
|
||||
// https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
|
||||
|
||||
int matchhere(char*, char*);
|
||||
int matchstar(int, char*, char*);
|
||||
int matchhere(char *, char *);
|
||||
int matchstar(int, char *, char *);
|
||||
|
||||
int
|
||||
match(char *re, char *text)
|
||||
{
|
||||
if(re[0] == '^')
|
||||
return matchhere(re+1, text);
|
||||
do{ // must look at empty string
|
||||
return matchhere(re + 1, text);
|
||||
do { // must look at empty string
|
||||
if(matchhere(re, text))
|
||||
return 1;
|
||||
}while(*text++ != '\0');
|
||||
} while(*text++ != '\0');
|
||||
return 0;
|
||||
}
|
||||
|
||||
// matchhere: search for re at beginning of text
|
||||
int matchhere(char *re, char *text)
|
||||
int
|
||||
matchhere(char *re, char *text)
|
||||
{
|
||||
if(re[0] == '\0')
|
||||
return 1;
|
||||
if(re[1] == '*')
|
||||
return matchstar(re[0], re+2, text);
|
||||
return matchstar(re[0], re + 2, text);
|
||||
if(re[0] == '$' && re[1] == '\0')
|
||||
return *text == '\0';
|
||||
if(*text!='\0' && (re[0]=='.' || re[0]==*text))
|
||||
return matchhere(re+1, text+1);
|
||||
if(*text != '\0' && (re[0] == '.' || re[0] == *text))
|
||||
return matchhere(re + 1, text + 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// matchstar: search for c*re at beginning of text
|
||||
int matchstar(int c, char *re, char *text)
|
||||
int
|
||||
matchstar(int c, char *re, char *text)
|
||||
{
|
||||
do{ // a * matches zero or more instances
|
||||
do { // a * matches zero or more instances
|
||||
if(matchhere(re, text))
|
||||
return 1;
|
||||
}while(*text!='\0' && (*text++==c || c=='.'));
|
||||
} while(*text != '\0' && (*text++ == c || c == '.'));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
202
user/grind.c
202
user/grind.c
|
@ -16,27 +16,27 @@
|
|||
int
|
||||
do_rand(unsigned long *ctx)
|
||||
{
|
||||
/*
|
||||
* Compute x = (7^5 * x) mod (2^31 - 1)
|
||||
* without overflowing 31 bits:
|
||||
* (2^31 - 1) = 127773 * (7^5) + 2836
|
||||
* From "Random number generators: good ones are hard to find",
|
||||
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
|
||||
* October 1988, p. 1195.
|
||||
*/
|
||||
long hi, lo, x;
|
||||
/*
|
||||
* Compute x = (7^5 * x) mod (2^31 - 1)
|
||||
* without overflowing 31 bits:
|
||||
* (2^31 - 1) = 127773 * (7^5) + 2836
|
||||
* From "Random number generators: good ones are hard to find",
|
||||
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
|
||||
* October 1988, p. 1195.
|
||||
*/
|
||||
long hi, lo, x;
|
||||
|
||||
/* Transform to [1, 0x7ffffffe] range. */
|
||||
x = (*ctx % 0x7ffffffe) + 1;
|
||||
hi = x / 127773;
|
||||
lo = x % 127773;
|
||||
x = 16807 * lo - 2836 * hi;
|
||||
if (x < 0)
|
||||
x += 0x7fffffff;
|
||||
/* Transform to [0, 0x7ffffffd] range. */
|
||||
x--;
|
||||
*ctx = x;
|
||||
return (x);
|
||||
/* Transform to [1, 0x7ffffffe] range. */
|
||||
x = (*ctx % 0x7ffffffe) + 1;
|
||||
hi = x / 127773;
|
||||
lo = x % 127773;
|
||||
x = 16807 * lo - 2836 * hi;
|
||||
if(x < 0)
|
||||
x += 0x7fffffff;
|
||||
/* Transform to [0, 0x7ffffffd] range. */
|
||||
x--;
|
||||
*ctx = x;
|
||||
return (x);
|
||||
}
|
||||
|
||||
unsigned long rand_next = 1;
|
||||
|
@ -44,124 +44,124 @@ unsigned long rand_next = 1;
|
|||
int
|
||||
rand(void)
|
||||
{
|
||||
return (do_rand(&rand_next));
|
||||
return (do_rand(&rand_next));
|
||||
}
|
||||
|
||||
void
|
||||
go(int which_child)
|
||||
{
|
||||
int fd = -1;
|
||||
int fd = -1;
|
||||
static char buf[999];
|
||||
char *break0 = sbrk(0);
|
||||
u64 iters = 0;
|
||||
char *break0 = sbrk(0);
|
||||
u64 iters = 0;
|
||||
|
||||
mkdir("grindir");
|
||||
if(chdir("grindir") != 0){
|
||||
if(chdir("grindir") != 0) {
|
||||
printf("grind: chdir grindir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
chdir("/");
|
||||
|
||||
while(1){
|
||||
|
||||
while(1) {
|
||||
iters++;
|
||||
if((iters % 500) == 0)
|
||||
write(1, which_child?"B":"A", 1);
|
||||
write(1, which_child ? "B" : "A", 1);
|
||||
int what = rand() % 23;
|
||||
if(what == 1){
|
||||
close(open("grindir/../a", O_CREATE|O_RDWR));
|
||||
} else if(what == 2){
|
||||
close(open("grindir/../grindir/../b", O_CREATE|O_RDWR));
|
||||
} else if(what == 3){
|
||||
if(what == 1) {
|
||||
close(open("grindir/../a", O_CREATE | O_RDWR));
|
||||
} else if(what == 2) {
|
||||
close(open("grindir/../grindir/../b", O_CREATE | O_RDWR));
|
||||
} else if(what == 3) {
|
||||
unlink("grindir/../a");
|
||||
} else if(what == 4){
|
||||
if(chdir("grindir") != 0){
|
||||
} else if(what == 4) {
|
||||
if(chdir("grindir") != 0) {
|
||||
printf("grind: chdir grindir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
unlink("../b");
|
||||
chdir("/");
|
||||
} else if(what == 5){
|
||||
} else if(what == 5) {
|
||||
close(fd);
|
||||
fd = open("/grindir/../a", O_CREATE|O_RDWR);
|
||||
} else if(what == 6){
|
||||
fd = open("/grindir/../a", O_CREATE | O_RDWR);
|
||||
} else if(what == 6) {
|
||||
close(fd);
|
||||
fd = open("/./grindir/./../b", O_CREATE|O_RDWR);
|
||||
} else if(what == 7){
|
||||
fd = open("/./grindir/./../b", O_CREATE | O_RDWR);
|
||||
} else if(what == 7) {
|
||||
write(fd, buf, sizeof(buf));
|
||||
} else if(what == 8){
|
||||
} else if(what == 8) {
|
||||
read(fd, buf, sizeof(buf));
|
||||
} else if(what == 9){
|
||||
} else if(what == 9) {
|
||||
mkdir("grindir/../a");
|
||||
close(open("a/../a/./a", O_CREATE|O_RDWR));
|
||||
close(open("a/../a/./a", O_CREATE | O_RDWR));
|
||||
unlink("a/a");
|
||||
} else if(what == 10){
|
||||
} else if(what == 10) {
|
||||
mkdir("/../b");
|
||||
close(open("grindir/../b/b", O_CREATE|O_RDWR));
|
||||
close(open("grindir/../b/b", O_CREATE | O_RDWR));
|
||||
unlink("b/b");
|
||||
} else if(what == 11){
|
||||
} else if(what == 11) {
|
||||
unlink("b");
|
||||
link("../grindir/./../a", "../b");
|
||||
} else if(what == 12){
|
||||
} else if(what == 12) {
|
||||
unlink("../grindir/../a");
|
||||
link(".././b", "/grindir/../a");
|
||||
} else if(what == 13){
|
||||
} else if(what == 13) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
wait(0);
|
||||
} else if(what == 14){
|
||||
} else if(what == 14) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
fork();
|
||||
fork();
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
wait(0);
|
||||
} else if(what == 15){
|
||||
} else if(what == 15) {
|
||||
sbrk(6011);
|
||||
} else if(what == 16){
|
||||
} else if(what == 16) {
|
||||
if(sbrk(0) > break0)
|
||||
sbrk(-(sbrk(0) - break0));
|
||||
} else if(what == 17){
|
||||
} else if(what == 17) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
close(open("a", O_CREATE|O_RDWR));
|
||||
if(pid == 0) {
|
||||
close(open("a", O_CREATE | O_RDWR));
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(chdir("../grindir/..") != 0){
|
||||
if(chdir("../grindir/..") != 0) {
|
||||
printf("grind: chdir failed\n");
|
||||
exit(1);
|
||||
}
|
||||
kill(pid);
|
||||
wait(0);
|
||||
} else if(what == 18){
|
||||
} else if(what == 18) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
kill(getpid());
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
wait(0);
|
||||
} else if(what == 19){
|
||||
} else if(what == 19) {
|
||||
int fds[2];
|
||||
if(pipe(fds) < 0){
|
||||
if(pipe(fds) < 0) {
|
||||
printf("grind: pipe failed\n");
|
||||
exit(1);
|
||||
}
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
fork();
|
||||
fork();
|
||||
if(write(fds[1], "x", 1) != 1)
|
||||
|
@ -170,74 +170,74 @@ go(int which_child)
|
|||
if(read(fds[0], &c, 1) != 1)
|
||||
printf("grind: pipe read failed\n");
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
wait(0);
|
||||
} else if(what == 20){
|
||||
} else if(what == 20) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
unlink("a");
|
||||
mkdir("a");
|
||||
chdir("a");
|
||||
unlink("../a");
|
||||
fd = open("x", O_CREATE|O_RDWR);
|
||||
fd = open("x", O_CREATE | O_RDWR);
|
||||
unlink("x");
|
||||
exit(0);
|
||||
} else if(pid < 0){
|
||||
} else if(pid < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
wait(0);
|
||||
} else if(what == 21){
|
||||
} else if(what == 21) {
|
||||
unlink("c");
|
||||
// should always succeed. check that there are free i-nodes,
|
||||
// file descriptors, blocks.
|
||||
int fd1 = open("c", O_CREATE|O_RDWR);
|
||||
if(fd1 < 0){
|
||||
int fd1 = open("c", O_CREATE | O_RDWR);
|
||||
if(fd1 < 0) {
|
||||
printf("grind: create c failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(write(fd1, "x", 1) != 1){
|
||||
if(write(fd1, "x", 1) != 1) {
|
||||
printf("grind: write c failed\n");
|
||||
exit(1);
|
||||
}
|
||||
struct stat st;
|
||||
if(fstat(fd1, &st) != 0){
|
||||
if(fstat(fd1, &st) != 0) {
|
||||
printf("grind: fstat failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(st.size != 1){
|
||||
if(st.size != 1) {
|
||||
printf("grind: fstat reports wrong size %d\n", (int)st.size);
|
||||
exit(1);
|
||||
}
|
||||
if(st.ino > 200){
|
||||
if(st.ino > 200) {
|
||||
printf("grind: fstat reports crazy i-number %d\n", st.ino);
|
||||
exit(1);
|
||||
}
|
||||
close(fd1);
|
||||
unlink("c");
|
||||
} else if(what == 22){
|
||||
} else if(what == 22) {
|
||||
// echo hi | cat
|
||||
int aa[2], bb[2];
|
||||
if(pipe(aa) < 0){
|
||||
if(pipe(aa) < 0) {
|
||||
fprintf(2, "grind: pipe failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pipe(bb) < 0){
|
||||
if(pipe(bb) < 0) {
|
||||
fprintf(2, "grind: pipe failed\n");
|
||||
exit(1);
|
||||
}
|
||||
int pid1 = fork();
|
||||
if(pid1 == 0){
|
||||
if(pid1 == 0) {
|
||||
close(bb[0]);
|
||||
close(bb[1]);
|
||||
close(aa[0]);
|
||||
close(1);
|
||||
if(dup(aa[1]) != 1){
|
||||
if(dup(aa[1]) != 1) {
|
||||
fprintf(2, "grind: dup failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -246,22 +246,22 @@ go(int which_child)
|
|||
exec("grindir/../echo", args);
|
||||
fprintf(2, "grind: echo: not found\n");
|
||||
exit(2);
|
||||
} else if(pid1 < 0){
|
||||
} else if(pid1 < 0) {
|
||||
fprintf(2, "grind: fork failed\n");
|
||||
exit(3);
|
||||
}
|
||||
int pid2 = fork();
|
||||
if(pid2 == 0){
|
||||
if(pid2 == 0) {
|
||||
close(aa[1]);
|
||||
close(bb[0]);
|
||||
close(0);
|
||||
if(dup(aa[0]) != 0){
|
||||
if(dup(aa[0]) != 0) {
|
||||
fprintf(2, "grind: dup failed\n");
|
||||
exit(4);
|
||||
}
|
||||
close(aa[0]);
|
||||
close(1);
|
||||
if(dup(bb[1]) != 1){
|
||||
if(dup(bb[1]) != 1) {
|
||||
fprintf(2, "grind: dup failed\n");
|
||||
exit(5);
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ go(int which_child)
|
|||
exec("/cat", args);
|
||||
fprintf(2, "grind: cat: not found\n");
|
||||
exit(6);
|
||||
} else if(pid2 < 0){
|
||||
} else if(pid2 < 0) {
|
||||
fprintf(2, "grind: fork failed\n");
|
||||
exit(7);
|
||||
}
|
||||
|
@ -278,14 +278,14 @@ go(int which_child)
|
|||
close(aa[1]);
|
||||
close(bb[1]);
|
||||
char buf[4] = { 0, 0, 0, 0 };
|
||||
read(bb[0], buf+0, 1);
|
||||
read(bb[0], buf+1, 1);
|
||||
read(bb[0], buf+2, 1);
|
||||
read(bb[0], buf + 0, 1);
|
||||
read(bb[0], buf + 1, 1);
|
||||
read(bb[0], buf + 2, 1);
|
||||
close(bb[0]);
|
||||
int st1, st2;
|
||||
wait(&st1);
|
||||
wait(&st2);
|
||||
if(st1 != 0 || st2 != 0 || strcmp(buf, "hi\n") != 0){
|
||||
if(st1 != 0 || st2 != 0 || strcmp(buf, "hi\n") != 0) {
|
||||
printf("grind: exec pipeline failed %d %d \"%s\"\n", st1, st2, buf);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -298,24 +298,24 @@ iter()
|
|||
{
|
||||
unlink("a");
|
||||
unlink("b");
|
||||
|
||||
|
||||
int pid1 = fork();
|
||||
if(pid1 < 0){
|
||||
if(pid1 < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pid1 == 0){
|
||||
if(pid1 == 0) {
|
||||
rand_next ^= 31;
|
||||
go(0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int pid2 = fork();
|
||||
if(pid2 < 0){
|
||||
if(pid2 < 0) {
|
||||
printf("grind: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pid2 == 0){
|
||||
if(pid2 == 0) {
|
||||
rand_next ^= 7177;
|
||||
go(1);
|
||||
exit(0);
|
||||
|
@ -323,7 +323,7 @@ iter()
|
|||
|
||||
int st1 = -1;
|
||||
wait(&st1);
|
||||
if(st1 != 0){
|
||||
if(st1 != 0) {
|
||||
kill(pid1);
|
||||
kill(pid2);
|
||||
}
|
||||
|
@ -336,13 +336,13 @@ iter()
|
|||
int
|
||||
main()
|
||||
{
|
||||
while(1){
|
||||
while(1) {
|
||||
int pid = fork();
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
iter();
|
||||
exit(0);
|
||||
}
|
||||
if(pid > 0){
|
||||
if(pid > 0) {
|
||||
wait(0);
|
||||
}
|
||||
sleep(20);
|
||||
|
|
20
user/init.c
20
user/init.c
|
@ -16,34 +16,34 @@ main(void)
|
|||
{
|
||||
int pid, wpid;
|
||||
|
||||
if(open("console", O_RDWR) < 0){
|
||||
if(open("console", O_RDWR) < 0) {
|
||||
mknod("console", CONSOLE, 0);
|
||||
open("console", O_RDWR);
|
||||
}
|
||||
dup(0); // stdout
|
||||
dup(0); // stderr
|
||||
dup(0); // stdout
|
||||
dup(0); // stderr
|
||||
|
||||
for(;;){
|
||||
for(;;) {
|
||||
printf("init: starting sh\n");
|
||||
pid = fork();
|
||||
if(pid < 0){
|
||||
if(pid < 0) {
|
||||
printf("init: fork failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if(pid == 0){
|
||||
if(pid == 0) {
|
||||
exec("sh", argv);
|
||||
printf("init: exec sh failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(;;){
|
||||
for(;;) {
|
||||
// this call to wait() returns if the shell exits,
|
||||
// or if a parentless process exits.
|
||||
wpid = wait((int *) 0);
|
||||
if(wpid == pid){
|
||||
wpid = wait((int *)0);
|
||||
if(wpid == pid) {
|
||||
// the shell exited; restart it.
|
||||
break;
|
||||
} else if(wpid < 0){
|
||||
} else if(wpid < 0) {
|
||||
printf("init: wait returned an error\n");
|
||||
exit(1);
|
||||
} else {
|
||||
|
|
|
@ -7,11 +7,11 @@ main(int argc, char **argv)
|
|||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
if(argc < 2) {
|
||||
fprintf(2, "usage: kill pid...\n");
|
||||
exit(1);
|
||||
}
|
||||
for(i=1; i<argc; i++)
|
||||
for(i = 1; i < argc; i++)
|
||||
kill(atoi(argv[i]));
|
||||
exit(0);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 3){
|
||||
if(argc != 3) {
|
||||
fprintf(2, "Usage: ln old new\n");
|
||||
exit(1);
|
||||
}
|
||||
|
|
34
user/ls.c
34
user/ls.c
|
@ -3,14 +3,14 @@
|
|||
#include "user/user.h"
|
||||
#include "kernel/fs.h"
|
||||
|
||||
char*
|
||||
char *
|
||||
fmtname(char *path)
|
||||
{
|
||||
static char buf[DIRSIZ+1];
|
||||
char *p;
|
||||
static char buf[DIRSIZ + 1];
|
||||
char *p;
|
||||
|
||||
// Find first character after last slash.
|
||||
for(p=path+strlen(path); p >= path && *p != '/'; p--)
|
||||
for(p = path + strlen(path); p >= path && *p != '/'; p--)
|
||||
;
|
||||
p++;
|
||||
|
||||
|
@ -18,49 +18,49 @@ fmtname(char *path)
|
|||
if(strlen(p) >= DIRSIZ)
|
||||
return p;
|
||||
memmove(buf, p, strlen(p));
|
||||
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
|
||||
memset(buf + strlen(p), ' ', DIRSIZ - strlen(p));
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
ls(char *path)
|
||||
{
|
||||
char buf[512], *p;
|
||||
int fd;
|
||||
char buf[512], *p;
|
||||
int fd;
|
||||
struct dirent de;
|
||||
struct stat st;
|
||||
struct stat st;
|
||||
|
||||
if((fd = open(path, 0)) < 0){
|
||||
if((fd = open(path, 0)) < 0) {
|
||||
fprintf(2, "ls: cannot open %s\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
if(fstat(fd, &st) < 0){
|
||||
if(fstat(fd, &st) < 0) {
|
||||
fprintf(2, "ls: cannot stat %s\n", path);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(st.type){
|
||||
switch(st.type) {
|
||||
case T_DEVICE:
|
||||
case T_FILE:
|
||||
printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
|
||||
break;
|
||||
|
||||
case T_DIR:
|
||||
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
|
||||
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf) {
|
||||
printf("ls: path too long\n");
|
||||
break;
|
||||
}
|
||||
strcpy(buf, path);
|
||||
p = buf+strlen(buf);
|
||||
p = buf + strlen(buf);
|
||||
*p++ = '/';
|
||||
while(read(fd, &de, sizeof(de)) == sizeof(de)){
|
||||
while(read(fd, &de, sizeof(de)) == sizeof(de)) {
|
||||
if(de.inum == 0)
|
||||
continue;
|
||||
memmove(p, de.name, DIRSIZ);
|
||||
p[DIRSIZ] = 0;
|
||||
if(stat(buf, &st) < 0){
|
||||
if(stat(buf, &st) < 0) {
|
||||
printf("ls: cannot stat %s\n", buf);
|
||||
continue;
|
||||
}
|
||||
|
@ -76,11 +76,11 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
if(argc < 2) {
|
||||
ls(".");
|
||||
exit(0);
|
||||
}
|
||||
for(i=1; i<argc; i++)
|
||||
for(i = 1; i < argc; i++)
|
||||
ls(argv[i]);
|
||||
exit(0);
|
||||
}
|
||||
|
|
|
@ -7,13 +7,13 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
if(argc < 2) {
|
||||
fprintf(2, "Usage: mkdir files...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if(mkdir(argv[i]) < 0){
|
||||
for(i = 1; i < argc; i++) {
|
||||
if(mkdir(argv[i]) < 0) {
|
||||
fprintf(2, "mkdir: %s failed to create\n", argv[i]);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ static void
|
|||
printint(int fd, int xx, int base, int sgn)
|
||||
{
|
||||
char buf[16];
|
||||
int i, neg;
|
||||
u32 x;
|
||||
int i, neg;
|
||||
u32 x;
|
||||
|
||||
neg = 0;
|
||||
if(sgn && xx < 0){
|
||||
if(sgn && xx < 0) {
|
||||
neg = 1;
|
||||
x = -xx;
|
||||
} else {
|
||||
|
@ -28,9 +28,9 @@ printint(int fd, int xx, int base, int sgn)
|
|||
}
|
||||
|
||||
i = 0;
|
||||
do{
|
||||
do {
|
||||
buf[i++] = digits[x % base];
|
||||
}while((x /= base) != 0);
|
||||
} while((x /= base) != 0);
|
||||
if(neg)
|
||||
buf[i++] = '-';
|
||||
|
||||
|
@ -39,11 +39,12 @@ printint(int fd, int xx, int base, int sgn)
|
|||
}
|
||||
|
||||
static void
|
||||
printptr(int fd, u64 x) {
|
||||
printptr(int fd, u64 x)
|
||||
{
|
||||
int i;
|
||||
putc(fd, '0');
|
||||
putc(fd, 'x');
|
||||
for (i = 0; i < (sizeof(u64) * 2); i++, x <<= 4)
|
||||
for(i = 0; i < (sizeof(u64) * 2); i++, x <<= 4)
|
||||
putc(fd, digits[x >> (sizeof(u64) * 8 - 4)]);
|
||||
}
|
||||
|
||||
|
@ -52,19 +53,19 @@ void
|
|||
vprintf(int fd, const char *fmt, va_list ap)
|
||||
{
|
||||
char *s;
|
||||
int c, i, state;
|
||||
int c, i, state;
|
||||
|
||||
state = 0;
|
||||
for(i = 0; fmt[i]; i++){
|
||||
for(i = 0; fmt[i]; i++) {
|
||||
c = fmt[i] & 0xff;
|
||||
if(state == 0){
|
||||
if(c == '%'){
|
||||
if(state == 0) {
|
||||
if(c == '%') {
|
||||
state = '%';
|
||||
} else {
|
||||
putc(fd, c);
|
||||
}
|
||||
} else if(state == '%'){
|
||||
if(c == 'd'){
|
||||
} else if(state == '%') {
|
||||
if(c == 'd') {
|
||||
printint(fd, va_arg(ap, int), 10, 1);
|
||||
} else if(c == 'l') {
|
||||
printint(fd, va_arg(ap, u64), 10, 0);
|
||||
|
@ -72,17 +73,17 @@ vprintf(int fd, const char *fmt, va_list ap)
|
|||
printint(fd, va_arg(ap, int), 16, 0);
|
||||
} else if(c == 'p') {
|
||||
printptr(fd, va_arg(ap, u64));
|
||||
} else if(c == 's'){
|
||||
s = va_arg(ap, char*);
|
||||
} else if(c == 's') {
|
||||
s = va_arg(ap, char *);
|
||||
if(s == 0)
|
||||
s = "(null)";
|
||||
while(*s != 0){
|
||||
while(*s != 0) {
|
||||
putc(fd, *s);
|
||||
s++;
|
||||
}
|
||||
} else if(c == 'c'){
|
||||
} else if(c == 'c') {
|
||||
putc(fd, va_arg(ap, u32));
|
||||
} else if(c == '%'){
|
||||
} else if(c == '%') {
|
||||
putc(fd, c);
|
||||
} else {
|
||||
// Unknown % sequence. Print it to draw attention.
|
||||
|
|
|
@ -7,13 +7,13 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
if(argc < 2) {
|
||||
fprintf(2, "Usage: rm files...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if(unlink(argv[i]) < 0){
|
||||
for(i = 1; i < argc; i++) {
|
||||
if(unlink(argv[i]) < 0) {
|
||||
fprintf(2, "rm: %s failed to delete\n", argv[i]);
|
||||
break;
|
||||
}
|
||||
|
|
184
user/sh.c
184
user/sh.c
|
@ -18,62 +18,62 @@ struct cmd {
|
|||
};
|
||||
|
||||
struct execcmd {
|
||||
int type;
|
||||
int type;
|
||||
char *argv[MAXARGS];
|
||||
char *eargv[MAXARGS];
|
||||
};
|
||||
|
||||
struct redircmd {
|
||||
int type;
|
||||
int type;
|
||||
struct cmd *cmd;
|
||||
char *file;
|
||||
char *efile;
|
||||
int mode;
|
||||
int fd;
|
||||
char *file;
|
||||
char *efile;
|
||||
int mode;
|
||||
int fd;
|
||||
};
|
||||
|
||||
struct pipecmd {
|
||||
int type;
|
||||
int type;
|
||||
struct cmd *left;
|
||||
struct cmd *right;
|
||||
};
|
||||
|
||||
struct listcmd {
|
||||
int type;
|
||||
int type;
|
||||
struct cmd *left;
|
||||
struct cmd *right;
|
||||
};
|
||||
|
||||
struct backcmd {
|
||||
int type;
|
||||
int type;
|
||||
struct cmd *cmd;
|
||||
};
|
||||
|
||||
int fork1(void); // Fork but panics on failure.
|
||||
void panic(char*);
|
||||
struct cmd *parsecmd(char*);
|
||||
void runcmd(struct cmd*) __attribute__((noreturn));
|
||||
int fork1(void); // Fork but panics on failure.
|
||||
void panic(char *);
|
||||
struct cmd *parsecmd(char *);
|
||||
void runcmd(struct cmd *) __attribute__((noreturn));
|
||||
|
||||
// Execute cmd. Never returns.
|
||||
void
|
||||
runcmd(struct cmd *cmd)
|
||||
{
|
||||
int p[2];
|
||||
struct backcmd *bcmd;
|
||||
struct execcmd *ecmd;
|
||||
struct listcmd *lcmd;
|
||||
struct pipecmd *pcmd;
|
||||
int p[2];
|
||||
struct backcmd *bcmd;
|
||||
struct execcmd *ecmd;
|
||||
struct listcmd *lcmd;
|
||||
struct pipecmd *pcmd;
|
||||
struct redircmd *rcmd;
|
||||
|
||||
if(cmd == 0)
|
||||
exit(1);
|
||||
|
||||
switch(cmd->type){
|
||||
switch(cmd->type) {
|
||||
default:
|
||||
panic("runcmd");
|
||||
|
||||
case EXEC:
|
||||
ecmd = (struct execcmd*)cmd;
|
||||
ecmd = (struct execcmd *)cmd;
|
||||
if(ecmd->argv[0] == 0)
|
||||
exit(1);
|
||||
exec(ecmd->argv[0], ecmd->argv);
|
||||
|
@ -81,9 +81,9 @@ runcmd(struct cmd *cmd)
|
|||
break;
|
||||
|
||||
case REDIR:
|
||||
rcmd = (struct redircmd*)cmd;
|
||||
rcmd = (struct redircmd *)cmd;
|
||||
close(rcmd->fd);
|
||||
if(open(rcmd->file, rcmd->mode) < 0){
|
||||
if(open(rcmd->file, rcmd->mode) < 0) {
|
||||
fprintf(2, "open %s failed\n", rcmd->file);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ runcmd(struct cmd *cmd)
|
|||
break;
|
||||
|
||||
case LIST:
|
||||
lcmd = (struct listcmd*)cmd;
|
||||
lcmd = (struct listcmd *)cmd;
|
||||
if(fork1() == 0)
|
||||
runcmd(lcmd->left);
|
||||
wait(0);
|
||||
|
@ -99,17 +99,17 @@ runcmd(struct cmd *cmd)
|
|||
break;
|
||||
|
||||
case PIPE:
|
||||
pcmd = (struct pipecmd*)cmd;
|
||||
pcmd = (struct pipecmd *)cmd;
|
||||
if(pipe(p) < 0)
|
||||
panic("pipe");
|
||||
if(fork1() == 0){
|
||||
if(fork1() == 0) {
|
||||
close(1);
|
||||
dup(p[1]);
|
||||
close(p[0]);
|
||||
close(p[1]);
|
||||
runcmd(pcmd->left);
|
||||
}
|
||||
if(fork1() == 0){
|
||||
if(fork1() == 0) {
|
||||
close(0);
|
||||
dup(p[0]);
|
||||
close(p[0]);
|
||||
|
@ -123,7 +123,7 @@ runcmd(struct cmd *cmd)
|
|||
break;
|
||||
|
||||
case BACK:
|
||||
bcmd = (struct backcmd*)cmd;
|
||||
bcmd = (struct backcmd *)cmd;
|
||||
if(fork1() == 0)
|
||||
runcmd(bcmd->cmd);
|
||||
break;
|
||||
|
@ -146,23 +146,23 @@ int
|
|||
main(void)
|
||||
{
|
||||
static char buf[100];
|
||||
int fd;
|
||||
int fd;
|
||||
|
||||
// Ensure that three file descriptors are open.
|
||||
while((fd = open("console", O_RDWR)) >= 0){
|
||||
if(fd >= 3){
|
||||
while((fd = open("console", O_RDWR)) >= 0) {
|
||||
if(fd >= 3) {
|
||||
close(fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Read and run input commands.
|
||||
while(getcmd(buf, sizeof(buf)) >= 0){
|
||||
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
|
||||
while(getcmd(buf, sizeof(buf)) >= 0) {
|
||||
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
|
||||
// Chdir must be called by the parent, not the child.
|
||||
buf[strlen(buf)-1] = 0; // chop \n
|
||||
if(chdir(buf+3) < 0)
|
||||
fprintf(2, "cannot cd %s\n", buf+3);
|
||||
buf[strlen(buf) - 1] = 0; // chop \n
|
||||
if(chdir(buf + 3) < 0)
|
||||
fprintf(2, "cannot cd %s\n", buf + 3);
|
||||
continue;
|
||||
}
|
||||
if(fork1() == 0)
|
||||
|
@ -190,10 +190,10 @@ fork1(void)
|
|||
return pid;
|
||||
}
|
||||
|
||||
//PAGEBREAK!
|
||||
// Constructors
|
||||
// PAGEBREAK!
|
||||
// Constructors
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
execcmd(void)
|
||||
{
|
||||
struct execcmd *cmd;
|
||||
|
@ -201,10 +201,10 @@ execcmd(void)
|
|||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = EXEC;
|
||||
return (struct cmd*)cmd;
|
||||
return (struct cmd *)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
|
||||
{
|
||||
struct redircmd *cmd;
|
||||
|
@ -217,10 +217,10 @@ redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
|
|||
cmd->efile = efile;
|
||||
cmd->mode = mode;
|
||||
cmd->fd = fd;
|
||||
return (struct cmd*)cmd;
|
||||
return (struct cmd *)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
pipecmd(struct cmd *left, struct cmd *right)
|
||||
{
|
||||
struct pipecmd *cmd;
|
||||
|
@ -230,10 +230,10 @@ pipecmd(struct cmd *left, struct cmd *right)
|
|||
cmd->type = PIPE;
|
||||
cmd->left = left;
|
||||
cmd->right = right;
|
||||
return (struct cmd*)cmd;
|
||||
return (struct cmd *)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
listcmd(struct cmd *left, struct cmd *right)
|
||||
{
|
||||
struct listcmd *cmd;
|
||||
|
@ -243,10 +243,10 @@ listcmd(struct cmd *left, struct cmd *right)
|
|||
cmd->type = LIST;
|
||||
cmd->left = left;
|
||||
cmd->right = right;
|
||||
return (struct cmd*)cmd;
|
||||
return (struct cmd *)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
backcmd(struct cmd *subcmd)
|
||||
{
|
||||
struct backcmd *cmd;
|
||||
|
@ -255,10 +255,10 @@ backcmd(struct cmd *subcmd)
|
|||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = BACK;
|
||||
cmd->cmd = subcmd;
|
||||
return (struct cmd*)cmd;
|
||||
return (struct cmd *)cmd;
|
||||
}
|
||||
//PAGEBREAK!
|
||||
// Parsing
|
||||
// PAGEBREAK!
|
||||
// Parsing
|
||||
|
||||
char whitespace[] = " \t\r\n\v";
|
||||
char symbols[] = "<|>&;()";
|
||||
|
@ -267,7 +267,7 @@ int
|
|||
gettoken(char **ps, char *es, char **q, char **eq)
|
||||
{
|
||||
char *s;
|
||||
int ret;
|
||||
int ret;
|
||||
|
||||
s = *ps;
|
||||
while(s < es && strchr(whitespace, *s))
|
||||
|
@ -275,7 +275,7 @@ gettoken(char **ps, char *es, char **q, char **eq)
|
|||
if(q)
|
||||
*q = s;
|
||||
ret = *s;
|
||||
switch(*s){
|
||||
switch(*s) {
|
||||
case 0:
|
||||
break;
|
||||
case '|':
|
||||
|
@ -288,7 +288,7 @@ gettoken(char **ps, char *es, char **q, char **eq)
|
|||
break;
|
||||
case '>':
|
||||
s++;
|
||||
if(*s == '>'){
|
||||
if(*s == '>') {
|
||||
ret = '+';
|
||||
s++;
|
||||
}
|
||||
|
@ -320,21 +320,21 @@ peek(char **ps, char *es, char *toks)
|
|||
return *s && strchr(toks, *s);
|
||||
}
|
||||
|
||||
struct cmd *parseline(char**, char*);
|
||||
struct cmd *parsepipe(char**, char*);
|
||||
struct cmd *parseexec(char**, char*);
|
||||
struct cmd *nulterminate(struct cmd*);
|
||||
struct cmd *parseline(char **, char *);
|
||||
struct cmd *parsepipe(char **, char *);
|
||||
struct cmd *parseexec(char **, char *);
|
||||
struct cmd *nulterminate(struct cmd *);
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parsecmd(char *s)
|
||||
{
|
||||
char *es;
|
||||
char *es;
|
||||
struct cmd *cmd;
|
||||
|
||||
es = s + strlen(s);
|
||||
cmd = parseline(&s, es);
|
||||
peek(&s, es, "");
|
||||
if(s != es){
|
||||
if(s != es) {
|
||||
fprintf(2, "leftovers: %s\n", s);
|
||||
panic("syntax");
|
||||
}
|
||||
|
@ -342,62 +342,62 @@ parsecmd(char *s)
|
|||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parseline(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
||||
cmd = parsepipe(ps, es);
|
||||
while(peek(ps, es, "&")){
|
||||
while(peek(ps, es, "&")) {
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = backcmd(cmd);
|
||||
}
|
||||
if(peek(ps, es, ";")){
|
||||
if(peek(ps, es, ";")) {
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = listcmd(cmd, parseline(ps, es));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parsepipe(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
||||
cmd = parseexec(ps, es);
|
||||
if(peek(ps, es, "|")){
|
||||
if(peek(ps, es, "|")) {
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = pipecmd(cmd, parsepipe(ps, es));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parseredirs(struct cmd *cmd, char **ps, char *es)
|
||||
{
|
||||
int tok;
|
||||
int tok;
|
||||
char *q, *eq;
|
||||
|
||||
while(peek(ps, es, "<>")){
|
||||
while(peek(ps, es, "<>")) {
|
||||
tok = gettoken(ps, es, 0, 0);
|
||||
if(gettoken(ps, es, &q, &eq) != 'a')
|
||||
panic("missing file for redirection");
|
||||
switch(tok){
|
||||
switch(tok) {
|
||||
case '<':
|
||||
cmd = redircmd(cmd, q, eq, O_RDONLY, 0);
|
||||
break;
|
||||
case '>':
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE|O_TRUNC, 1);
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY | O_CREATE | O_TRUNC, 1);
|
||||
break;
|
||||
case '+': // >>
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
|
||||
case '+': // >>
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY | O_CREATE, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parseblock(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
@ -413,24 +413,24 @@ parseblock(char **ps, char *es)
|
|||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
parseexec(char **ps, char *es)
|
||||
{
|
||||
char *q, *eq;
|
||||
int tok, argc;
|
||||
char *q, *eq;
|
||||
int tok, argc;
|
||||
struct execcmd *cmd;
|
||||
struct cmd *ret;
|
||||
struct cmd *ret;
|
||||
|
||||
if(peek(ps, es, "("))
|
||||
return parseblock(ps, es);
|
||||
|
||||
ret = execcmd();
|
||||
cmd = (struct execcmd*)ret;
|
||||
cmd = (struct execcmd *)ret;
|
||||
|
||||
argc = 0;
|
||||
ret = parseredirs(ret, ps, es);
|
||||
while(!peek(ps, es, "|)&;")){
|
||||
if((tok=gettoken(ps, es, &q, &eq)) == 0)
|
||||
while(!peek(ps, es, "|)&;")) {
|
||||
if((tok = gettoken(ps, es, &q, &eq)) == 0)
|
||||
break;
|
||||
if(tok != 'a')
|
||||
panic("syntax");
|
||||
|
@ -447,46 +447,46 @@ parseexec(char **ps, char *es)
|
|||
}
|
||||
|
||||
// NUL-terminate all the counted strings.
|
||||
struct cmd*
|
||||
struct cmd *
|
||||
nulterminate(struct cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
struct backcmd *bcmd;
|
||||
struct execcmd *ecmd;
|
||||
struct listcmd *lcmd;
|
||||
struct pipecmd *pcmd;
|
||||
int i;
|
||||
struct backcmd *bcmd;
|
||||
struct execcmd *ecmd;
|
||||
struct listcmd *lcmd;
|
||||
struct pipecmd *pcmd;
|
||||
struct redircmd *rcmd;
|
||||
|
||||
if(cmd == 0)
|
||||
return 0;
|
||||
|
||||
switch(cmd->type){
|
||||
switch(cmd->type) {
|
||||
case EXEC:
|
||||
ecmd = (struct execcmd*)cmd;
|
||||
for(i=0; ecmd->argv[i]; i++)
|
||||
ecmd = (struct execcmd *)cmd;
|
||||
for(i = 0; ecmd->argv[i]; i++)
|
||||
*ecmd->eargv[i] = 0;
|
||||
break;
|
||||
|
||||
case REDIR:
|
||||
rcmd = (struct redircmd*)cmd;
|
||||
rcmd = (struct redircmd *)cmd;
|
||||
nulterminate(rcmd->cmd);
|
||||
*rcmd->efile = 0;
|
||||
break;
|
||||
|
||||
case PIPE:
|
||||
pcmd = (struct pipecmd*)cmd;
|
||||
pcmd = (struct pipecmd *)cmd;
|
||||
nulterminate(pcmd->left);
|
||||
nulterminate(pcmd->right);
|
||||
break;
|
||||
|
||||
case LIST:
|
||||
lcmd = (struct listcmd*)cmd;
|
||||
lcmd = (struct listcmd *)cmd;
|
||||
nulterminate(lcmd->left);
|
||||
nulterminate(lcmd->right);
|
||||
break;
|
||||
|
||||
case BACK:
|
||||
bcmd = (struct backcmd*)cmd;
|
||||
bcmd = (struct backcmd *)cmd;
|
||||
nulterminate(bcmd->cmd);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
int fd, i;
|
||||
char path[] = "stressfs0";
|
||||
char data[512];
|
||||
|
||||
|
@ -32,14 +32,14 @@ main(int argc, char *argv[])
|
|||
path[8] += i;
|
||||
fd = open(path, O_CREATE | O_RDWR);
|
||||
for(i = 0; i < 20; i++)
|
||||
// printf(fd, "%d\n", i);
|
||||
// printf(fd, "%d\n", i);
|
||||
write(fd, data, sizeof(data));
|
||||
close(fd);
|
||||
|
||||
printf("read\n");
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
for (i = 0; i < 20; i++)
|
||||
for(i = 0; i < 20; i++)
|
||||
read(fd, data, sizeof(data));
|
||||
close(fd);
|
||||
|
||||
|
|
32
user/ulib.c
32
user/ulib.c
|
@ -14,7 +14,7 @@ _main()
|
|||
exit(0);
|
||||
}
|
||||
|
||||
char*
|
||||
char *
|
||||
strcpy(char *s, const char *t)
|
||||
{
|
||||
char *os;
|
||||
|
@ -43,33 +43,33 @@ strlen(const char *s)
|
|||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
void *
|
||||
memset(void *dst, int c, u32 n)
|
||||
{
|
||||
char *cdst = (char *) dst;
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
char *cdst = (char *)dst;
|
||||
int i;
|
||||
for(i = 0; i < n; i++) {
|
||||
cdst[i] = c;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
char*
|
||||
char *
|
||||
strchr(const char *s, char c)
|
||||
{
|
||||
for(; *s; s++)
|
||||
if(*s == c)
|
||||
return (char*)s;
|
||||
return (char *)s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char*
|
||||
char *
|
||||
gets(char *buf, int max)
|
||||
{
|
||||
int i, cc;
|
||||
int i, cc;
|
||||
char c;
|
||||
|
||||
for(i=0; i+1 < max; ){
|
||||
for(i = 0; i + 1 < max;) {
|
||||
cc = read(0, &c, 1);
|
||||
if(cc < 1)
|
||||
break;
|
||||
|
@ -102,19 +102,19 @@ atoi(const char *s)
|
|||
|
||||
n = 0;
|
||||
while('0' <= *s && *s <= '9')
|
||||
n = n*10 + *s++ - '0';
|
||||
n = n * 10 + *s++ - '0';
|
||||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
void *
|
||||
memmove(void *vdst, const void *vsrc, int n)
|
||||
{
|
||||
char *dst;
|
||||
char *dst;
|
||||
const char *src;
|
||||
|
||||
dst = vdst;
|
||||
src = vsrc;
|
||||
if (src > dst) {
|
||||
if(src > dst) {
|
||||
while(n-- > 0)
|
||||
*dst++ = *src++;
|
||||
} else {
|
||||
|
@ -130,8 +130,8 @@ int
|
|||
memcmp(const void *s1, const void *s2, u32 n)
|
||||
{
|
||||
const char *p1 = s1, *p2 = s2;
|
||||
while (n-- > 0) {
|
||||
if (*p1 != *p2) {
|
||||
while(n-- > 0) {
|
||||
if(*p1 != *p2) {
|
||||
return *p1 - *p2;
|
||||
}
|
||||
p1++;
|
||||
|
|
|
@ -11,14 +11,14 @@ typedef long Align;
|
|||
union header {
|
||||
struct {
|
||||
union header *ptr;
|
||||
u32 size;
|
||||
u32 size;
|
||||
} s;
|
||||
Align x;
|
||||
};
|
||||
|
||||
typedef union header Header;
|
||||
|
||||
static Header base;
|
||||
static Header base;
|
||||
static Header *freep;
|
||||
|
||||
void
|
||||
|
@ -26,16 +26,16 @@ free(void *ap)
|
|||
{
|
||||
Header *bp, *p;
|
||||
|
||||
bp = (Header*)ap - 1;
|
||||
bp = (Header *)ap - 1;
|
||||
for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
|
||||
if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
|
||||
break;
|
||||
if(bp + bp->s.size == p->s.ptr){
|
||||
if(bp + bp->s.size == p->s.ptr) {
|
||||
bp->s.size += p->s.ptr->s.size;
|
||||
bp->s.ptr = p->s.ptr->s.ptr;
|
||||
} else
|
||||
bp->s.ptr = p->s.ptr;
|
||||
if(p + p->s.size == bp){
|
||||
if(p + p->s.size == bp) {
|
||||
p->s.size += bp->s.size;
|
||||
p->s.ptr = bp->s.ptr;
|
||||
} else
|
||||
|
@ -43,36 +43,36 @@ free(void *ap)
|
|||
freep = p;
|
||||
}
|
||||
|
||||
static Header*
|
||||
static Header *
|
||||
morecore(u32 nu)
|
||||
{
|
||||
char *p;
|
||||
char *p;
|
||||
Header *hp;
|
||||
|
||||
if(nu < 4096)
|
||||
nu = 4096;
|
||||
p = sbrk(nu * sizeof(Header));
|
||||
if(p == (char*)-1)
|
||||
if(p == (char *)-1)
|
||||
return 0;
|
||||
hp = (Header*)p;
|
||||
hp = (Header *)p;
|
||||
hp->s.size = nu;
|
||||
free((void*)(hp + 1));
|
||||
free((void *)(hp + 1));
|
||||
return freep;
|
||||
}
|
||||
|
||||
void*
|
||||
void *
|
||||
malloc(u32 nbytes)
|
||||
{
|
||||
Header *p, *prevp;
|
||||
u32 nunits;
|
||||
u32 nunits;
|
||||
|
||||
nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1;
|
||||
if((prevp = freep) == 0){
|
||||
nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;
|
||||
if((prevp = freep) == 0) {
|
||||
base.s.ptr = freep = prevp = &base;
|
||||
base.s.size = 0;
|
||||
}
|
||||
for(p = prevp->s.ptr; ; prevp = p, p = p->s.ptr){
|
||||
if(p->s.size >= nunits){
|
||||
for(p = prevp->s.ptr;; prevp = p, p = p->s.ptr) {
|
||||
if(p->s.size >= nunits) {
|
||||
if(p->s.size == nunits)
|
||||
prevp->s.ptr = p->s.ptr;
|
||||
else {
|
||||
|
@ -81,7 +81,7 @@ malloc(u32 nbytes)
|
|||
p->s.size = nunits;
|
||||
}
|
||||
freep = prevp;
|
||||
return (void*)(p + 1);
|
||||
return (void *)(p + 1);
|
||||
}
|
||||
if(p == freep)
|
||||
if((p = morecore(nunits)) == 0)
|
||||
|
|
70
user/user.h
70
user/user.h
|
@ -1,41 +1,41 @@
|
|||
struct stat;
|
||||
|
||||
// system calls
|
||||
int fork(void);
|
||||
int exit(int) __attribute__((noreturn));
|
||||
int wait(int*);
|
||||
int pipe(int*);
|
||||
int write(int, const void*, int);
|
||||
int read(int, void*, int);
|
||||
int close(int);
|
||||
int kill(int);
|
||||
int exec(const char*, char**);
|
||||
int open(const char*, int);
|
||||
int mknod(const char*, short, short);
|
||||
int unlink(const char*);
|
||||
int fstat(int fd, struct stat*);
|
||||
int link(const char*, const char*);
|
||||
int mkdir(const char*);
|
||||
int chdir(const char*);
|
||||
int dup(int);
|
||||
int getpid(void);
|
||||
char* sbrk(int);
|
||||
int sleep(int);
|
||||
int uptime(void);
|
||||
int fork(void);
|
||||
int exit(int) __attribute__((noreturn));
|
||||
int wait(int *);
|
||||
int pipe(int *);
|
||||
int write(int, const void *, int);
|
||||
int read(int, void *, int);
|
||||
int close(int);
|
||||
int kill(int);
|
||||
int exec(const char *, char **);
|
||||
int open(const char *, int);
|
||||
int mknod(const char *, short, short);
|
||||
int unlink(const char *);
|
||||
int fstat(int fd, struct stat *);
|
||||
int link(const char *, const char *);
|
||||
int mkdir(const char *);
|
||||
int chdir(const char *);
|
||||
int dup(int);
|
||||
int getpid(void);
|
||||
char *sbrk(int);
|
||||
int sleep(int);
|
||||
int uptime(void);
|
||||
|
||||
// ulib.c
|
||||
int stat(const char*, struct stat*);
|
||||
char* strcpy(char*, const char*);
|
||||
void *memmove(void*, const void*, int);
|
||||
char* strchr(const char*, char c);
|
||||
int strcmp(const char*, const char*);
|
||||
void fprintf(int, const char*, ...);
|
||||
void printf(const char*, ...);
|
||||
char* gets(char*, int max);
|
||||
u32 strlen(const char*);
|
||||
void* memset(void*, int, u32);
|
||||
void* malloc(u32);
|
||||
void free(void*);
|
||||
int atoi(const char*);
|
||||
int memcmp(const void *, const void *, u32);
|
||||
int stat(const char *, struct stat *);
|
||||
char *strcpy(char *, const char *);
|
||||
void *memmove(void *, const void *, int);
|
||||
char *strchr(const char *, char c);
|
||||
int strcmp(const char *, const char *);
|
||||
void fprintf(int, const char *, ...);
|
||||
void printf(const char *, ...);
|
||||
char *gets(char *, int max);
|
||||
u32 strlen(const char *);
|
||||
void *memset(void *, int, u32);
|
||||
void *malloc(u32);
|
||||
void free(void *);
|
||||
int atoi(const char *);
|
||||
int memcmp(const void *, const void *, u32);
|
||||
void *memcpy(void *, const void *, u32);
|
||||
|
|
1254
user/usertests.c
1254
user/usertests.c
File diff suppressed because it is too large
Load diff
14
user/wc.c
14
user/wc.c
|
@ -12,20 +12,20 @@ wc(int fd, char *name)
|
|||
|
||||
l = w = c = 0;
|
||||
inword = 0;
|
||||
while((n = read(fd, buf, sizeof(buf))) > 0){
|
||||
for(i=0; i<n; i++){
|
||||
while((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
for(i = 0; i < n; i++) {
|
||||
c++;
|
||||
if(buf[i] == '\n')
|
||||
l++;
|
||||
if(strchr(" \r\t\n\v", buf[i]))
|
||||
inword = 0;
|
||||
else if(!inword){
|
||||
else if(!inword) {
|
||||
w++;
|
||||
inword = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(n < 0){
|
||||
if(n < 0) {
|
||||
printf("wc: read error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -37,13 +37,13 @@ main(int argc, char *argv[])
|
|||
{
|
||||
int fd, i;
|
||||
|
||||
if(argc <= 1){
|
||||
if(argc <= 1) {
|
||||
wc(0, "");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
for(i = 1; i < argc; i++) {
|
||||
if((fd = open(argv[i], 0)) < 0) {
|
||||
printf("wc: cannot open %s\n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,6 @@ int
|
|||
main(void)
|
||||
{
|
||||
if(fork() > 0)
|
||||
sleep(5); // Let child exit before parent.
|
||||
sleep(5); // Let child exit before parent.
|
||||
exit(0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue