int __init(int argc, char **argv) { int ans; _error_recursion = 0; _curbrk = &end; _ctype_init(); /* init to C locale */ _init_alloc(); _exit_init(); /* must happen before exit() can be called */ _initio(NULL, NULL, NULL); ans = main(argc, argv, _environ); while (number_of_exit_functions!=0) (*_exitvector[--number_of_exit_functions])(); _terminateio(); _syscall1(SYS_exit, ans); }
extern int system(const char *string) { int pid; void (*s2)(int); void (*s3)(int); if ((pid = _vfork()) == 0) { char *gv[4] = {"sh", "-c", (char*)0, (char*)0}; gv[2] = (char *)string; _syscall3(SYS_execve, (int)"/bin/sh", (int)gv, (int)_environ); _syscall1(SYS_exit,127); /* Should not get here!! */ } s2 = signal(SIGINT, SIG_IGN); s3 = signal(SIGQUIT, SIG_IGN); while (1) { int status; int wp = _wait(&status); if (wp == pid || wp == -1) { signal(SIGINT,s2); signal(SIGQUIT,s3); return (wp == -1 ? -1 : status); } } }
//等待子进程,子进程状态存储到status pid_t wait(int32_t* status){ return _syscall1(SYS_WAIT,status); }
//以状态status退出 void exit(int32_t status){ _syscall1(SYS_EXIT,status); }
/* 改变工作目录为path */ int32_t chdir(const char* path) { return _syscall1(SYS_CHDIR, path); }
/* 回归目录指针 */ void rewinddir(struct dir* dir) { _syscall1(SYS_REWINDDIR, dir); }
/* 读取目录dir */ struct dir_entry* readdir(struct dir* dir) { return (struct dir_entry*)_syscall1(SYS_READDIR, dir); }
//释放ptr指向的内存 void free(void* ptr){ _syscall1(SYS_FREE,ptr); }
/* 打开目录name */ struct dir* opendir(const char* name) { return (struct dir*)_syscall1(SYS_OPENDIR, name); }
/* 创建目录pathname */ int32_t mkdir(const char* pathname) { return _syscall1(SYS_MKDIR, pathname); }
/* 删除文件pathname */ int32_t unlink(const char* pathname) { return _syscall1(SYS_UNLINK, pathname); }
/* 关闭文件fd */ int32_t close(int32_t fd) { return _syscall1(SYS_CLOSE, fd); }
uint32_t wait(int* stat_loc) { return _syscall1(SYS_WAIT, (uint32_t)stat_loc); }
//生成管道,pipefd[0]负责读入管道,pipefd[1]负责写入管道 int32_t pipe(int32_t pipefd[2]){ return _syscall1(SYS_PIPE,pipefd); }
/* 关闭目录dir */ int32_t closedir(struct dir* dir) { return _syscall1(SYS_CLOSEDIR, dir); }
//申请size字节大小的内存,并返回结果 void *malloc(uint32_t size){ return (void*)_syscall1(SYS_MALLOC,size); }
/* 删除目录pathname */ int32_t rmdir(const char* pathname) { return _syscall1(SYS_RMDIR, pathname); }
//输出一个字符 void putchar(char char_asci){ _syscall1(SYS_PUTCHAR,char_asci); }
int remove(const char *pathname) { return _syscall1(SYS_unlink,(int)pathname); }