home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 283 / Shell5SourceCode.sis / du (.txt) < prev    next >
Encoding:
EPOC OPL Source  |  1998-08-30  |  3.7 KB  |  157 lines

  1.  
  2.  
  3. Rem   du, an addon command for Shell5
  4. Rem   Display the usage of a filesystem by directory
  5. Rem
  6. Rem   Copyright (C) 1998  Nick Murray
  7. Rem
  8. Rem   This program is free software; you can redistribute it and/or
  9. Rem   modify it under the terms of the GNU General Public License
  10. Rem   as published by the Free Software Foundation; either version 2
  11. Rem   of the License, or (at your option) any later version.
  12. Rem
  13. Rem   This program is distributed in the hope that it will be useful,
  14. Rem   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. Rem   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. Rem   See the GNU General Public License for more details.
  17. Rem
  18. Rem   You should have received a copy of the GNU General Public License
  19. Rem   along with this program; if not, write to the Free Software Foundation,
  20. Rem   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21. Rem
  22. PROC du%:(n%)
  23. GLOBAL du_bad_count%
  24. LOCAL i%,buf$(255),ret%,flag%,tot&
  25.     ONERR ErrTrap::
  26.     i%=2
  27.     WHILE i%<=n%
  28.         buf$=PEEK$(argv&(i%))
  29.         IF buf$="-s"
  30.             flag%=1
  31.         ELSE
  32.             BREAK
  33.         ENDIF
  34.         i%=i%+1
  35.     ENDWH
  36.     IF i%>n%    Rem no directories given
  37.         buf$=_cwd$
  38.     ENDIF
  39.     DO
  40.         du_bad_count%=0
  41.         ret%=Fparse%:(ADDR(buf$),buf$)
  42.         IF ret%<0
  43.             PRINT PrPath$:(buf$),"-",err$:(ret%)
  44.             RETURN
  45.         ELSEIF ret% AND 16
  46.             tot&=_du&:(buf$,flag%)
  47.             IF flag%        Rem NOTE: this IS printed when ESC breaks the recursion
  48.                 fprint%:(NUM$(tot&,-9)+" "+PrPath$:(buf$))
  49.             ENDIF
  50.         ENDIF
  51.         IOYIELD
  52.         IF _stat%<>-46
  53.             IF _key%(1)=27
  54.                 BREAK
  55.             ELSE
  56.                 KEYA(_stat%,_key%())
  57.             ENDIF
  58.         ENDIF
  59.         i%=i%+1
  60.         IF i%<=n%
  61.             buf$=PEEK$(argv&(i%))
  62.         ENDIF
  63.         IF du_bad_count%
  64.             PRINT "Warning:",du_bad_count%,"files were open and aren't included in the total."
  65.         ENDIF
  66.     UNTIL i%>n%
  67.     RETURN
  68. ErrTrap::
  69.     ONERR off
  70.     PRINT err$:(ERR)
  71.     RETURN ERR
  72. ENDP
  73.  
  74. PROC _du&:(d$,flag%)
  75. LOCAL i$(255),ret%,tot&,p&,base&,h%,Fsize&,q&
  76. Rem this must be redesigned for psion5 as we can't use IOOPEN/IOW to read
  77. Rem directories. Instead build a linked list of contents of the directory and then
  78. Rem parse one entry at a time - when DIR$ will be safe again
  79.     ONERR ErrTrap::
  80. Rem MUST flag that an error has occured and unroll all alloc "further down"
  81. Rem    PRINT "Called:",d$
  82.     base&=ALLOC(16)
  83. Rem    base&=ALLOC&:(16,"du1")
  84.     IF base&=0
  85.         RAISE -10        Rem must catch this as we "collapse" down
  86.     ENDIF
  87.     p&=base&
  88.     POKEL p&,0
  89.     i$=DIR$(d$)
  90.     WHILE LEN(i$)
  91.         POKEL p&,ALLOC((LEN(i$)+20) AND $FFF0)
  92. Rem        POKEL p&,ALLOC&:((LEN(i$)+20) AND $FFF0,"du2")
  93.         Rem LEN + 1 + 15 + space for pointer
  94.         IF PEEKL(p&)=0
  95.             RAISE -10
  96.         ENDIF
  97.         p&=PEEKL(p&)
  98.         POKEL p&,0
  99.         POKE$ p&+4,i$
  100. Rem        PRINT "Storing",i$,p&
  101.         i$=DIR$("")
  102.         IOYIELD
  103.         IF _stat%<>-46
  104.             IF _key%(1)=27
  105.                 GOTO Cleanup::
  106.             ELSE
  107.                 KEYA(_stat%,_key%())
  108.             ENDIF
  109.         ENDIF
  110.     ENDWH
  111.     p&=PEEKL(base&)
  112.     WHILE p&
  113.         ret%=IOOPEN(h%,PEEK$(p&+4),$0600)    Rem read-only, shared, random access
  114. Rem        PRINT PEEK$(p&+4),ret%
  115.         IF ret%=0            Rem files
  116.             Fsize&=0
  117.             IOSEEK(h%,2,Fsize&)
  118.             tot&=tot&+Fsize&
  119.             IOCLOSE(h%)
  120.         ELSEIF ret%=-9            Rem file open
  121.             du_bad_count%=du_bad_count%+1
  122.         ELSE                                Rem -33 is a directory in Z!!
  123.             tot&=tot&+_du&:(PEEK$(p&+4)+"\",flag%)
  124.         ENDIF
  125.         p&=PEEKL(p&)
  126.         IOYIELD
  127.         IF _stat%<>-46
  128.             IF _key%(1)=27
  129.                 GOTO Cleanup::
  130.             ELSE
  131.                 KEYA(_stat%,_key%(1))
  132.             ENDIF
  133.         ENDIF
  134.     ENDWH
  135.     IF flag%=0
  136.         fprint%:(NUM$(tot&,-9)+" "+PrPath$:(d$))
  137.     ENDIF
  138.     GOTO Cleanup::
  139. ErrTrap::
  140.     ONERR off
  141.     tot&=ERR                    Rem flag error (will be <0)
  142. Cleanup::
  143.     p&=PEEKL(base&)
  144.     WHILE p&
  145. Rem        PRINT p&,"Freeing:",PEEK$(p&+4)
  146.         q&=PEEKL(p&)
  147.         FREEALLOC(p&)
  148. Rem        FREEALLOC&:(p&)
  149.         p&=q&
  150.     ENDWH
  151.     FREEALLOC(base&)
  152. Rem    FREEALLOC&:(base&)
  153.     RETURN tot&
  154. ENDP
  155.  
  156.  
  157.