def main(): if open_("console", O_RDWR) < 0: mknod("console", 1, 1) open_("console", O_RDWR) dup(0) # stdout dup(0) # stderr while True: 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 True: wpid = wait() if wpid < 0 or wpid == pid: break print(1, "zombie!\n")
def opentest(): printf(stdout, "open test\n") fd = open_("echo", 0) if fd < 0: printf(stdout, "open echo failed!\n") exit_() close(fd) fd = open_("doesnotexist", 0) if fd >= 0: printf(stdout, "open doesnotexist succeeded!\n") exit_() printf(stdout, "open test ok\n")
def writetest1(): buf = "\0" * 2048 printf(stdout, "big files test\n") fd = open_("big", O_CREATE | O_RDWR) if fd < 0: printf(stdout, "error: creat big failed!\n") exit_() for i in range(MAXFILE): buf = chr(i % 256) + buf[1:] if write(fd, buf, 512) != 512: printf(stdout, "error: write big file failed\n", i) exit_() close(fd) fd = open_("big", O_RDONLY) if fd < 0: printf(stdout, "error: open big failed!\n") exit_() n = 0 while True: i, buf = read(fd, 512) if i == 0: if n == MAXFILE - 1: printf(stdout, "read only %d blocks from big", n) exit_() break elif i != 512: printf(stdout, "read failed %d\n", i) exit_() if buf[0] != chr(n % 256): printf(stdout, "read content of block %d is %d\n", n, buf[0]) exit_() n += 1 close(fd) if unlink("big") < 0: printf(stdout, "unlink big failed\n") exit_() printf(stdout, "big files ok\n")
def stat(name): fd = open_(name, O_RDONLY) if fd < 0: return -1 r, st = fstat(fd) close(fd) return r, st
def main(argv, argc): #static char buf[100]; #int fd; # Assumes three file descriptors open. # while((fd = open("console", O_RDWR)) >= 0){ while True: fd = open_("console", O_RDWR) if fd < 0: break if fd >= 3: close(fd) break # Read and run input commands. # while(getcmd(buf, sizeof(buf)) >= 0){ while True: n, buf = getcmd(100) if n < 0: break if buf[0] == "c" and buf[1] == "d" and buf[2] == " ": # Clumsy but will have to do for now. # Chdir has no effect on the parent if run in the child. buf = buf[:strlen(buf) - 1] # chop \n if chdir(buf[3:]) < 0: printf(2, "cannot cd %s\n", buf[3:]) continue fork1(runcmd, parsecmd(buf)) # wait() exit_()
def writetest(): printf(stdout, "small file test\n") fd = open_("small", O_CREATE | O_RDWR) if fd >= 0: printf(stdout, "creat small succeeded; ok\n") else: printf(stdout, "error: creat small failed!\n") exit_() for i in range(100): if write(fd, "aaaaaaaaaa", 10) != 10: printf(stdout, "error: write aa %d new file failed\n", i) exit_() if write(fd, "bbbbbbbbbb", 10) != 10: printf(stdout, "error: write bb %d new file failed\n", i) exit_() printf(stdout, "writes ok\n") close(fd) fd = open_("small", O_RDONLY) if fd >= 0: printf(stdout, "open small succeeded ok\n") else: printf(stdout, "error: open small failed!\n") exit_() i, buf = read(fd, 2000) if i == 2000: printf(stdout, "read succeeded ok\n") else: printf(stdout, "read failed\n") exit_() close(fd) if unlink("small") < 0: printf(stdout, "unlink small failed\n") exit_() printf(stdout, "small file test ok\n")
def main(argc, argv): if argc <= 1: wc(0, "") exit_() for i in range(1, argc): fd = open_(argv[i], 0) if fd < 0: printf(1, "wc: cannot open %s\n", argv[i]) # @@@ xv6 had 'cat' for 'wc' exit_() wc(fd, argv[i]) close(fd) exit_()
def main(argc, argv): printf(1, "usertests starting\n") if open_("usertests.ran", 0) >= 0: printf(1, "already ran user tests -- rebuild fs.img\n") exit_() close(open_("usertests.ran", O_CREATE)) opentest() writetest() writetest1() createtest() # mem() # pipe1() # preempt() # exitwait() # # rmdot() # fourteen() # bigfile() # subdir() # concreate() # linktest() # unlinkread() # createdelete() # twofiles() # sharedfd() # dirfile() # iref() # forktest() # bigdir() exectest() exit_()
def main(argc, argv): if argc <= 1: cat(0) exit_() for i in range(1, argc): fd = open_(argv[i], 0) if fd < 0: printf(1, "cat: cannot open %s\n", argv[i]) # @@@ should that be 2? exit_() cat(fd) close(fd) exit_()
def main(argc, argv): if argc <= 1: printf(2, "usage: grep pattern [file ...]\n") exit_() pattern = argv[1] if argc <= 2: grep(pattern, 0) exit_() for i in range(2, argc): fd = open_(argv[i], 0) if fd < 0: printf(1, "grep: cannot open %s\n", argv[i]) exit_() grep(pattern, fd) close(fd) exit_()
def ls(path): fd = open_(path, 0) if fd < 0: printf(2, "ls: cannot open %s\n", path) return n, st = fstat(fd) if n < 0: printf(2, "ls: cannot stat %s\n", path) close(fd) return if st.type == T_FILE: printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size) elif st.type == T_DIR: if strlen(path) + 1 + DIRSIZ + 1 > 512: printf(1, "ls: path too long\n") else: prefix = path + "/" #while (read(fd, &de, sizeof(de)) == sizeof(de)) while True: n, de = read(fd, 20) # @@@ 20 = sizeof(de) if n != 20: break de_inum = int(de[:6]) de_name = de[6:].strip() # @@@ if de_inum == 0: continue path = prefix + de_name n, st = stat(path) if n < 0: printf(1, "ls: cannot stat %s\n", path) continue printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size) close(fd)
def createtest(): printf(stdout, "many creates, followed by unlink test\n") name = [None, None, None] name[0] = "a" name[2] = "\0" for i in range(52): name[1] = chr(ord("0") + i) fd = open_("".join(name), O_CREATE | O_RDWR) close(fd) name[0] = "a" name[2] = "\0" for i in range(52): name[1] = chr(ord("0") + i) unlink("".join(name)) printf(stdout, "many creates, followed by unlink; ok\n")
def runcmd(cmd): p = [0, 0] if cmd == 0: exit_() if cmd.type == EXEC: ecmd = cmd if ecmd.argv[0] == 0: exit_() exec_(ecmd.argv[0], ecmd.argv) printf(2, "exec %s failed\n", ecmd.argv[0]) elif cmd.type == REDIR: rcmd = cmd close(rcmd.fd) if open_(rcmd.file, rcmd.mode) < 0: printf(2, "open %s failed\n", rcmd.file) exit_() runcmd(rcmd.cmd) elif cmd.type == LIST: lcmd = cmd # if fork1() == 0: # runcmd(lcmd.left) # wait() # runcmd(lcmd.right) fork1(runcmd, lcmd.left) wait() runcmd(lcmd.right) elif cmd.type == PIPE: pcmd = 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() elif cmd.type == BACK: bcmd = cmd if fork1() == 0: runcmd(bcmd.cmd) else: panic("runcmd") exit_()