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 fmtname(path): # static char buf[DIRSIZ+1]; # find first character after last slash # for(p=path+strlen(path); p >= path && *p != '/'; p--) # ; # p++; for i in range(strlen(path), 0, -1): if path[i - 1] == "/": break p = path[i:] # return blank-padded name if strlen(p) >= DIRSIZ: return p buf = p + " " * (DIRSIZ - strlen(p)) return buf
def parsecmd(st): s = 0 es = s + strlen(st) cmd, s = parseline(st, s, es) dummy, s = peek(st, s, es, "\0") if s != es: printf(2, "leftovers: %s\n", st) panic("syntax") # nulterminate(cmd) return cmd
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)