home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / CREATA.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  5.1 KB  |  156 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - creata.cas
  3.  *
  4.  * function(s)
  5.  *        CreateFile - create a file
  6.  *        _creat     - creates a new file or rewrites an existing one.
  7.  *        creattemp  - creates a new file
  8.  *        creatnew   - creates a new file
  9.  *--------------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1987, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <io.h>
  23. #include <_io.h>
  24. #include <fcntl.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <RtlData.h>
  28.  
  29. #undef creatnew         /* Remove the macro version of creatnew() */
  30.  
  31. /*--------------------------------------------------------------------------*
  32.  
  33. Name            CreateFile - create a file
  34.  
  35. Usage           local to this module
  36.  
  37. Description     used by _creat, creattemp, and creatnew to create
  38.                 a file.
  39.  
  40. Return value    success : the new file handle
  41.                 else    : -1 and errno is set to one of the following:
  42.  
  43.                         ENOENT   Path or file name not found
  44.                         EMFILE   Too many open files
  45.                         EACCESS  Permission denied
  46.  
  47.  
  48. *---------------------------------------------------------------------------*/
  49. static int near pascal CreateFile(char *pathP,
  50.                                   int attr,
  51.                                   unsigned char func,
  52.                                   unsigned oflags)
  53. {
  54.         register int    handle;
  55.  
  56.         pushDS_
  57. asm     mov     ah,func
  58. asm     mov     cx, attr
  59. asm     LDS_    dx, pathP
  60. asm     int     21h
  61.         popDS_
  62. asm     jc      CreateFileFailed
  63.         handle = _AX;
  64.         _RTLInstanceData(_openfd) [handle] = oflags;
  65.         return handle;
  66.  
  67. CreateFileFailed:
  68.         return  __IOerror (_AX);
  69. }
  70.  
  71.  
  72. /*--------------------------------------------------------------------------*
  73.  
  74. Name            _creat - creates a new file or rewrites an existing
  75.                          one.
  76.  
  77. Usage           int _creat(const char *filename, int attrib);
  78.  
  79. Prototype in    io.h
  80.  
  81. Description     A file created with _creat is always created in the
  82.                 translation mode specified by the global variable
  83.                 _fmode (O_TEXT or O_BINARY).
  84.  
  85.                 _creat accepts attrib, an MS-DOS attribute word.  Any
  86.                 attribute bits may be set with this call.  The file
  87.                 is always opened in binary mode.  Upon successful
  88.                 creation, the file pointer is set to the beginning
  89.                 of the file.  The file is opened for both reading
  90.                 and writing.
  91.  
  92.                 The attrib argument to _creat, creatnew, and creattemp
  93.                 can be one of the following constants (defined in dos.h):
  94.  
  95.                         FA_RDONLY   Read only attribute
  96.                         FA_HIDDEN   Hidden file
  97.                         FA_SYSTEM   System file
  98.  
  99. Return value    success : the new file handle
  100.                 else    : -1 and errno is set to one of the following:
  101.  
  102.                         ENOENT   Path or file name not found
  103.                         EMFILE   Too many open files
  104.                         EACCESS  Permission denied
  105.  
  106. *---------------------------------------------------------------------------*/
  107. int _CType _FARFUNC _creat (const char *pathP, int attr)
  108. {
  109.         return( CreateFile((char *)pathP, attr, 0x3C, O_RDWR | O_BINARY ) );
  110. }
  111.  
  112.  
  113. /*--------------------------------------------------------------------------*
  114.  
  115. Name            creattemp - creates a new file
  116.  
  117. Usage           int creattemp(const char *filename, int attrib);
  118.  
  119. Prototype in    io.h
  120.  
  121. Description     similar to _creat except that the filename is a path
  122.                 name ending with a backslash (\).  A unique file name
  123.                 is selected in the directory given by filename.  The
  124.                 newly created file name is stored in the filename string
  125.                 supplied.  The file is not automatically deleted when the
  126.                 program terminates.
  127.  
  128. Return value    same as _creat
  129.  
  130. *---------------------------------------------------------------------------*/
  131. int _FARFUNC creattemp (char *pathP, int attr)
  132. {
  133.   return( CreateFile(pathP, attr, 0x5A, _RTLInstanceData(_fmode) | O_RDWR ) );
  134. }
  135.  
  136.  
  137. /*--------------------------------------------------------------------------*
  138.  
  139. Name            creatnew - creates a new file
  140.  
  141. Usage           int creatnew(char *filename, int attrib);
  142.  
  143. Prototype in    io.h
  144.  
  145. Description     identical to _creat, with the exception that, if
  146.                 the file exists, the creatnew call returns an
  147.                 error and leaves the file untouched.
  148.  
  149. Return value    same as _creat
  150.  
  151. *---------------------------------------------------------------------------*/
  152. int _FARFUNC creatnew (const char *pathP, int attr)
  153. {
  154.   return( CreateFile((char *)pathP, attr, 0x5B, _RTLInstanceData(_fmode) | O_RDWR ) );
  155. }
  156.