From 10befe6cc49820eb380df268f017e4c8b28cd775 Mon Sep 17 00:00:00 2001 From: Warren Toomey Date: Fri, 4 Mar 2016 21:56:46 +1000 Subject: [PATCH] Warren's ls now has -l argument which prints out lines in long format! --- src/other/wktls.s | 209 ++++++++++++++++++++++++++++++++++++++++------ tools/a7out | 2 +- 2 files changed, 186 insertions(+), 25 deletions(-) diff --git a/src/other/wktls.s b/src/other/wktls.s index f8dba88..d2b36e1 100644 --- a/src/other/wktls.s +++ b/src/other/wktls.s @@ -1,33 +1,130 @@ -" Warren's version of ls. Simply print out the names in the current directory +" Warren's version of ls. ls [-l] +" +" When -l is used, you see +" +" inum [dls][r-][w-][r-][w-] nlink uid size name +" +" with numbers in octal. All filenames need to be 8 characters +" long or less, or ls will crash. main: - sys open; curdir; 0 " Open up the currect directory + + lac 017777 i " Load the pointer pointer in 017777 + sad d4 " to see if we have any arguments + jmp 1f " No arguments, so no long option + lac fd1 + dac longopt " Yes an argument, set the long option +1: + sys open; curdir; 0 " Open up the current directory spa - sys exit " Unable, so die now - dac fd " Save the fd + sys exit " Unable, so die now + dac fd " Save the fd fileloop: - " Read 64 words into the buffer from the input file - lac fd + lac fd " Read 64 words into the buffer from the input file sys read; buf; 64 spa " Skip if result was >= 0 jmp fileend " Result was -ve, so error result sna " Skip if result was >0 jmp fileend " Result was zero, so nothing left to read - " Save the count of words read in - dac count + dac count " Save the count of words read in lac ibufptr " Point bufptr at the base of the buffer dac bufptr " Each directory entry is eight words. We need to print out -" the filename which is in words 2 to 5. -printloop: +" the filename which is in words 2 to 5. Word 1 is the inum. +entryloop: + lac longopt " Are we printing out in long format? + sna + jmp 1f " No, don't print out the inode number - isz bufptr " Move up to the filename - lac d1 - sys write; bufptr:0; 4 " Write a filename out to stdout - lac d1 + lac bufptr " Print out the inode number as 5 digits + jms octal; -5 + +1: isz bufptr " Move up to the filename + lac longopt " Are we printing out in long format? + sna + jmp printname " No, jump to printname + + lac bufptr + dac statfile " Copy the pointer to the status call + lac statbufptr " Get the file's details into the statbuf + sys status; statfile:0; 0 + spa + jms fileend + + " Ugly code. Improvements welcome! + lac s.perm " See if this is a directory + and isdirmask + sna + jmp 1f + lac fd1 + sys write; d; 1 " Yes, print a d + jmp 2f +1: lac s.perm " Not a dir, see if its a large file + and largemask + sna + jmp 1f + lac fd1 + sys write; l; 1 " Yes, print an l + jmp 2f +1: lac fd1 + sys write; minus; 1 " Not a dir, not large, print an s + +2: lac s.perm " Readable by owner? + and ureadmask + sna + jmp 1f + lac fd1 +1: sys write; r; 1 " Yes, print an r + jmp 2f + sys write; minus; 1 " No, print a - sign + +2: lac s.perm " Writable by owner? + and uwritemask + sna + jmp 1f + lac fd1 + sys write; w; 1 " Yes, print a w + jmp 2f +1: lac fd1 + sys write; minus; 1 " No, print a - sign + +2: lac s.perm " Readable by other? + and oreadmask + sna + jmp 1f + lac fd1 + sys write; r; 1 " Yes, print an r + jmp 2f +1: lac fd1 + sys write; minus; 1 " No, print a - sign + +2: lac s.perm " Writable by other? + and owritemask + sna + jmp 1f + lac fd1 + sys write; w; 1 " Yes, print a w + jmp 2f +1: lac fd1 + sys write; minus; 1 " No, print a - sign + +2: lac fd1 + sys write; space; 1 " Print a space + + lac s.nlinks " Print the number of links out + jms octal; -2 + lac s.uid " Print the user-id out + jms octal; -3 + lac s.size " Print the size out + jms octal; -5 + +printname: + lac fd1 + sys write; bufptr:0; 4 " Write the filename out to stdout + lac fd1 sys write; newline; 1 " followed by a newline lac bufptr " Add 7 to the bufptr @@ -37,23 +134,87 @@ printloop: tad count " Decrement the count of words by 8 dac count sza " Anything left in the buffer to print? - jmp printloop " Yes, stuff left to print + jmp entryloop " Yes, stuff left to print jmp fileloop " Nothing in the buffer, try reading some more fileend: - " Close the open file descriptor and exit - lac fd + lac fd " Close the open file descriptor and exit sys close sys exit - -curdir: <. 040; 040040; 040040; 040040 " i.e. "." -newline: 012000 -fd: 0 -d1: 1 " stdout fd + " Octal print code: This code borrowed from ds.s +octal: 0 + lmq " Move the negative argument into the MQ + " as we will use shifting to deal with the + " number by shifting groups of 3 digits. + + lac d5 " By adding 5 to the negative count and + tad octal i " complementing it, we set the actual + cma " loop count up to 6 - count. So, if we + dac c " want to print 2 digits, we lose 6 - 2 = 4 digits +1: + llss 3 " Lose top 3 bits of the MQ + isz c " Do we have any more to lose? + jmp 1b " Yes, keep looping + lac octal i " Save the actual number of print digits into c + dac c " as a negative number. +1: + cla + llss 3 " Shift 3 more bits into AC + tad o60 " Add AC to ASCII '0' + dac cbuf " and print out the digit + lac fd1 + sys write; cbuf; 1 + isz c " Any more characters to print out? + jmp 1b " Yes, loop back + lac fd1 " Print out a space + sys write; space; 1 + isz octal " Move return address 1 past the argument + jmp octal i " and return from subroutine + +longopt: 0 " User set the -l option when this is 1 +fd: 0 " File descriptor for the directory +fd1: 1 " File descriptor 1 +d4: 4 +d5: 5 d7: 7 -count: 0 +o40: 040 +o60: 060 +count: 0 " Count of # of directory words read in +cbuf: 0 " Used to print out in the octal routing +c: .=.+1 " Loop counter for printing octal digits " Input buffer for read ibufptr: buf " Constant pointer to the buffer -buf: .=.+64 +buf: .=.+64 " Directory buffer +statbufptr: statbuf " Pointer to the statbuf +statbuf: " Status buffer fields below +s.perm: 0 +s.blk1: 0 +s.blk2: 0 +s.blk3: 0 +s.blk4: 0 +s.blk5: 0 +s.blk6: 0 +s.blk7: 0 +s.uid: 0 +s.nlinks: 0 +s.size: 0 +s.uniq: 0 +largemask: 200000 " large file, bigger than 4096 words +isdirmask: 000020 " is a directory +ureadmask: 000010 " user read +uwritemask: 000004 " user write +oreadmask: 000002 " other read +owritemask: 000001 " other write + +d: 0144 " ASCII characters: d, l, s, r, w, -, space, \n +l: 0154 +s: 0163 +r: 0162 +w: 0167 +minus: 055 +space: 040 +newline: 012 + +curdir: <. 040; 040040; 040040; 040040 " i.e. "." diff --git a/tools/a7out b/tools/a7out index 0d6a9c1..1b90832 100755 --- a/tools/a7out +++ b/tools/a7out @@ -717,7 +717,7 @@ sub sys_close { # That filehandle is not open, set an error -1 in octal if ( !defined( $FD[$fd] ) ) { - dprint("close: fd $fd is not open\n"); + dprintf("close: fd $fd is not open\n"); $AC = MAXINT; return; }