home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / OS2ASM.ZIP / IO.ASM < prev    next >
Assembly Source File  |  1991-08-09  |  9KB  |  470 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; C compiler system calls for Phar Lap DOS386.
  3. ;Copyright (C) 1985-1990 by Walter Bright
  4. ;All Rights Reserved
  5. ;
  6. ; Modified by Joe Huffman September 17, 1990
  7.  
  8. include macros.asm
  9.  
  10. ;Open flags
  11. O_RDONLY    equ    0
  12. O_WRONLY    equ    1
  13. O_RDWR        equ    2
  14. O_APPEND    equ    8
  15. O_NOINHERIT    equ    80h
  16. O_CREAT        equ    100h
  17. O_TRUNC        equ    200h
  18. O_EXCL        equ    400h
  19.  
  20. ;Error numbers
  21. ENOENT        equ    2
  22. EINVAL        equ    22
  23. EEXIST        equ    80
  24.  
  25. ;Permission modes
  26. S_IWRITE    equ    80h
  27. S_IREAD        equ    0100h
  28.  
  29.     begdata
  30.     c_extrn errno,dword
  31.     c_extrn _osmajor,byte
  32.     enddata
  33.  
  34.     begcode io
  35.  
  36.     c_public read,write,open,sopen,close,creat
  37.     c_public lseek,filesize,rename,unlink
  38.  
  39.     c_public isatty,remove,dos_open,dos_creat
  40.  
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;
  42. ; Read data from a file.
  43. ; Use:
  44. ;    read(fd,buffer,length)
  45. ; Returns:
  46. ;    -1    error
  47. ;    0    end of file
  48. ;    n    number of bytes actually read
  49. ;
  50.  
  51. func    read
  52.     mov    AH,3Fh            ;read from file or device
  53. F4:    push    EBP
  54.     mov    EBP,ESP
  55.     uses    <EBX,ECX,EDX>
  56.     mov    EBX,P[EBP]        ;EBX = fd (file handle)
  57.     mov    EDX,P+4[EBP]        ;EDX = buffer address
  58.     mov    ECX,P+4+SIZEPTR[EBP]    ;ECX = number of bytes to read/write
  59.     bdos                ;read/write from/to file or device
  60.     unuse    <EDX,ECX,EBX>
  61.  
  62. F2:    jnc    F1            ;no error
  63.     movsx    EAX,AX
  64.     mov    errno,EAX        ;save DOS error number
  65.     sbb    EAX,EAX            ;error
  66.  
  67. F1:    pop    EBP
  68.     ret
  69. c_endp    read
  70.  
  71.  
  72. ;;;;;;;;;;;;;;;;;;;;;;;;;
  73. ; Write data to a file.
  74. ; Use:
  75. ;    int write(fd,buffer,length)
  76. ; Returns:
  77. ;    -1    error
  78. ;    n    number of bytes actually written
  79. ;
  80.  
  81. func    write
  82.     mov    AH,40h            ;write to file or device
  83.     jmps    F4
  84. c_endp    write
  85.  
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ; Open a file (DOS mode)
  88. ; Use:
  89. ;    int dos_open(name,rwmode)
  90. ; Returns:
  91. ;    -1    error
  92. ;    fd    file handle
  93. ;
  94.  
  95. func    dos_open
  96.     push    EBP
  97.     mov    EBP,ESP
  98.     mov    AH,3Dh            ;open file
  99.     mov    AL,P+SIZEPTR[EBP]    ;AL = rwmode (0,1,2)
  100. OPEN1:
  101.     mov    EDX,P[EBP]         ;EDX -> name
  102.     bdos
  103.     jmp    F2
  104. c_endp    dos_open
  105.  
  106. ;;;;;;;;;;;;;;;;;;;;;;;;;
  107. ; Open a file
  108. ; Use:
  109. ;    int open(name,rwmode,pmode)
  110. ; Returns:
  111. ;    -1    error
  112. ;    fd    file handle
  113. ;
  114.  
  115. rwmode_data    equ    -4[EBP]
  116. pmode_data    equ    -8[EBP]
  117.  
  118. func    open
  119.     push    EBP
  120.     mov    EBP,ESP
  121.     sub    ESP,8
  122.     uses    <EBX>
  123.     mov    EAX,P+SIZEPTR[EBP]    ;EAX = rwmode (0,1,2)
  124.     and    AL,8Fh            ;Mask off sharing bits...
  125. ;    or    AL,40h            ; add sharing (deny none)...
  126.     _if    _osmajor ge 3, DO4    ;if DOS 3+?
  127.     and    AL,7            ;only these bits have meaning
  128. DO4:    mov    rwmode_data,EAX        ;  and save it
  129.     mov    EAX,P+4+SIZEPTR[EBP]    ;EAX = pmode
  130.     mov    pmode_data,EAX
  131.  
  132. opens:
  133.     if 1
  134.     mov    AL,rwmode_data        ;AL = rwmode (0,1,2)
  135.     and    AL,0F3h            ;clear reserved bits
  136.     mov    AH,3Dh            ;open file
  137.     else
  138.     mov    AX,3D00h        ;see if file exists
  139.     endif
  140.     mov    EDX,P[EBP]        ;filename offset
  141.     bdos
  142.     jc    nofile            ;maybe doesn't exist
  143.  
  144.     mov    EBX,EAX            ;opened file checking existance,
  145.     bdos    3Eh            ; now close it
  146.     mov    EBX,rwmode_data        ;check for O_EXCL && O_CREAT
  147.     and    EBX,O_EXCL+O_CREAT
  148.     cmp    EBX,O_EXCL+O_CREAT
  149.  
  150.     jne    gotfile            ;all's OK - continue
  151.     mov    EAX,EEXIST        ;return EEXIST error
  152.  
  153. DOX:    stc
  154. DOXX:    unuse    <EBX>
  155.     mov    ESP,EBP
  156.     jmp    F2
  157.  
  158. nofile:
  159.     _ifs    AX ne ENOENT, DOX    ;if not "file not found" error
  160.     test    word ptr rwmode_data,O_CREAT ;check for O_CREAT
  161.     jz    DOX            ;no, return ENOENT
  162.     clr    ECX
  163.     test    byte ptr pmode_data,S_IWRITE ;Write permission?
  164.     jnz    DO2            ;Yes, continue
  165.     inc    ECX            ;No, set FA_RDONLY
  166. DO2:
  167.     mov    EDX,P[EBP]        ;EDX -> name
  168.     bdos    3ch            ;create file
  169.     jc    DOXX            ;error
  170.  
  171.     test    byte ptr rwmode_data,S_IWRITE ;Write permission?
  172.     jz    DOXX            ;no, return as it is
  173.  
  174.     mov    EBX,EAX
  175.     bdos    3Eh            ;file is created, now close it
  176.  
  177.     jc    DOXX
  178. gotfile:
  179.     mov    AL,rwmode_data        ;AL = rwmode (0,1,2)
  180.     and    AL,0F3h
  181.     mov    AH,3Dh            ;open file
  182.  
  183.     mov    EDX,P[EBP]        ;EDX -> name
  184.     bdos
  185.     jc    DOXX
  186.  
  187.     clr    ECX
  188.     movzx    EBX,AX            ;fd
  189.     test    word ptr rwmode_data,O_TRUNC ;Truncate?
  190.     jz    notrunc            ;No, skip it
  191.  
  192.     ;Truncate it by writing 0 bytes to file
  193.     bdos    40h            ;write
  194.     jc    DOXX
  195.     jmps    noappend
  196.  
  197. notrunc:
  198.     test    word ptr rwmode_data,O_APPEND ;Append?
  199.     jz    noappend        ;No, skip it
  200.  
  201.     mov    EDX,ECX            ;offset is 0L
  202.     mov    AX,4202h        ;use SEEK_END
  203.     bdos
  204.     jc    DOXX
  205.  
  206. noappend:
  207.     movzx    EAX,BX            ;return fd
  208.     unuse    <EBX>
  209.     mov    ESP,EBP
  210.     pop    EBP
  211.     ret
  212. c_endp    open
  213.  
  214. func    sopen
  215.     push    EBP
  216.     mov    EBP,ESP
  217.     sub    ESP,8
  218.     uses    <EBX>
  219.  
  220.     _if    _osmajor ge 3, SO1    ;DOS 3+?
  221.     mov    EAX,EINVAL
  222.     jmp    DOX
  223. SO1:
  224.     mov    EAX,P+SIZEPTR[EBP]    ;EAX = rwmode (0,1,2)
  225.     and    AL,8fh
  226.     mov    ECX,P+4+SIZEPTR[EBP]    ;Add sharing bits...
  227.     and    CL,70h
  228.     or    AL,CL
  229.     mov    -4[EBP],AX        ; and save it
  230.     mov    EAX,P+8+SIZEPTR[EBP]    ;EAX = pmode
  231.     mov    -8[EBP],EAX
  232.     jmp    opens
  233. c_endp    sopen
  234.  
  235. ;;;;;;;;;;;;;;;;;;;;;;;;;
  236. ; Create a file
  237. ; Use:
  238. ;    int creat(name,pmode)
  239. ; Returns:
  240. ;    -1    error
  241. ;    fd    file handle
  242. ;
  243.  
  244. func    creat
  245.     push    EBP
  246.     mov    EBP,ESP
  247.     movzx    ECX,byte ptr P+SIZEPTR[EBP]
  248.     rol    CL,1            ;put S_IWRITE in bit 0
  249.     inc    CL            ;toggle it
  250.     and    ECX,1            ;turning it into read-only bit
  251.     jmps    creat1
  252. c_endp    creat
  253.  
  254. func    dos_creat
  255.     push    EBP
  256.     mov    EBP,ESP
  257.     mov    ECX,P+SIZEPTR[EBP]    ;ECX = file attribute
  258. creat1:
  259.     mov    AH,3Ch            ;create file
  260.     mov    EDX,P[EBP]        ;EDX -> name
  261.     bdos
  262.     movzx    EAX,AX
  263.  
  264.     jnc    short creat4        ;no error
  265.     mov    errno,EAX        ;save DOS error number
  266.     sbb    EAX,EAX            ;error
  267. creat4: pop    EBP
  268.     ret
  269. c_endp    dos_creat
  270.  
  271. ;;;;;;;;;;;;;;;;;;;;;;;;;
  272. ; Close a file
  273. ; Use:
  274. ;    int close(fd)
  275. ; Returns:
  276. ;    -1    error
  277. ;    0    successful
  278. ;
  279.  
  280. func    close
  281.     push    EBP
  282.     mov    EBP,ESP
  283.     uses    <EBX>
  284.     mov    EBX,P[EBP]        ;file handle
  285.     bdos    3Eh            ;write to file or device
  286.     unuse    <EBX>
  287.     jmp    U2
  288. c_endp    close
  289.  
  290. ;;;;;;;;;;;;;;;;;;;;;;;;
  291. ; Rename a file.        J.K.H. 2/15/86
  292. ; Use:
  293. ;    int rename (from, to)
  294. ; Returns:
  295. ;    -1    error
  296. ;    0    successful
  297. ;
  298.  
  299. func    rename
  300.     push    EBP
  301.     mov    EBP,ESP
  302.     uses    <EDI>
  303.       ife ESeqDS
  304.     mov    AX,DS
  305.     mov    ES,AX
  306.       endif
  307.     mov    AH,56h            ;DOS rename function
  308.     mov    EDI,P+SIZEPTR[EBP]    ;The new name.
  309.     jmps    U1
  310. c_endp    rename
  311.  
  312. ;;;;;;;;;;;;;;;;;;;;;;;;;
  313. ; Delete a file
  314. ; Use:
  315. ;    int remove(char *name)
  316. ; Returns:
  317. ;    -1    error
  318. ;    0    successful
  319. ;
  320.  
  321. func    remove
  322. c_endp    remove
  323.  
  324. func    unlink
  325.     push    EBP
  326.     mov    EBP,ESP
  327.     uses    <EDI>
  328.     mov    AH,41h            ;delete file function
  329. U1:
  330.     mov    EDX,P[EBP]        ;EDX -> name
  331.     bdos
  332.     unuse    <EDI>
  333. U2:    pop    EBP
  334.     jnc    L7            ;no error
  335. L8:    movsx    EAX,AX
  336.     mov    errno,EAX
  337. L7:    sbb    EAX,EAX            ;-1 if C, 0 if not
  338.     ret
  339. c_endp    unlink
  340.  
  341. ;;;;;;;;;;;;;;;;;;;;;;;;;
  342. ; Seek to specified file position.
  343. ; Use:
  344. ;    long lseek(int fd,long offset,int mode)
  345. ; Input:
  346. ;    mode =    SEEK_SET or SEEK_CUR or SEEK_END
  347. ; Returns:
  348. ;    -1L    error
  349. ;    n    new file position
  350. ;
  351.  
  352. func    lseek
  353.     push    EBP
  354.     mov    EBP,ESP
  355.     uses    <EBX,ECX,EDX>
  356.     mov    EBX,P[EBP]        ;file handle
  357.     mov    DX,P+4[EBP]        ;lsw of offset
  358.     mov    CX,P+6[EBP]        ;msw of offset
  359.     mov    AL,P+8[EBP]        ;mode (0,1,2)
  360.     bdos    42h            ;write to file or device
  361.     movzx    EAX,AX
  362.     jnc    short L9        ;no error
  363.     mov    errno,EAX        ;save DOS error number
  364.     sbb    EAX,EAX            ;EAX = -1
  365. L9:    shl    EDX,16            ;mov msw to top half of reg.
  366.     or    EAX,EDX            ;combine into one reg.
  367.     unuse    <EDX,ECX,EBX>
  368.     pop    EBP
  369.     ret
  370. c_endp    lseek
  371.  
  372. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  373. ; Get and return the size of a file.
  374. ; Use:
  375. ;    long filesize(filename)
  376. ;    char *filename;
  377. ; Returns:
  378. ;    -1L    error
  379.  
  380. func    filesize
  381.     push    EBP
  382.     mov    EBP,ESP
  383.     sub    ESP,44        ;44 bytes for DTA
  384.  
  385.     ;Set DTA to the 44 bytes on the stack (SS:EBP)
  386.     mov    EDX,ESP        ;DS:EDX is DTA
  387.     bdos    1Ah        ;set DTA
  388.  
  389.     ;Find first matching file
  390.     mov    EDX,P[EBP]
  391.     mov    CX,6        ;find all normal files, plus system and hidden
  392.     bdos    4Eh        ;findfirst (DS:EDX -> filename)
  393.     jc    L11        ;no error
  394.  
  395.     ;Load file size from DTA
  396.     mov    EAX,26[ESP]
  397.  
  398. L12:    leave
  399.     ret
  400.  
  401. L11:
  402.     movsx    EAX,AX
  403.     mov    errno,EAX    ;remember error code
  404.     sbb    EAX,EAX
  405.     cwd            ;return -1L on error
  406.     leave
  407.     ret
  408. c_endp    filesize
  409.  
  410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  411. ; Determine if handle is a tty.
  412. ; Use:
  413. ;    int isatty(fd)
  414. ;    int fd;
  415. ; Returns:
  416. ;    !=0    character device
  417. ;    0    not a character device or error
  418.  
  419. func    isatty
  420.     push    EBX
  421.     mov    EBX,P[ESP]    ;get fd (file handle)
  422.     mov    AX,04400h    ;get device information
  423.     bdos            ;IOCTL
  424.     pop    EBX
  425.     cmc
  426.     jnc    I4        ;error
  427.  
  428.     ;If sign bit is set in DL, it is a character device.
  429.     movsx    EAX,DL        ;AH = 0FFh if char dev, else 0
  430.     mov    AL,AH
  431.     ret
  432. c_endp    isatty
  433.  
  434. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  435. ;    #include <io.h>
  436. ;    int access(char *path,int mode);
  437. ; Synopsis:
  438. ;    Test file for existence and it's access bits. The access bits are:
  439. ;    #define F_OK    0    /* does file exist?    */
  440. ;    #define X_OK    1    /* execute permission?    */
  441. ;    #define W_OK    2    /* write permission?    */
  442. ;    #define R_OK    4    /* read permission?    */
  443. ;    OR these values together to test a combination.
  444. ; Bugs:
  445. ;    Since MS-DOS only records a write permission bit, the X_OK and
  446. ;    R_OK access permissions always succeed. They are included here
  447. ;    for unix compatibility.
  448. ; Returns:
  449. ;    0    file exists and access mode is granted
  450. ;    -1    otherwise and errno is set
  451.  
  452.     c_public access
  453. func    access
  454.     mov    AX,4300h    ;get file's attribute
  455.     mov    EDX,PS[ESP]    ;DS:DX = path
  456.     bdos
  457.     jc    I4
  458.     test    byte ptr PS+SIZEPTR[ESP],2    ;is W_OK set?
  459.     jz    L7                ;no
  460.     shr    CL,1        ;is file read-only?
  461.     jnc    L7        ;no
  462.     mov    EAX,5        ;EACCES
  463. I4:    jmp    L8
  464. c_endp    access
  465.  
  466.     endcode io
  467.  
  468.     end
  469.  
  470.