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