home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c220 / 4.ddi / ETC / UNIX.IO / SYSUNIX.C next >
Encoding:
C/C++ Source or Header  |  1990-12-16  |  5.0 KB  |  144 lines

  1. #include <implement.cf>
  2. #include <language.cf>
  3. #include <status.cf>
  4.    pragma data(common,_Private_prefix "dosregs");
  5.    /* DON'T TOUCH THIS REGISTER STRUCTURE.  It matches that in msdos.cf. */
  6.    typedef union {
  7.      struct {char L,H;} LH;   /* Lower & Upper portions of 16-bit register. */
  8.      unsigned R;          /* Entire register. */
  9.      } Register;
  10.       Register AX,BX,CX,DX,SI,DI,DS,ES;
  11.       /* The rightmost bit of Flags is the carry bit.  MS-DOS usually sets */
  12.       /* carry if an error occurs.                       */
  13.       /* Thus, if Flags is odd on return, an error occurred;           */
  14.       /* ax contains the error number #.                   */
  15.       unsigned Flags;
  16.     pragma data;
  17. /*
  18. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  19. >> WARNING!!!  The run-time library uses this Registers                  <<
  20. >> variable.  So DO NOT expect it to be preserved across calls to        <<
  21. >> the library that require DOS communication (e.g., Professional Pascal <<
  22. >> "writeln" or High C "printf").                                        <<
  23. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  24. */
  25. #if ! _system_defs_included
  26.  
  27. typedef unsigned int File_handle;     /* Small integer file handle. */
  28. typedef enum{From_beginning,From_current,From_end} Seek_method;
  29. typedef enum{Compatibility,Deny_read_write,Deny_write,Deny_read,Deny_none} 
  30.         Sharing_mode_type;
  31. /* Use data pragma to share with Pascal system.pf package: */        
  32.     pragma data(common,"?" _Private_prefix "system");
  33. Sharing_mode_type Sharing_mode; /* Initialized to Compatibility. */
  34.     pragma data;
  35. typedef enum{For_reading, For_writing, For_updating} Open_method;
  36. /* File attributes: */
  37. #define Attr_read_only 1       /* File may not be modified. */
  38. #define Attr_hidden    2       /* File is hidden. */
  39. #define Attr_system    4       /* File is a "system" file. */
  40. #define Attr_volume_id 8       /* File is the volume id. */
  41. #define Attr_directory 16      /* File is a directory. */
  42. #define Attr_archive   32      /* File has not been archived. */
  43. #ifdef CDOS
  44. #define Attr_security  64      /* File has security enabled. */
  45. #endif
  46. typedef unsigned char File_mode; /* A set of the above bits. */
  47.  
  48. extern int errno;
  49. pragma Calling_convention(PASCAL);
  50. extern void _mwc_dos_name(char *Name, unsigned char I); 
  51. extern void _mwdos(unsigned char I);
  52. extern void _mwload_ds_dx(void *A);
  53. pragma Calling_convention(_DEFAULT_CALLING_CONVENTION);
  54.  
  55. /*
  56.   NOTE: Following every call, the imported variable errno contains the
  57.     status of the call. If zero, then the call was successful, otherwise
  58.     it will contain one of the error codes specified after each
  59.     routine declaration below.
  60. */
  61.  
  62. /* Close file whose handle is "F". */
  63. int close(File_handle F) {
  64.    int save = errno; errno = 0;
  65.    BX.R = F; _mwdos(62);
  66.    int error_occurred = errno != 0 ? -1 : 0;
  67.    if (errno == 0) errno = save;
  68.    return error_occurred;
  69.    }    
  70.  
  71. /* Create file "name" and open it for writing. */
  72. /* NOTE!!! The Mode is NOT For_reading, For_writing, or For_updating, */
  73. /* but is a combination of attribute bits such as Attr_hidden, Attr_archive, */
  74. /* etc.     Usually you should specify Mode = 0. */
  75. File_handle creat(char *Name, File_mode Mode) {
  76.    CX.R = Mode;   /* was retype (Mode, Byte) */
  77.    int save = errno; errno = 0;
  78.    _mwc_dos_name(Name,60);
  79.    if (errno) return -1;
  80.    else {
  81.       errno = save;
  82.       return AX.R;
  83.       }
  84.    }    
  85.  
  86. /* Reposition the file pointer assoicated with "F" to location "Loc" */
  87. /* according to the method specified in "Method". */
  88. /* Return the new position. */
  89. long lseek(File_handle F, long Loc, Seek_method Method) {
  90.    union Addr {long L;  struct{short int Lo, Hi;}Bits;} A;
  91.    A.L=Loc;
  92.    BX.R = F; AX.LH.L = Method;
  93.    CX.R=A.Bits.Hi;
  94.    DX.R=A.Bits.Lo;
  95.    _mwdos(66);
  96.    A.Bits.Hi = DX.R;
  97.    A.Bits.Lo = AX.R;
  98.    return A.L;
  99.    }
  100.  
  101. /* Open file "Name" for reading, writing, or updating. */
  102.  
  103. File_handle open(char *Name, Open_method Method) {
  104.    AX.LH.L = (Sharing_mode << 4) | Method;
  105.    int save = errno; errno = 0;
  106.    _mwc_dos_name(Name,61);
  107.    if (errno) return -1;
  108.    else {
  109.       errno = save;
  110.       return AX.R;
  111.       }
  112.    }
  113.  
  114. /* Read "Cnt" bytes from file "F" into buffer whose address is "Bufp". */
  115. /* Returns the number of bytes actually read. */
  116. unsigned read(File_handle F, void *Buf, unsigned Cnt) {
  117.    _mwload_ds_dx(Buf);
  118.    CX.R = Cnt;
  119.    BX.R = F;
  120.    _mwdos(63);
  121.    return(AX.R);
  122.    }
  123.  
  124. /* Erase file from directory. */
  125. void unlink(char *Name) {
  126.    _mwc_dos_name(Name,65);
  127.    }
  128.  
  129. /* Write "Cnt" bytes from buffer whose address is "Bufp" into file "F". */
  130. /* Note: If disk overflow occurs, Errno will have the value Error_disk_overflow */
  131. /*     and Write will return with the number of bytes actually written. */
  132.  
  133. unsigned write(File_handle F, void *Buf, unsigned Cnt) {
  134.    _mwload_ds_dx(Buf);
  135.    CX.R = Cnt;
  136.    BX.R = F;
  137.    _mwdos(64);
  138.    if ((AX.R < Cnt) && (errno == No_error_occurred)) 
  139.       errno = Error_write_failed;
  140.    return(AX.R);
  141.    }    
  142.  
  143. #define _system_defs_included 1 
  144. #endif