home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / OPEN.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  11.2 KB  |  292 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - open.cas
  3.  *
  4.  * function(s)
  5.  *        dosCreat     - creates a file
  6.  *        dosWriteNone - writes zero bytes to a file
  7.  *        open         - opens a file for reading or writing
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*[]------------------------------------------------------------[]*/
  11. /*|                                                              |*/
  12. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  13. /*|                                                              |*/
  14. /*|                                                              |*/
  15. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  16. /*|     All Rights Reserved.                                     |*/
  17. /*|                                                              |*/
  18. /*[]------------------------------------------------------------[]*/
  19.  
  20.  
  21. #pragma inline
  22.  
  23. #ifndef __PAS__
  24.  
  25. #define __IN_OPEN
  26.  
  27. #include <asmrules.h>
  28. #include <io.h>
  29. #include <_io.h>
  30. #include <fcntl.h>
  31. #include <sys\stat.h>
  32.  
  33. extern  unsigned      _notUmask;
  34.  
  35.  
  36. /*-----------------------------------------------------------------------*
  37.  
  38. Name            dosCreat -  creates a file
  39.  
  40. Usage           static  int pascal near  dosCreat (char *pathP,
  41.                                                    unsigned attrib);
  42.  
  43. Description     creates a file using DOS function 0x3c
  44.  
  45. Return value    file handle or -1 on error
  46.  
  47. *------------------------------------------------------------------------*/
  48. static  int pascal near  dosCreat (char *pathP, unsigned attrib)
  49. {
  50.     pushDS_
  51. asm    mov    cx, attrib
  52. asm    mov    ah, 3Ch
  53. asm    LDS_    dx, pathP
  54. asm    int    21h    /* AX = creat (DS:DX, CX) */
  55.     popDS_
  56. asm    jc    dosCreatFailed
  57.  
  58.     return _AX;
  59.  
  60. dosCreatFailed:
  61.     return  __IOerror (_AX);
  62. }
  63.  
  64.  
  65. /*-----------------------------------------------------------------------*
  66.  
  67. Name            dosWriteNone - writes zero bytes to a file
  68.  
  69. Usage           static  void pascal near    dosWriteNone (int handle);
  70.  
  71. Description     write 0 bytes to a file
  72.  
  73. Return value    Nothing
  74.  
  75. *------------------------------------------------------------------------*/
  76. static  void pascal near    dosWriteNone (int handle)
  77. {
  78. asm             mov             bx, handle
  79. asm             sub             cx, cx   /* zero length causes truncation */
  80. asm             sub             dx, dx
  81. asm             mov             ah, 40h
  82. asm             int             21h      /* dosWrite (fildes, dont-care, 0) */
  83.  
  84.                 return;                    /* no error checking */
  85. }
  86.  
  87.  
  88. /*-----------------------------------------------------------------------*
  89.  
  90. Name            open - opens a file for reading or writing
  91.  
  92. Usage           #include <fcntl.h>
  93.                 #include<sys\stat.h>
  94.                 int open(const char *pathname, int access [,unsigned permiss]);
  95.  
  96. Related
  97. functions usage int _open(const char *pathname, int access);
  98.                 int sopen(const char *pathname, int access, int shflag,
  99.                           unsigned permiss);
  100.  
  101. Prototype in    io.h
  102.  
  103. Description     open opens the file specified by pathname, then
  104.                 prepares it for reading and/or writing as determined by
  105.                 the value of access.
  106.  
  107.                 For open, access is constructed by bitwise ORing flags
  108.                 from the following two lists. Only one flag from the first
  109.                 list may be used; the remaining flags may be used in any
  110.                 logical combination.
  111.  
  112.                 List 1: Read/Write flags
  113.  
  114.                 O_RDONLY        Open for reading only.
  115.                 O_WRONLY        Open for writing only.
  116.                 O_RDWR          Open for reading and writing.
  117.  
  118.  
  119.                 List 2: Other access flags
  120.  
  121.                 O_NDELAY        Not used; for UNIX compatibility.
  122.                 O_APPEND        If set, the file pointer will be set
  123.                                 to the end of the file prior to each
  124.                                 write.
  125.                 O_CREAT         If the file exists, this flag has no effect.
  126.                                 If the file does not exist, the file is
  127.                                 created, and the bits of permiss are used
  128.                                 to set the file attribute bits, as in chmod.
  129.                 O_TRUNC         If the file exists, its length is truncated
  130.                                 to 0.
  131.                                 The file attributes remain unchanged.
  132.                 O_EXCL          Used only with O_CREAT. If the file already
  133.                                 exists, an error is returned.
  134.                 O_BINARY        This flag can be given to explicitly open
  135.                                 the file in binary mode.
  136.                 O_TEXT          This flag can be given to explicitly open
  137.                                 the file in text mode.
  138.  
  139.                 These O_... symbolic constants are defined in fcntl.h.
  140.  
  141.                 If neither O_BINARY nor O_TEXT is given, the file is
  142.                 opened in the translation mode set by the global variable
  143.                 _fmode.
  144.  
  145.                 If the O_CREAT flag is used in constructing access, you need
  146.                 to supply the permiss argument to open, from the following
  147.                 symbolic constants defined in sys\stat.h.
  148.  
  149.                 Value of permiss        Access Permission
  150.  
  151.                 S_IWRITE                Permission to write
  152.                 S_IREAD                 Permission to read
  153.                 S_IREAD|S_IWRITE        Permission to read and write
  154.  
  155.  
  156.                 For _open, the value of access in MS-DOS 2.x is limited to
  157.                 O_RDONLY, O_WRONLY, and O_RDWR. For MS-DOS 3.x, the following
  158.                 additional values can also be used:
  159.  
  160.                 O_NOINHERIT     Included if the file is not to be passed to
  161.                                 child programs.
  162.                 O_DENYALL       Allows only the current handle to have access to
  163.                                 the file.
  164.                 O_DENYWRITE     Allows only reads from any other open to the
  165.                                 file.
  166.                 O_DENYREAD      Allows only writes from any other open to the
  167.                                 file.
  168.                 O_DENYNONE      Allows other shared opens to the file.
  169.  
  170.                 Only one of the O_DENYxxx values may be included in a single
  171.                 _open under DOS 3.x. These file-sharing attributes are in
  172.                 addition to any locking performed on the files.
  173.  
  174.                 The maximum number of simultaneously open files is a system
  175.                 configuration parameter.
  176.  
  177.                 sopen is a macro defined as
  178.  
  179.                    open(pathname, (access | shflag), permiss)]
  180.  
  181.                 where pathname, access, and permiss are the same as for open,
  182.                 and shflag is a flag specifying the type of file-sharing
  183.                 allowed on the file pathname. Symbolic constants for shflag
  184.                 are defined in share.h.
  185.  
  186. Return value    On successful completion, these routines return a
  187.                 non-negative integer (the file handle), and the file pointer
  188.                 (that marks the current position in the file) is set to the
  189.                 beginning of the file. On error, they return -1 and errno is
  190.                 set to one of the following:
  191.  
  192.                         ENOENT  Path or file name not found
  193.                         EMFILE  Too many open files
  194.                         EACCES  Permission denied
  195.                         EINVACC Invalid access code
  196.  
  197. *------------------------------------------------------------------------*/
  198. /* open declared old style since prototype has ... */
  199.  
  200. int  open(pathP, oflag, mode)
  201.     const char *pathP; register int oflag; unsigned mode;
  202. /*
  203.   Open a new file or replace an existing file with the given pathname.
  204.   The opened file's read/write permission will be (pmode && !_notUmask),
  205.   unless the file already exists in which case its present mode is kept.
  206. */
  207. {
  208.         unsigned  attrib;
  209.     unsigned  devstat;
  210.         register int  fildes;
  211.  
  212.         if (! (oflag & (O_TEXT | O_BINARY)))
  213.                 oflag |= _fmode & (O_TEXT | O_BINARY);
  214.  
  215.         if (oflag & O_CREAT)
  216.         {
  217.                 if (((mode &= _notUmask) & (S_IREAD | S_IWRITE)) == 0)
  218.                         __IOerror (1);
  219.  
  220.                 if ((attrib = _chmod (pathP, 0)) == ~0U)
  221.                 {
  222.                         attrib = (mode & S_IWRITE) ? 0 : 1 /* read-only */;
  223.                 }
  224.                 else
  225.                 {
  226.                         if (oflag & O_EXCL)
  227.                                 return(__IOerror (e_fileExists));
  228.                         else  /* ignore O_CREAT if file exists */
  229.                                 goto OpenExisting;
  230.                 }
  231.  
  232. /* Are any sharing/inheritance bits specified ?  If so, then we need to use
  233.    _open(), so  we must create, close,  then reopen it. Since  the creation
  234.    may be read only mode, but we may need read-write access, we must create
  235.    it with      write access to  allow us to  reopen for write,  and then later
  236.    change the mode after a successful open.
  237. */
  238.                 if (oflag & 0xF0)               /* any sharing/inheritance ? */
  239.                 {
  240.                         if ((fildes = dosCreat ((char *)pathP, 0)) < 0)
  241.                                 return(fildes);
  242.                         _close (fildes);
  243.                         goto OpenWithSharing;
  244.                 }
  245.                 else
  246.                         if ((fildes = dosCreat ((char *)pathP, attrib)) < 0)
  247.                                 return(fildes);
  248.         }
  249.         else
  250.         {
  251. OpenExisting:
  252.                 attrib = 0;     /* inhibit _chmod, see below     */
  253. OpenWithSharing:
  254.                 if ((fildes = _open (pathP, oflag)) >= 0)
  255.                 {
  256.  
  257.                         if ( (devstat = ioctl (fildes, 0)) & 0x80)
  258.                         {
  259.                                 oflag |= O_DEVICE;
  260. /* Set mode to Binary */
  261.                 if (oflag & O_BINARY)
  262.                                         ioctl(fildes, 1, (void *)((devstat & 0xff) | 0x20));
  263.                         }
  264.                         else
  265.                         {
  266.                                 if ((oflag & O_TRUNC))
  267.                                         dosWriteNone (fildes);
  268. #if CPM_ctlZ    /* see commentary in file "zapctlz.cas" */
  269.                                 else
  270.                                         if (! (oflag & (O_BINARY | O_RDONLY)))
  271.                                                 __TrimCtlZ (fildes);
  272. #endif
  273.                         }
  274.  
  275.                         /* set shared file to read-only */
  276.                         if (attrib      &&      oflag & 0xF0)
  277.                         {
  278.                                 _chmod (pathP, 1, 1);
  279.                         }
  280.                 }
  281.         }
  282.  
  283.         if (fildes >= 0)
  284.         {
  285.                 _openfd [fildes] = (oflag & ~_O_RUNFLAGS) |
  286.                            ((oflag & (O_CREAT | O_TRUNC)) ? O_CHANGED : 0);
  287.         }
  288.         return( fildes );
  289. }
  290.  
  291. #endif
  292.