def wc(fd, name): l = 0 w = 0 c = 0 inword = False # while((n = read(fd, buf, sizeof(buf))) > 0) while True: n, buf = read(fd, 512) if n <= 0: break for i in range(n): c += 1 if buf[i] == "\n": l += 1 if strchr(" \r\t\n\v", buf[i]): inword = False elif not inword: w += 1 inword = True if n < 0: printf(1, "wc: read error\n") exit_(1) printf(1, "%d %d %d %s\n", l, w, c, name)
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 main(argc, argv): if argc < 2: ls(".") exit_() for i in range(1, argc): ls(argv[i]) exit_()
def main(argc, 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_()
def main(argc, argv): if argc < 2: # @@@ C source has < 1 printf(2, "usage: kill pid...\n") exit_() for i in range(1, argc): kill(atoi(argv[i])) exit_()
def main(argc, argv): if argc < 2: printf(2, "Usage: rm files...\n") exit_() for i in range(1, argc): if unlink(argv[i]) < 0: printf(2, "rm: %s failed to delete\n", argv[i]) break exit_()
def main(argc, argv): if argc < 2: printf(2, "Usage: mkdir files...\n") exit_() for i in range(1, argc): if mkdir(argv[i]) < 0: printf(2, "mkdir: %s failed to create\n", argv[i]) break exit_()
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 cat(fd): # while((n = read(fd, buf, sizeof(buf))) > 0) while True: n, buf = read(fd, 512) if n <= 0: break write(1, buf, n) if n < 0: printf(1, "cat: read error\n") # @@@ should that be 2? exit_(1)
def forktest(): printf(1, "fork test\n") for n in range(1000): pid = fork() if pid < 0: break if pid == 0: exit_() if n == 1000: printf(1, "fork claimed to work 1000 times!\n") exit_() while n > 0: if wait() < 0: printf(1, "wait stopped early\n") exit_(1) n -= 1 if wait() != -1: printf(1, "wait got too many\n") exit_(1) printf(1, "fork test OK\n")
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: 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 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): 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 fork() > 0: sleep(5) # Let child exit before parent exit_()
def main(argc, argv): forktest() exit_()
def exectest(): printf(stdout, "exec test\n") if exec_("echo", echoargv) < 0: printf(stdout, "exec echo failed\n") exit_()
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_()
def panic(s): printf(2, "%s\n", s) exit_()
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 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): for i in range(1, argc): printf(1, "%s%s", argv[i], " " if i + 1 < argc else "\n") exit_()