Skip to content

Commit

Permalink
here it is v0.01!
Browse files Browse the repository at this point in the history
  • Loading branch information
flaneur2020 committed Mar 18, 2011
1 parent f0556fa commit aea3d65
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 28 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ end

# ----------------------------------------------------------------------

usr_cfiles = Dir['usr/test_*.c'] + %w{
usr_cfiles = Dir['usr/test/*.c'] + %w{
usr/init.c
usr/hello.c
usr/sh.c
usr/ls.c
usr/cat.c
}
usr_ofiles = usr_cfiles.map{|fn| 'bin/usr/'+File.basename(fn).ext('o') }
usr_efiles = usr_cfiles.map{|fn| 'bin/usr/'+File.basename(fn).ext('') }
Expand Down
Empty file removed root/about2.txt
Empty file.
Empty file removed root/about3.txt
Empty file.
File renamed without changes.
5 changes: 5 additions & 0 deletions root/home/about2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
When life leaves us blind
Love keeps us kind
When Life leaves us blind
Love keeps us kind

1 change: 1 addition & 0 deletions root/home/about3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
god saves us everyone when we are burning in the fire of a thousand suns.
1 change: 1 addition & 0 deletions src/fs/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
#include <super.h>
#include <inode.h>
#include <dirent.h>
#include <file.h>
#include <stat.h>

Expand Down
4 changes: 2 additions & 2 deletions src/fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
#include <super.h>
#include <inode.h>
#include <dirent.h>
#include <stat.h>


Expand Down Expand Up @@ -124,7 +125,6 @@ struct inode* _namei(char *path, uchar creat, uchar parent, char **name){
// set it's root directory here.
if (*path == '/') {
wip = iget(rootdev, ROOTINO);
cu->p_wdir = wip;
}
else {
cdp = cu->p_wdir;
Expand All @@ -139,8 +139,8 @@ struct inode* _namei(char *path, uchar creat, uchar parent, char **name){
}
// if working inode is root and componet is ".."
if ((wip->i_num==ROOTINO) && (strncmp(path, "..", 2)==0)) {
continue;
}

// wip must be a directory
if ((wip->i_mode & S_IFMT)!=S_IFDIR) {
iput(wip);
Expand Down
3 changes: 3 additions & 0 deletions src/fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ int do_dup(int fd){
if ((newfd=ufalloc())<0) {
return -1;
}
fp->f_count++;
fp->f_ino->i_count++;
cu->p_ofile[newfd] = fp;
return newfd;
Expand All @@ -134,6 +135,7 @@ int do_dup2(int fd, int newfd){
}

do_close(newfd);
fp->f_count++;
fp->f_ino->i_count++;
cu->p_ofile[newfd] = fp;
return newfd;
Expand Down Expand Up @@ -170,5 +172,6 @@ struct file* falloc(int fd){
}
}
syserr(EMFILE);
panic("no free file structure\n");
return NULL;
}
4 changes: 2 additions & 2 deletions src/fs/rdwr.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ int do_read(int fd, char *buf, int cnt){
}

ip = fp->f_ino;
lock_ino(fp->f_ino);
switch(ip->i_mode & S_IFMT) {
case S_IFBLK:
// TODO;
break;
case S_IFCHR:
dev = ip->i_dev;
dev = ip->i_zone[0];
r = (*cdevsw[MAJOR(dev)].d_read)(dev, buf, cnt);
break;
case S_IFDIR:
case S_IFREG:
default:
lock_ino(fp->f_ino);
r = readi(ip, buf, fp->f_offset, cnt);
}
if (r < 0){
Expand Down
13 changes: 13 additions & 0 deletions src/inc/dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DIRENT_H
#define DIRENT_H

/* directory entry */
#define NAMELEN 12

struct dirent {
ushort d_ino;
char d_name[NAMELEN];
char __p[18]; /* a padding. each dirent is aligned with a 32 bytes boundary. */
};

#endif
9 changes: 0 additions & 9 deletions src/inc/inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,4 @@ extern struct inode inode[NINODE];

/* i_mode resides in stat.h */

/* directory entry */
#define NAMELEN 12

struct dirent {
ushort d_ino;
char d_name[NAMELEN];
char __p[18]; /* a padding. each dirent is aligned with a 32 bytes boundary. */
};

#endif
7 changes: 7 additions & 0 deletions src/inc/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ struct stat {
#define S_ISGID 0002000
#define S_ISVTX 0001000

/* predicates */
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)

/* access */
#define RWX_MODES 0000777 /* mode bits for RWX only */
#define R_BIT 0000004 /* Rwx protection bit */
Expand Down
11 changes: 10 additions & 1 deletion src/kern/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@
int do_exec(char *path, char **argv){
struct inode *ip;
struct buf *bp;
struct sigaction *sa;
struct ahead *ah;
struct page *pg;
struct vm *vm;
struct vma *vp;
struct file *fp;
uint bn, fd, argc, esp;
uint bn, fd, argc, esp, nr;
char **tmp;

ip = namei(path, 0);
Expand Down Expand Up @@ -94,6 +95,14 @@ int do_exec(char *path, char **argv){
do_close(fd);
}
}
// clear all sigactions
for (nr=0; nr<NSIG; nr++){
sa = &cu->p_sigact[nr];
sa->sa_handler = SIG_DFL;
sa->sa_mask = 0;
sa->sa_flags = 0;
sa->sa_restorer = NULL;
}
// never forget this:
brelse(bp);
iput(ip);
Expand Down
1 change: 1 addition & 0 deletions src/kern/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int do_exit(int ret){
if (fp != NULL) {
do_close(fd);
}
cu->p_ofile[fd] = NULL;
}
iput(cu->p_iroot);
iput(cu->p_wdir);
Expand Down
1 change: 1 addition & 0 deletions src/kern/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct proc* kspawn(void (*func)()){
for (fd=0; fd<NOFILE; fd++){
fp = cu->p_ofile[fd];
if (fp != NULL) {
fp->f_count++;
fp->f_ino->i_count++;
}
p->p_ofile[fd] = fp;
Expand Down
11 changes: 6 additions & 5 deletions src/kern/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ void psig(){
cu->p_sig &= ~(1<<(n-1));
sa = &(cu->p_sigact[n-1]);
// check blocked signal
if ((sa->sa_flags & SA_NOMASK)==0) {
cu->p_sigmask |= sa->sa_mask;
}
cu->p_cursig = 0;
if (sa->sa_handler != SIG_DFL) {
tf = cu->p_trap;
// save registers and the old mask
// save registers and the old sa_mask
usigsav(&jbuf, tf, cu->p_sigmask);
// store the new sa_mask
if ((sa->sa_flags & SA_NOMASK)==0) {
cu->p_sigmask |= sa->sa_mask;
}
// push to the user stack, with a "shellcode"
esp = tf->esp;
usr = upush(&esp, &_usigret, 16);
Expand All @@ -91,7 +92,7 @@ void psig(){
case SIGCHLD:
case SIGCONT:
return;
// exited on default
// exited on default
case SIGSEGV:
printk("seg fault.\n");
case SIGINT:
Expand Down
32 changes: 32 additions & 0 deletions usr/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <param.h>
#include <file.h>
#include <stat.h>
#include <unistd.h>
#include <dirent.h>

int cat(char *pathp){
int fd;
char buf[32];

fd = open(pathp, "r", 0);
if (fd < 0)
return -1;

while(read(fd, buf, 32)>0){
printf("%s", buf);
memset(buf, 0, 32);

}
write(0, "", 0);
close(fd);
}

int main(int argc, char **argv){
int i = 0;

if (argc>1) {
for(i=1; i<argc; i++)
cat(argv[i]);
}
printf("\n");
}
3 changes: 3 additions & 0 deletions usr/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ int main(int argc, char **argv) {
for(i=0; i<argc; i++){
printf("%s\n", argv[i]);
}
while(1){
printf("hello forever...\n");
}
return 0;
}
41 changes: 41 additions & 0 deletions usr/ls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <param.h>
#include <file.h>
#include <stat.h>
#include <unistd.h>
#include <dirent.h>

int ls(char *pathp){
struct stat sbuf;
struct dirent dbuf;
char pathbuf[1024];
int fd, r;

if (pathp==NULL)
strcpy(pathbuf, ".");
else
strncpy(pathbuf, pathp, 1024);
//
fd = open(pathbuf, O_RDONLY, 0);
if (fd<0) {
return -1;
}
fstat(fd, &sbuf);
if (!S_ISDIR(sbuf.st_mode)){
return -1;
}

while((r=read(fd, &dbuf, sizeof(struct dirent)))>0)
printf("%s\t", dbuf.d_name);
}

int main(int argc, char **argv){
int i = 0;

if (argc==1)
ls(NULL);
else{
for(i=1; i<argc; i++)
ls(argv[i]);
}
printf("\n");
}
42 changes: 34 additions & 8 deletions usr/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ char linebuf[LINEBUFSIZ];
char* argvbuf[ARGVBUFSIZ];
char pathbuf[PATHBUFSIZ];

int hwsig(int sign){
printf("^C\n");
}

int lparse(char *lbuf) {
char *lp, c;
int argc, i;
Expand All @@ -33,23 +37,45 @@ int lparse(char *lbuf) {
}

int main(int argc, char **argv){
int r, i, cnt, ret;
int r,fd, i, cnt, ret;
struct sigaction sa;

setpgrp();
fd = open("/dev/tty0", O_RDWR, 0);
close(0);
close(1);
close(2);
dup(fd);
dup(fd);
dup(fd);
fcntl(0, F_SETFD, 0); // turn off FD_CLOEXEC
fcntl(1, F_SETFD, 0);
fcntl(2, F_SETFD, 0);
close(fd);
//
sa.sa_handler = &hwsig;
sigaction(SIGINT, &sa, NULL);
//
while(1) {
printf("$ ");
memset(linebuf, 0, LINEBUFSIZ);
r = read(0, linebuf, 1024);
linebuf[r] = 0;
cnt = lparse(linebuf);
if (cnt > 0) {
if (fork()==0) {
exec(argvbuf[0], &argvbuf[1]);
strcpy(pathbuf, "/bin/");
strncat(pathbuf, argvbuf[0], PATHBUFSIZ);
exec(pathbuf, &argvbuf[1]);
exit(1);
if (strncmp(argvbuf[0], "cd", 2)==0) {
chdir(argvbuf[1]);
}
else {
if (fork()==0) {
exec(argvbuf[0], &argvbuf[1]);
strcpy(pathbuf, "/bin/");
strncat(pathbuf, argvbuf[0], PATHBUFSIZ);
exec(pathbuf, &argvbuf[1]);
exit(1);
}
wait(&ret);
}
wait(&ret);
}
}
}
19 changes: 19 additions & 0 deletions usr/test/test_chdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <param.h>
#include <file.h>
#include <unistd.h>

int a=3;

int main(int argc, char **argv) {
int fd;

printf("open about.txt: %d\n", open("about.txt", O_RDWR, 0));
chdir("home");
printf("cd home\n");
printf("open about.txt: %d\n", open("about.txt", O_RDWR, 0));
chdir(".");
printf(".\n");
printf("open about.txt: %d\n", open("about.txt", O_RDWR, 0));
return 0;
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit aea3d65

Please sign in to comment.