separate source into kernel/ user/ mkfs/
This commit is contained in:
parent
91ba81110a
commit
5753553213
72 changed files with 157 additions and 296 deletions
43
user/cat.c
Normal file
43
user/cat.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
char buf[512];
|
||||
|
||||
void
|
||||
cat(int fd)
|
||||
{
|
||||
int n;
|
||||
|
||||
while((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
if (write(1, buf, n) != n) {
|
||||
printf(1, "cat: write error\n");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
if(n < 0){
|
||||
printf(1, "cat: read error\n");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
|
||||
if(argc <= 1){
|
||||
cat(0);
|
||||
exit();
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
printf(1, "cat: cannot open %s\n", argv[i]);
|
||||
exit();
|
||||
}
|
||||
cat(fd);
|
||||
close(fd);
|
||||
}
|
||||
exit();
|
||||
}
|
13
user/echo.c
Normal file
13
user/echo.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 1; i < argc; i++)
|
||||
printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n");
|
||||
exit();
|
||||
}
|
56
user/forktest.c
Normal file
56
user/forktest.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
// Test that fork fails gracefully.
|
||||
// Tiny executable so that the limit can be filling the proc table.
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
#define N 1000
|
||||
|
||||
void
|
||||
printf(int fd, const char *s, ...)
|
||||
{
|
||||
write(fd, s, strlen(s));
|
||||
}
|
||||
|
||||
void
|
||||
forktest(void)
|
||||
{
|
||||
int n, pid;
|
||||
|
||||
printf(1, "fork test\n");
|
||||
|
||||
for(n=0; n<N; n++){
|
||||
pid = fork();
|
||||
if(pid < 0)
|
||||
break;
|
||||
if(pid == 0)
|
||||
exit();
|
||||
}
|
||||
|
||||
if(n == N){
|
||||
printf(1, "fork claimed to work N times!\n", N);
|
||||
exit();
|
||||
}
|
||||
|
||||
for(; n > 0; n--){
|
||||
if(wait() < 0){
|
||||
printf(1, "wait stopped early\n");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
if(wait() != -1){
|
||||
printf(1, "wait got too many\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
printf(1, "fork test OK\n");
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
forktest();
|
||||
exit();
|
||||
}
|
107
user/grep.c
Normal file
107
user/grep.c
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Simple grep. Only supports ^ . * $ operators.
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
char buf[1024];
|
||||
int match(char*, char*);
|
||||
|
||||
void
|
||||
grep(char *pattern, int fd)
|
||||
{
|
||||
int n, m;
|
||||
char *p, *q;
|
||||
|
||||
m = 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){
|
||||
*q = 0;
|
||||
if(match(pattern, p)){
|
||||
*q = '\n';
|
||||
write(1, p, q+1 - p);
|
||||
}
|
||||
p = q+1;
|
||||
}
|
||||
if(p == buf)
|
||||
m = 0;
|
||||
if(m > 0){
|
||||
m -= p - buf;
|
||||
memmove(buf, p, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
char *pattern;
|
||||
|
||||
if(argc <= 1){
|
||||
printf(2, "usage: grep pattern [file ...]\n");
|
||||
exit();
|
||||
}
|
||||
pattern = argv[1];
|
||||
|
||||
if(argc <= 2){
|
||||
grep(pattern, 0);
|
||||
exit();
|
||||
}
|
||||
|
||||
for(i = 2; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
printf(1, "grep: cannot open %s\n", argv[i]);
|
||||
exit();
|
||||
}
|
||||
grep(pattern, fd);
|
||||
close(fd);
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
// Regexp matcher from Kernighan & Pike,
|
||||
// The Practice of Programming, Chapter 9.
|
||||
|
||||
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
|
||||
if(matchhere(re, text))
|
||||
return 1;
|
||||
}while(*text++ != '\0');
|
||||
return 0;
|
||||
}
|
||||
|
||||
// matchhere: search for re at beginning of text
|
||||
int matchhere(char *re, char *text)
|
||||
{
|
||||
if(re[0] == '\0')
|
||||
return 1;
|
||||
if(re[1] == '*')
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// matchstar: search for c*re at beginning of text
|
||||
int matchstar(int c, char *re, char *text)
|
||||
{
|
||||
do{ // a * matches zero or more instances
|
||||
if(matchhere(re, text))
|
||||
return 1;
|
||||
}while(*text!='\0' && (*text++==c || c=='.'));
|
||||
return 0;
|
||||
}
|
||||
|
37
user/init.c
Normal file
37
user/init.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
// init: The initial user-level program
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/fcntl.h"
|
||||
|
||||
char *argv[] = { "sh", 0 };
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int pid, wpid;
|
||||
|
||||
if(open("console", O_RDWR) < 0){
|
||||
mknod("console", 1, 1);
|
||||
open("console", O_RDWR);
|
||||
}
|
||||
dup(0); // stdout
|
||||
dup(0); // stderr
|
||||
|
||||
for(;;){
|
||||
printf(1, "init: starting sh\n");
|
||||
pid = fork();
|
||||
if(pid < 0){
|
||||
printf(1, "init: fork failed\n");
|
||||
exit();
|
||||
}
|
||||
if(pid == 0){
|
||||
exec("sh", argv);
|
||||
printf(1, "init: exec sh failed\n");
|
||||
exit();
|
||||
}
|
||||
while((wpid=wait()) >= 0 && wpid != pid)
|
||||
printf(1, "zombie!\n");
|
||||
}
|
||||
}
|
28
user/initcode.S
Normal file
28
user/initcode.S
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Initial process execs /init.
|
||||
# This code runs in user space.
|
||||
|
||||
#include "syscall.h"
|
||||
|
||||
# exec(init, argv)
|
||||
.globl start
|
||||
start:
|
||||
la a0, init
|
||||
la a1, argv
|
||||
li a7, SYS_exec
|
||||
ecall
|
||||
|
||||
# for(;;) exit();
|
||||
exit:
|
||||
li a7, SYS_exit
|
||||
ecall
|
||||
jal exit
|
||||
|
||||
# char init[] = "/init\0";
|
||||
init:
|
||||
.string "/init\0"
|
||||
|
||||
# char *argv[] = { init, 0 };
|
||||
.p2align 2
|
||||
argv:
|
||||
.long init
|
||||
.long 0
|
17
user/kill.c
Normal file
17
user/kill.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
printf(2, "usage: kill pid...\n");
|
||||
exit();
|
||||
}
|
||||
for(i=1; i<argc; i++)
|
||||
kill(atoi(argv[i]));
|
||||
exit();
|
||||
}
|
15
user/ln.c
Normal file
15
user/ln.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 3){
|
||||
printf(2, "Usage: ln old new\n");
|
||||
exit();
|
||||
}
|
||||
if(link(argv[1], argv[2]) < 0)
|
||||
printf(2, "link %s %s: failed\n", argv[1], argv[2]);
|
||||
exit();
|
||||
}
|
85
user/ls.c
Normal file
85
user/ls.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/fs.h"
|
||||
|
||||
char*
|
||||
fmtname(char *path)
|
||||
{
|
||||
static char buf[DIRSIZ+1];
|
||||
char *p;
|
||||
|
||||
// Find first character after last slash.
|
||||
for(p=path+strlen(path); p >= path && *p != '/'; p--)
|
||||
;
|
||||
p++;
|
||||
|
||||
// Return blank-padded name.
|
||||
if(strlen(p) >= DIRSIZ)
|
||||
return p;
|
||||
memmove(buf, p, strlen(p));
|
||||
memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
ls(char *path)
|
||||
{
|
||||
char buf[512], *p;
|
||||
int fd;
|
||||
struct dirent de;
|
||||
struct stat st;
|
||||
|
||||
if((fd = open(path, 0)) < 0){
|
||||
printf(2, "ls: cannot open %s\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
if(fstat(fd, &st) < 0){
|
||||
printf(2, "ls: cannot stat %s\n", path);
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(st.type){
|
||||
case T_FILE:
|
||||
printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size);
|
||||
break;
|
||||
|
||||
case T_DIR:
|
||||
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
|
||||
printf(1, "ls: path too long\n");
|
||||
break;
|
||||
}
|
||||
strcpy(buf, path);
|
||||
p = buf+strlen(buf);
|
||||
*p++ = '/';
|
||||
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){
|
||||
printf(1, "ls: cannot stat %s\n", buf);
|
||||
continue;
|
||||
}
|
||||
printf(1, "%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
|
||||
}
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
ls(".");
|
||||
exit();
|
||||
}
|
||||
for(i=1; i<argc; i++)
|
||||
ls(argv[i]);
|
||||
exit();
|
||||
}
|
23
user/mkdir.c
Normal file
23
user/mkdir.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
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();
|
||||
}
|
95
user/printf.c
Normal file
95
user/printf.c
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
static char digits[] = "0123456789ABCDEF";
|
||||
|
||||
static void
|
||||
putc(int fd, char c)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
||||
static void
|
||||
printint(int fd, int xx, int base, int sgn)
|
||||
{
|
||||
char buf[16];
|
||||
int i, neg;
|
||||
uint x;
|
||||
|
||||
neg = 0;
|
||||
if(sgn && xx < 0){
|
||||
neg = 1;
|
||||
x = -xx;
|
||||
} else {
|
||||
x = xx;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
do{
|
||||
buf[i++] = digits[x % base];
|
||||
}while((x /= base) != 0);
|
||||
if(neg)
|
||||
buf[i++] = '-';
|
||||
|
||||
while(--i >= 0)
|
||||
putc(fd, buf[i]);
|
||||
}
|
||||
|
||||
static void
|
||||
printptr(int fd, uint64 x) {
|
||||
int i;
|
||||
putc(fd, '0');
|
||||
putc(fd, 'x');
|
||||
for (i = 0; i < (sizeof(uint64) * 2); i++, x <<= 4)
|
||||
putc(fd, digits[x >> (sizeof(uint64) * 8 - 4)]);
|
||||
}
|
||||
|
||||
// Print to the given fd. Only understands %d, %x, %p, %s.
|
||||
void
|
||||
printf(int fd, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *s;
|
||||
int c, i, state;
|
||||
|
||||
va_start(ap, fmt);
|
||||
state = 0;
|
||||
for(i = 0; fmt[i]; i++){
|
||||
c = fmt[i] & 0xff;
|
||||
if(state == 0){
|
||||
if(c == '%'){
|
||||
state = '%';
|
||||
} else {
|
||||
putc(fd, c);
|
||||
}
|
||||
} else if(state == '%'){
|
||||
if(c == 'd'){
|
||||
printint(fd, va_arg(ap, int), 10, 1);
|
||||
} else if(c == 'x') {
|
||||
printint(fd, va_arg(ap, int), 16, 0);
|
||||
} else if(c == 'p') {
|
||||
printptr(fd, va_arg(ap, uint64));
|
||||
} else if(c == 's'){
|
||||
s = va_arg(ap, char*);
|
||||
if(s == 0)
|
||||
s = "(null)";
|
||||
while(*s != 0){
|
||||
putc(fd, *s);
|
||||
s++;
|
||||
}
|
||||
} else if(c == 'c'){
|
||||
putc(fd, va_arg(ap, uint));
|
||||
} else if(c == '%'){
|
||||
putc(fd, c);
|
||||
} else {
|
||||
// Unknown % sequence. Print it to draw attention.
|
||||
putc(fd, '%');
|
||||
putc(fd, c);
|
||||
}
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
}
|
23
user/rm.c
Normal file
23
user/rm.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
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, "rm: %s failed to delete\n", argv[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit();
|
||||
}
|
493
user/sh.c
Normal file
493
user/sh.c
Normal file
|
@ -0,0 +1,493 @@
|
|||
// Shell.
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/fcntl.h"
|
||||
|
||||
// Parsed command representation
|
||||
#define EXEC 1
|
||||
#define REDIR 2
|
||||
#define PIPE 3
|
||||
#define LIST 4
|
||||
#define BACK 5
|
||||
|
||||
#define MAXARGS 10
|
||||
|
||||
struct cmd {
|
||||
int type;
|
||||
};
|
||||
|
||||
struct execcmd {
|
||||
int type;
|
||||
char *argv[MAXARGS];
|
||||
char *eargv[MAXARGS];
|
||||
};
|
||||
|
||||
struct redircmd {
|
||||
int type;
|
||||
struct cmd *cmd;
|
||||
char *file;
|
||||
char *efile;
|
||||
int mode;
|
||||
int fd;
|
||||
};
|
||||
|
||||
struct pipecmd {
|
||||
int type;
|
||||
struct cmd *left;
|
||||
struct cmd *right;
|
||||
};
|
||||
|
||||
struct listcmd {
|
||||
int type;
|
||||
struct cmd *left;
|
||||
struct cmd *right;
|
||||
};
|
||||
|
||||
struct backcmd {
|
||||
int type;
|
||||
struct cmd *cmd;
|
||||
};
|
||||
|
||||
int fork1(void); // Fork but panics on failure.
|
||||
void panic(char*);
|
||||
struct cmd *parsecmd(char*);
|
||||
|
||||
// Execute cmd. Never returns.
|
||||
void
|
||||
runcmd(struct cmd *cmd)
|
||||
{
|
||||
int p[2];
|
||||
struct backcmd *bcmd;
|
||||
struct execcmd *ecmd;
|
||||
struct listcmd *lcmd;
|
||||
struct pipecmd *pcmd;
|
||||
struct redircmd *rcmd;
|
||||
|
||||
if(cmd == 0)
|
||||
exit();
|
||||
|
||||
switch(cmd->type){
|
||||
default:
|
||||
panic("runcmd");
|
||||
|
||||
case EXEC:
|
||||
ecmd = (struct execcmd*)cmd;
|
||||
if(ecmd->argv[0] == 0)
|
||||
exit();
|
||||
exec(ecmd->argv[0], ecmd->argv);
|
||||
printf(2, "exec %s failed\n", ecmd->argv[0]);
|
||||
break;
|
||||
|
||||
case REDIR:
|
||||
rcmd = (struct redircmd*)cmd;
|
||||
close(rcmd->fd);
|
||||
if(open(rcmd->file, rcmd->mode) < 0){
|
||||
printf(2, "open %s failed\n", rcmd->file);
|
||||
exit();
|
||||
}
|
||||
runcmd(rcmd->cmd);
|
||||
break;
|
||||
|
||||
case LIST:
|
||||
lcmd = (struct listcmd*)cmd;
|
||||
if(fork1() == 0)
|
||||
runcmd(lcmd->left);
|
||||
wait();
|
||||
runcmd(lcmd->right);
|
||||
break;
|
||||
|
||||
case PIPE:
|
||||
pcmd = (struct pipecmd*)cmd;
|
||||
if(pipe(p) < 0)
|
||||
panic("pipe");
|
||||
if(fork1() == 0){
|
||||
close(1);
|
||||
dup(p[1]);
|
||||
close(p[0]);
|
||||
close(p[1]);
|
||||
runcmd(pcmd->left);
|
||||
}
|
||||
if(fork1() == 0){
|
||||
close(0);
|
||||
dup(p[0]);
|
||||
close(p[0]);
|
||||
close(p[1]);
|
||||
runcmd(pcmd->right);
|
||||
}
|
||||
close(p[0]);
|
||||
close(p[1]);
|
||||
wait();
|
||||
wait();
|
||||
break;
|
||||
|
||||
case BACK:
|
||||
bcmd = (struct backcmd*)cmd;
|
||||
if(fork1() == 0)
|
||||
runcmd(bcmd->cmd);
|
||||
break;
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
int
|
||||
getcmd(char *buf, int nbuf)
|
||||
{
|
||||
printf(2, "$ ");
|
||||
memset(buf, 0, nbuf);
|
||||
gets(buf, nbuf);
|
||||
if(buf[0] == 0) // EOF
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
static char buf[100];
|
||||
int fd;
|
||||
|
||||
// Ensure that three file descriptors are open.
|
||||
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] == ' '){
|
||||
// Chdir must be called by the parent, not the child.
|
||||
buf[strlen(buf)-1] = 0; // chop \n
|
||||
if(chdir(buf+3) < 0)
|
||||
printf(2, "cannot cd %s\n", buf+3);
|
||||
continue;
|
||||
}
|
||||
if(fork1() == 0)
|
||||
runcmd(parsecmd(buf));
|
||||
wait();
|
||||
}
|
||||
exit();
|
||||
}
|
||||
|
||||
void
|
||||
panic(char *s)
|
||||
{
|
||||
printf(2, "%s\n", s);
|
||||
exit();
|
||||
}
|
||||
|
||||
int
|
||||
fork1(void)
|
||||
{
|
||||
int pid;
|
||||
|
||||
pid = fork();
|
||||
if(pid == -1)
|
||||
panic("fork");
|
||||
return pid;
|
||||
}
|
||||
|
||||
//PAGEBREAK!
|
||||
// Constructors
|
||||
|
||||
struct cmd*
|
||||
execcmd(void)
|
||||
{
|
||||
struct execcmd *cmd;
|
||||
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = EXEC;
|
||||
return (struct cmd*)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
|
||||
{
|
||||
struct redircmd *cmd;
|
||||
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = REDIR;
|
||||
cmd->cmd = subcmd;
|
||||
cmd->file = file;
|
||||
cmd->efile = efile;
|
||||
cmd->mode = mode;
|
||||
cmd->fd = fd;
|
||||
return (struct cmd*)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
pipecmd(struct cmd *left, struct cmd *right)
|
||||
{
|
||||
struct pipecmd *cmd;
|
||||
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = PIPE;
|
||||
cmd->left = left;
|
||||
cmd->right = right;
|
||||
return (struct cmd*)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
listcmd(struct cmd *left, struct cmd *right)
|
||||
{
|
||||
struct listcmd *cmd;
|
||||
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = LIST;
|
||||
cmd->left = left;
|
||||
cmd->right = right;
|
||||
return (struct cmd*)cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
backcmd(struct cmd *subcmd)
|
||||
{
|
||||
struct backcmd *cmd;
|
||||
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
memset(cmd, 0, sizeof(*cmd));
|
||||
cmd->type = BACK;
|
||||
cmd->cmd = subcmd;
|
||||
return (struct cmd*)cmd;
|
||||
}
|
||||
//PAGEBREAK!
|
||||
// Parsing
|
||||
|
||||
char whitespace[] = " \t\r\n\v";
|
||||
char symbols[] = "<|>&;()";
|
||||
|
||||
int
|
||||
gettoken(char **ps, char *es, char **q, char **eq)
|
||||
{
|
||||
char *s;
|
||||
int ret;
|
||||
|
||||
s = *ps;
|
||||
while(s < es && strchr(whitespace, *s))
|
||||
s++;
|
||||
if(q)
|
||||
*q = s;
|
||||
ret = *s;
|
||||
switch(*s){
|
||||
case 0:
|
||||
break;
|
||||
case '|':
|
||||
case '(':
|
||||
case ')':
|
||||
case ';':
|
||||
case '&':
|
||||
case '<':
|
||||
s++;
|
||||
break;
|
||||
case '>':
|
||||
s++;
|
||||
if(*s == '>'){
|
||||
ret = '+';
|
||||
s++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ret = 'a';
|
||||
while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s))
|
||||
s++;
|
||||
break;
|
||||
}
|
||||
if(eq)
|
||||
*eq = s;
|
||||
|
||||
while(s < es && strchr(whitespace, *s))
|
||||
s++;
|
||||
*ps = s;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
peek(char **ps, char *es, char *toks)
|
||||
{
|
||||
char *s;
|
||||
|
||||
s = *ps;
|
||||
while(s < es && strchr(whitespace, *s))
|
||||
s++;
|
||||
*ps = s;
|
||||
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*
|
||||
parsecmd(char *s)
|
||||
{
|
||||
char *es;
|
||||
struct cmd *cmd;
|
||||
|
||||
es = s + strlen(s);
|
||||
cmd = parseline(&s, es);
|
||||
peek(&s, es, "");
|
||||
if(s != es){
|
||||
printf(2, "leftovers: %s\n", s);
|
||||
panic("syntax");
|
||||
}
|
||||
nulterminate(cmd);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
parseline(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
||||
cmd = parsepipe(ps, es);
|
||||
while(peek(ps, es, "&")){
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = backcmd(cmd);
|
||||
}
|
||||
if(peek(ps, es, ";")){
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = listcmd(cmd, parseline(ps, es));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
parsepipe(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
||||
cmd = parseexec(ps, es);
|
||||
if(peek(ps, es, "|")){
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = pipecmd(cmd, parsepipe(ps, es));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
parseredirs(struct cmd *cmd, char **ps, char *es)
|
||||
{
|
||||
int tok;
|
||||
char *q, *eq;
|
||||
|
||||
while(peek(ps, es, "<>")){
|
||||
tok = gettoken(ps, es, 0, 0);
|
||||
if(gettoken(ps, es, &q, &eq) != 'a')
|
||||
panic("missing file for redirection");
|
||||
switch(tok){
|
||||
case '<':
|
||||
cmd = redircmd(cmd, q, eq, O_RDONLY, 0);
|
||||
break;
|
||||
case '>':
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
|
||||
break;
|
||||
case '+': // >>
|
||||
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
parseblock(char **ps, char *es)
|
||||
{
|
||||
struct cmd *cmd;
|
||||
|
||||
if(!peek(ps, es, "("))
|
||||
panic("parseblock");
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = parseline(ps, es);
|
||||
if(!peek(ps, es, ")"))
|
||||
panic("syntax - missing )");
|
||||
gettoken(ps, es, 0, 0);
|
||||
cmd = parseredirs(cmd, ps, es);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
struct cmd*
|
||||
parseexec(char **ps, char *es)
|
||||
{
|
||||
char *q, *eq;
|
||||
int tok, argc;
|
||||
struct execcmd *cmd;
|
||||
struct cmd *ret;
|
||||
|
||||
if(peek(ps, es, "("))
|
||||
return parseblock(ps, es);
|
||||
|
||||
ret = execcmd();
|
||||
cmd = (struct execcmd*)ret;
|
||||
|
||||
argc = 0;
|
||||
ret = parseredirs(ret, ps, es);
|
||||
while(!peek(ps, es, "|)&;")){
|
||||
if((tok=gettoken(ps, es, &q, &eq)) == 0)
|
||||
break;
|
||||
if(tok != 'a')
|
||||
panic("syntax");
|
||||
cmd->argv[argc] = q;
|
||||
cmd->eargv[argc] = eq;
|
||||
argc++;
|
||||
if(argc >= MAXARGS)
|
||||
panic("too many args");
|
||||
ret = parseredirs(ret, ps, es);
|
||||
}
|
||||
cmd->argv[argc] = 0;
|
||||
cmd->eargv[argc] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// NUL-terminate all the counted strings.
|
||||
struct cmd*
|
||||
nulterminate(struct cmd *cmd)
|
||||
{
|
||||
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){
|
||||
case EXEC:
|
||||
ecmd = (struct execcmd*)cmd;
|
||||
for(i=0; ecmd->argv[i]; i++)
|
||||
*ecmd->eargv[i] = 0;
|
||||
break;
|
||||
|
||||
case REDIR:
|
||||
rcmd = (struct redircmd*)cmd;
|
||||
nulterminate(rcmd->cmd);
|
||||
*rcmd->efile = 0;
|
||||
break;
|
||||
|
||||
case PIPE:
|
||||
pcmd = (struct pipecmd*)cmd;
|
||||
nulterminate(pcmd->left);
|
||||
nulterminate(pcmd->right);
|
||||
break;
|
||||
|
||||
case LIST:
|
||||
lcmd = (struct listcmd*)cmd;
|
||||
nulterminate(lcmd->left);
|
||||
nulterminate(lcmd->right);
|
||||
break;
|
||||
|
||||
case BACK:
|
||||
bcmd = (struct backcmd*)cmd;
|
||||
nulterminate(bcmd->cmd);
|
||||
break;
|
||||
}
|
||||
return cmd;
|
||||
}
|
49
user/stressfs.c
Normal file
49
user/stressfs.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Demonstrate that moving the "acquire" in iderw after the loop that
|
||||
// appends to the idequeue results in a race.
|
||||
|
||||
// For this to work, you should also add a spin within iderw's
|
||||
// idequeue traversal loop. Adding the following demonstrated a panic
|
||||
// after about 5 runs of stressfs in QEMU on a 2.1GHz CPU:
|
||||
// for (i = 0; i < 40000; i++)
|
||||
// asm volatile("");
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/fs.h"
|
||||
#include "kernel/fcntl.h"
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
char path[] = "stressfs0";
|
||||
char data[512];
|
||||
|
||||
printf(1, "stressfs starting\n");
|
||||
memset(data, 'a', sizeof(data));
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
if(fork() > 0)
|
||||
break;
|
||||
|
||||
printf(1, "write %d\n", i);
|
||||
|
||||
path[8] += i;
|
||||
fd = open(path, O_CREATE | O_RDWR);
|
||||
for(i = 0; i < 20; i++)
|
||||
// printf(fd, "%d\n", i);
|
||||
write(fd, data, sizeof(data));
|
||||
close(fd);
|
||||
|
||||
printf(1, "read\n");
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
for (i = 0; i < 20; i++)
|
||||
read(fd, data, sizeof(data));
|
||||
close(fd);
|
||||
|
||||
wait();
|
||||
|
||||
exit();
|
||||
}
|
109
user/ulib.c
Normal file
109
user/ulib.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "kernel/fcntl.h"
|
||||
#include "user/user.h"
|
||||
|
||||
char*
|
||||
strcpy(char *s, const char *t)
|
||||
{
|
||||
char *os;
|
||||
|
||||
os = s;
|
||||
while((*s++ = *t++) != 0)
|
||||
;
|
||||
return os;
|
||||
}
|
||||
|
||||
int
|
||||
strcmp(const char *p, const char *q)
|
||||
{
|
||||
while(*p && *p == *q)
|
||||
p++, q++;
|
||||
return (uchar)*p - (uchar)*q;
|
||||
}
|
||||
|
||||
uint
|
||||
strlen(const char *s)
|
||||
{
|
||||
int n;
|
||||
|
||||
for(n = 0; s[n]; n++)
|
||||
;
|
||||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
memset(void *dst, int c, uint n)
|
||||
{
|
||||
char *cdst = (char *) dst;
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
cdst[i] = c;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
char*
|
||||
strchr(const char *s, char c)
|
||||
{
|
||||
for(; *s; s++)
|
||||
if(*s == c)
|
||||
return (char*)s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char*
|
||||
gets(char *buf, int max)
|
||||
{
|
||||
int i, cc;
|
||||
char c;
|
||||
|
||||
for(i=0; i+1 < max; ){
|
||||
cc = read(0, &c, 1);
|
||||
if(cc < 1)
|
||||
break;
|
||||
buf[i++] = c;
|
||||
if(c == '\n' || c == '\r')
|
||||
break;
|
||||
}
|
||||
buf[i] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
int
|
||||
stat(const char *n, struct stat *st)
|
||||
{
|
||||
int fd;
|
||||
int r;
|
||||
|
||||
fd = open(n, O_RDONLY);
|
||||
if(fd < 0)
|
||||
return -1;
|
||||
r = fstat(fd, st);
|
||||
close(fd);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
atoi(const char *s)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = 0;
|
||||
while('0' <= *s && *s <= '9')
|
||||
n = n*10 + *s++ - '0';
|
||||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
memmove(void *vdst, const void *vsrc, int n)
|
||||
{
|
||||
char *dst;
|
||||
const char *src;
|
||||
|
||||
dst = vdst;
|
||||
src = vsrc;
|
||||
while(n-- > 0)
|
||||
*dst++ = *src++;
|
||||
return vdst;
|
||||
}
|
90
user/umalloc.c
Normal file
90
user/umalloc.c
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
#include "kernel/param.h"
|
||||
|
||||
// Memory allocator by Kernighan and Ritchie,
|
||||
// The C programming Language, 2nd ed. Section 8.7.
|
||||
|
||||
typedef long Align;
|
||||
|
||||
union header {
|
||||
struct {
|
||||
union header *ptr;
|
||||
uint size;
|
||||
} s;
|
||||
Align x;
|
||||
};
|
||||
|
||||
typedef union header Header;
|
||||
|
||||
static Header base;
|
||||
static Header *freep;
|
||||
|
||||
void
|
||||
free(void *ap)
|
||||
{
|
||||
Header *bp, *p;
|
||||
|
||||
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){
|
||||
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){
|
||||
p->s.size += bp->s.size;
|
||||
p->s.ptr = bp->s.ptr;
|
||||
} else
|
||||
p->s.ptr = bp;
|
||||
freep = p;
|
||||
}
|
||||
|
||||
static Header*
|
||||
morecore(uint nu)
|
||||
{
|
||||
char *p;
|
||||
Header *hp;
|
||||
|
||||
if(nu < 4096)
|
||||
nu = 4096;
|
||||
p = sbrk(nu * sizeof(Header));
|
||||
if(p == (char*)-1)
|
||||
return 0;
|
||||
hp = (Header*)p;
|
||||
hp->s.size = nu;
|
||||
free((void*)(hp + 1));
|
||||
return freep;
|
||||
}
|
||||
|
||||
void*
|
||||
malloc(uint nbytes)
|
||||
{
|
||||
Header *p, *prevp;
|
||||
uint nunits;
|
||||
|
||||
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){
|
||||
if(p->s.size == nunits)
|
||||
prevp->s.ptr = p->s.ptr;
|
||||
else {
|
||||
p->s.size -= nunits;
|
||||
p += p->s.size;
|
||||
p->s.size = nunits;
|
||||
}
|
||||
freep = prevp;
|
||||
return (void*)(p + 1);
|
||||
}
|
||||
if(p == freep)
|
||||
if((p = morecore(nunits)) == 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
39
user/user.h
Normal file
39
user/user.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
struct stat;
|
||||
struct rtcdate;
|
||||
|
||||
// system calls
|
||||
int fork(void);
|
||||
int exit(void) __attribute__((noreturn));
|
||||
int wait(void);
|
||||
int pipe(int*);
|
||||
int write(int, const void*, int);
|
||||
int read(int, void*, int);
|
||||
int close(int);
|
||||
int kill(int);
|
||||
int exec(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 printf(int, const char*, ...);
|
||||
char* gets(char*, int max);
|
||||
uint strlen(const char*);
|
||||
void* memset(void*, int, uint);
|
||||
void* malloc(uint);
|
||||
void free(void*);
|
||||
int atoi(const char*);
|
1789
user/usertests.c
Normal file
1789
user/usertests.c
Normal file
File diff suppressed because it is too large
Load diff
38
user/usys.pl
Executable file
38
user/usys.pl
Executable file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/perl -w
|
||||
|
||||
# Generate usys.S, the stubs for syscalls.
|
||||
|
||||
print "# generated by usys.pl - do not edit\n";
|
||||
|
||||
print "#include \"kernel/syscall.h\"\n";
|
||||
|
||||
sub entry {
|
||||
my $name = shift;
|
||||
print ".global $name\n";
|
||||
print "${name}:\n";
|
||||
print " li a7, SYS_${name}\n";
|
||||
print " ecall\n";
|
||||
print " ret\n";
|
||||
}
|
||||
|
||||
entry("fork");
|
||||
entry("exit");
|
||||
entry("wait");
|
||||
entry("pipe");
|
||||
entry("read");
|
||||
entry("write");
|
||||
entry("close");
|
||||
entry("kill");
|
||||
entry("exec");
|
||||
entry("open");
|
||||
entry("mknod");
|
||||
entry("unlink");
|
||||
entry("fstat");
|
||||
entry("link");
|
||||
entry("mkdir");
|
||||
entry("chdir");
|
||||
entry("dup");
|
||||
entry("getpid");
|
||||
entry("sbrk");
|
||||
entry("sleep");
|
||||
entry("uptime");
|
54
user/wc.c
Normal file
54
user/wc.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
char buf[512];
|
||||
|
||||
void
|
||||
wc(int fd, char *name)
|
||||
{
|
||||
int i, n;
|
||||
int l, w, c, inword;
|
||||
|
||||
l = w = c = 0;
|
||||
inword = 0;
|
||||
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){
|
||||
w++;
|
||||
inword = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(n < 0){
|
||||
printf(1, "wc: read error\n");
|
||||
exit();
|
||||
}
|
||||
printf(1, "%d %d %d %s\n", l, w, c, name);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd, i;
|
||||
|
||||
if(argc <= 1){
|
||||
wc(0, "");
|
||||
exit();
|
||||
}
|
||||
|
||||
for(i = 1; i < argc; i++){
|
||||
if((fd = open(argv[i], 0)) < 0){
|
||||
printf(1, "wc: cannot open %s\n", argv[i]);
|
||||
exit();
|
||||
}
|
||||
wc(fd, argv[i]);
|
||||
close(fd);
|
||||
}
|
||||
exit();
|
||||
}
|
14
user/zombie.c
Normal file
14
user/zombie.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Create a zombie process that
|
||||
// must be reparented at exit.
|
||||
|
||||
#include "kernel/types.h"
|
||||
#include "kernel/stat.h"
|
||||
#include "user/user.h"
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
if(fork() > 0)
|
||||
sleep(5); // Let child exit before parent.
|
||||
exit();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue