home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / chasm2.zip / CHMOD.ASM < prev    next >
Assembly Source File  |  1986-03-08  |  13KB  |  370 lines

  1. ;================================================
  2. ; CHMOD    Version 2.0   3/8/86
  3. ;
  4. ;         by David Whitman
  5. ;
  6. ; Utility to examine and modify the read/write
  7. ; mode of a file under DOS 2.0.
  8. ;
  9. ; Syntax:
  10. ;
  11. ; CHMOD [d:][path] [filespec] [/N] [/R] [/S] [/H]
  12. ;
  13. ; If no file is specified, a help message is printed,
  14. ; summarizing the CHMOD command syntax.
  15. ;
  16. ; The attribute byte of the specified file is set
  17. ; according to the selected options:
  18. ;
  19. ;     /N - normal file
  20. ;     /R - read only
  21. ;     /S - system file
  22. ;     /H - hidden file
  23. ;
  24. ; Multiple options may be selected.  /N will cancel
  25. ; options preceding it in the command line.
  26. ;
  27. ; If no options are selected, a report on the current
  28. ; mode of the specified file is sent to the standard output.
  29. ;
  30. ; On exit, ERRORLEVEL is set to reflect the current file
  31. ; mode, and/or the success of the change, as follows:
  32. ;
  33. ;   normal file      --> 0
  34. ;   read only file   --> 1
  35. ;   hidden file      --> 2
  36. ;   system file      --> 4
  37. ;   failed operation --> 8
  38. ;
  39. ; Requires DOS 2.0, will abort under earlier versions.
  40. ;
  41. ; This source file is in CHASM assembler syntax.
  42. ;======================================================
  43.  
  44. ;==========
  45. ; EQUATES
  46. ;==========
  47.  
  48. @chmod     equ    43H        ;change file mode
  49. @dosver    equ    30H        ;get DOS version number
  50. @exit      equ    4CH        ;set ERRORLEVEL and exit
  51. @prnstr    equ    09H        ;print string
  52.  
  53. normal     equ    00H
  54. readonly   equ    01H
  55. hidden     equ    02H
  56. system     equ    04H
  57. failure    equ    08H
  58.  
  59. true       equ    0FFH       ;boolean values
  60. false      equ    00H
  61.  
  62. cr         equ    0DH        ;carriage return
  63. lf         equ    0AH        ;line feed
  64. beep       equ    07H        ;bell
  65.  
  66. param_area  equ   [81H]      ;unformatted parameter area
  67. param_count equ   [80H]      ;number of characters in above
  68.  
  69. ;===========================================================
  70.  
  71. chmod      proc   near
  72.            call   chkdos              ;test for proper DOS
  73.            call   parsefile           ;parse path/filename
  74.            call   options             ;parse options, set flags
  75.            call   doit                ;perform specified action
  76.            call   cleanup             ;final processing, exit
  77.            call   set_errlev          ;set errorlevel value in AL
  78.            mov    ah, @exit           ;and exit
  79.            int    21H
  80.            endp
  81.  
  82. ;=================================================
  83. ; SUBROUTINE CHKDOS
  84. ; Checks for proper DOS, exits if not 2.0 or above
  85. ;=================================================
  86. chkdos     proc   near
  87.            mov    ah, @dosver         ;get dos version number
  88.            int    21H                 ;with dos call
  89.            cmp    al, 2               ;2.0 or over?
  90.            jae    a1                  ;yes, skip
  91.  
  92.            mov    ah, @prnstr         ;no, bitch
  93.            mov    dx, offset(baddos)  ;point to message
  94.            int    21H                 ;and print it
  95.            pop    ax                  ;reset stack
  96.            int    20H                 ;and exit
  97. a1
  98.            ret
  99.  
  100. baddos     db     beep, cr, lf, 'This program requires DOS 2.0!' cr, lf, '$'
  101.            endp
  102.  
  103. ;===================================================
  104. ; SUBROUTINE PARSEFILE
  105. ; Scans the parameter area for a path/filename.
  106. ; Sets PATHPTR to point to the name, and terminates
  107. ; it with a 0, to make an ASCIIZ string.
  108. ;
  109. ; If no name is found, ERRORLEVEL is set to 8,
  110. ; and control is passed back to DOS.
  111. ;===================================================
  112. parsefile  proc   near
  113.  
  114.            xor    ch,ch               ;cx <== # of parameter characters
  115.            mov    cl, param_count     ;    "
  116.            mov    di, offset(param_area)
  117.            mov    al, ' '             ;search for first non-blank
  118.            rep
  119.            scasb
  120.            jcxz   berror              ;nothing? bitch
  121.  
  122.            dec    di                  ;back up to character found
  123.            inc    cx                  ; "
  124.            cmpb   [di], '/'           ;are we into the options?
  125.            jne    b1                  ;no, skip
  126. berror     mov    ah, @prnstr         ;yes, print help message
  127.            mov    dx, offset(nofile)
  128.            int    21H
  129.            pop    ax                  ;reset stack
  130.            mov    ah, @exit           ;set error level and exit
  131.            mov    al, failure
  132.            int    21H
  133.  
  134. b1
  135.            mov    pathptr, di         ;otherwise point to pathname
  136.  
  137.            repne                      ;now search for next blank
  138.            scasb
  139.            jcxz   b2                  ;reached end of data?
  140.            dec    di                  ;nope, back up to last non-blank
  141.            inc    cx                  ; "
  142. b2         movb   [di], 00H           ;ASCIIZ string terminator
  143.            ret
  144.  
  145. pathptr    dw   0000H                 ;pointer to path/filename
  146. nofile     db   cr, lf
  147.            db   'CHMOD version 2.0' cr lf
  148.            db   cr lf
  149.            db   'Syntax:  CHMOD [d:][path] [filespec] [/N] [/R] [/S] [/H]'
  150.            db   cr lf cr lf
  151.            db   'The attribute byte of the specified file is set' cr lf
  152.            db   'according to the selected options:' cr lf
  153.            db   cr lf
  154.            db   '     /N - normal file' cr lf
  155.            db   '     /R - read only' cr lf
  156.            db   '     /S - system file' cr lf
  157.            db   '     /H - hidden file' cr lf
  158.            db   cr lf
  159.            db   'Multiple options may be selected.' cr lf
  160.            db   cr lf
  161.            db   'If no options are specified, a report of the current' cr lf
  162.            db   'attributes is printed, and ERRORLEVEL is set to the' cr lf
  163.            db   'value of the attribute byte.' cr lf
  164.            db   '$'
  165.            endp
  166. ;=====================================================
  167. ; SUBROUTINE OPTIONS
  168. ; Scans the command line for options, and builds
  169. ; the new attribute byte in NEWATTRIB.
  170. ;
  171. ; If options are found, OPFOUND is set true, otherwise
  172. ; it remains false.
  173. ;======================================================
  174. options    proc near
  175.                                       ;cx still has number of chars. left,
  176.                                       ;di points to current position.
  177.  
  178. c1         mov   al, '/'              ;options marked with '/'
  179.            repnz                      ;scan for options
  180.            scasb
  181.            jcxz  cexit                ;reached end
  182.  
  183.            mov   al, [di]             ;get option character
  184.            and   al, 0DFH             ;guarantees upper case
  185.  
  186.            cmp   al, 'N'              ;normal file?
  187.            jne   c2                   ;no, skip
  188.            movb  newattrib, normal    ;yes, clear all bits
  189.            movb  opfound, true        ;set found flag
  190.            jmps  c1                   ;and loop
  191.  
  192. c2         cmp   al, 'R'              ;read only?
  193.            jne   c3                   ;no, skip
  194.            orb   newattrib, readonly  ;yes, set option flag
  195.            movb  opfound, true        ;set found flag
  196.            jmps  c1                   ;and loop
  197.  
  198. c3         cmp   al, 'S'              ;system?
  199.            jne   c4                   ;no, skip
  200.            orb   newattrib, system    ;yes, set option flag
  201.            movb  opfound, true        ;set found flag
  202.            jmps  c1                   ;and loop
  203.  
  204. c4         cmp   al, 'H'              ;hidden?
  205.            jne   c1                   ;no, just loop
  206.            orb   newattrib, hidden    ;yes, set option flag
  207.            movb  opfound, true        ;set found flag
  208.            jmps  c1                   ;and loop
  209.  
  210. cexit      ret
  211.  
  212. newattrib  db    00H                  ;options selected - new attribute byte
  213. opfound    db    00H                  ;if non-zero, an option was decoded
  214.            endp
  215.  
  216. ;========================================================
  217. ; SUBROUTINE DOIT
  218. ; Does the actual work.  Either sets new file attribute,
  219. ; or reads the existing attribute.
  220. ;========================================================
  221. doit       proc  near
  222.                                       ;read the old attributes
  223.            mov   dx, pathptr          ;point DX at ASCIIZ path/filename
  224.            mov   al, 00H              ;read file mode
  225.            mov   ah, @chmod           ;specify CHMOD call
  226.            int   21H                  ;call dos
  227.            jc    derr                 ;carry flag means error
  228.            and   cl, 07H              ;OK? mask off high bits
  229.            mov   oldattrib, cl        ;save attribute byte
  230.  
  231.            cmpb  opfound, true        ;were there any options?
  232.            jne   dexit                ;no? exit
  233.                                       ;yes, reset attributes
  234.            mov   dx, pathptr          ;point DX at ASCIIZ path/filename
  235.            xor   ch, ch               ;CX <== new attribute byte
  236.            mov   cl, newattrib        ; "
  237.            mov   al, 01H              ;set file mode
  238.            mov   ah, @chmod           ;specify CHMOD call
  239.            int   21H                  ;call dos
  240.            jc    derr                 ;carry flag means error
  241.            jmps  dexit                ;otherwise done
  242.  
  243. derr       cmp   al, 02H              ;file not found error?
  244.            jne   d3
  245.            mov   dx, offset(fnferror)
  246.            jmps  dabort
  247.  
  248. d3         cmp   al, 03H              ;path not found error?
  249.            jne   d4
  250.            mov   dx, offset(pnferror)
  251.            jmps  dabort
  252.  
  253. d4         cmp   al, 05H              ;access denied error?
  254.            jne   dabort
  255.            mov   dx, offset(aderror)
  256.            jmps  dabort
  257.  
  258.            mov   dx, offset(unkerror) ;not matched? panic
  259.  
  260. dabort     mov   ah, @prnstr          ;print the error message
  261.            int   21H
  262.            pop   ax                   ;reset stack
  263.            mov   al, failure          ;set errorlevel
  264.            mov   ah, @exit            ;and abort to dos
  265.            int   21H
  266.  
  267. dexit      ret                        ;normal return
  268.  
  269. oldattrib  db    00H
  270. fnferror   db    cr, lf, 'File not found.' cr, lf, '$'
  271. pnferror   db    cr, lf, 'Path not found.' cr, lf, '$'
  272. aderror    db    cr, lf, 'Access denied.'  cr, lf, '$'
  273. unkerror   db    cr, lf, 'CHMOD internal error.' cr, lf, '$'
  274.            endp
  275.  
  276. ;==================================================
  277. ; SUBROUTINE CLEANUP
  278. ; Reports old and new attributes.
  279. ;==================================================
  280. cleanup    proc  near
  281.            mov   ah, @prnstr          ;print crlf
  282.            mov   dx, offset(newline)
  283.            int   21H
  284.  
  285.            cmpb  opfound, true        ;were options specified?
  286.            jne   p_current            ;no, so just report current settings
  287.  
  288.            mov   ah, @prnstr          ;yes, report old attributes...
  289.            mov   dx, offset(oldheader)
  290.            int   21H
  291.            mov   bl, oldattrib
  292.            call  print_att
  293.  
  294.            mov   ah, @prnstr          ;...and the new ones
  295.            mov   dx, offset(newheader)
  296.            int   21H
  297.            mov   bl, newattrib
  298.            call  print_att
  299.            jmps  clean_exit
  300.  
  301. p_current  mov   ah, @prnstr          ;no options, just report current
  302.            mov   dx, offset(newheader)
  303.            int   21H
  304.            mov   bl, oldattrib
  305.            call  print_att
  306.  
  307. clean_exit ret
  308. newline    db    cr lf '$'
  309. oldheader  db    'Previous Attributes:  $'
  310. newheader  db    'Current Attributes:   $'
  311.            endp
  312.  
  313. ;===========================================
  314. ; PRINT_ATT
  315. ;
  316. ; Decodes an attribute byte in BL and prints
  317. ; a summary.
  318. ;===========================================
  319. print_att  proc near
  320.            mov   ah, @prnstr
  321.            test  bl, readonly         ;read only?
  322.            jz    e1
  323.            mov   dx, offset(roattrib) ;print attribute message
  324.            int   21H
  325.  
  326. e1         test  bl, system           ;system?
  327.            jz    e2
  328.            mov   dx, offset(sattrib)  ;print attribute message
  329.            int   21H
  330.  
  331. e2         test  bl, hidden           ;hidden?
  332.            jz    e3
  333.            mov   dx, offset(hattrib)  ;print attribute message
  334.            int   21H
  335.  
  336.            ;note: a "normal" byte has none of the bits set,
  337.            ;so we use CMP rather than TEST
  338. e3         cmp   bl, normal           ;normal?
  339.            jne   eexit
  340.            mov   dx, offset(nattrib)  ;print attribute message
  341.            int   21H
  342.  
  343. eexit      mov   dx, offset(eolstr)   ;terminate line
  344.            int   21H
  345.            ret
  346.  
  347. roattrib   db            'Read only  $'
  348. sattrib    db            'System  $'
  349. hattrib    db            'Hidden  $'
  350. nattrib    db            'Normal  $'
  351. eolstr     db            cr lf cr lf '$'
  352.            endp
  353.  
  354. ;===========================================
  355. ; SET_ERRLEV
  356. ;
  357. ; Moves an attribute value into AL
  358. ; for output in ERRORLEVEL.  If options were
  359. ; specified, the new attribute byte is used,
  360. ; otherwise the existing value is used.
  361. ;============================================
  362. set_errlev proc   near
  363.            cmpb   opfound, true       ;options found?
  364.            jne    se1                 ;no, skip
  365.            mov    al, newattrib       ;yes, use new attributes
  366.            jmps   se_exit
  367. se1        mov    al, oldattrib       ;no, use old attributes
  368. se_exit    ret
  369.            endp
  370.