home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  3.1 KB  |  135 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: open.c,v 1.3 1997/01/17 16:23:48 digulla Exp $
  4.  
  5.     Desc: ANSI C function open()
  6.     Lang: english
  7. */
  8. #include <exec/memory.h>
  9. #include <dos/dos.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <errno.h>
  13. #include "__errno.h"
  14. #include "__stdio.h"
  15.  
  16.  
  17. /*****************************************************************************
  18.  
  19.     NAME */
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22.  
  23.     int open (
  24.  
  25. /*  SYNOPSIS */
  26.     const char * pathname,
  27.     int         flags,
  28.     ...)
  29.  
  30. /*  FUNCTION
  31.     Opens a file with the specified flags and name.
  32.  
  33.     INPUTS
  34.     pathname - Path and filename of the file you want to open.
  35.     flags - Most be exactly one of: O_RDONLY, O_WRONLY or O_RDWR
  36.         to open a file for reading, writing or for reading and
  37.         writing.
  38.  
  39.         The mode can be modified by or'ing the following bits in:
  40.  
  41.         O_CREAT: Create the file if it doesn't exist (only for
  42.             O_WRONLY or O_RDWR). If this flag is set, then
  43.             open() will look for a third parameter mode. mode
  44.             must contain the access modes for the file
  45.             (mostly 0644).
  46.         O_EXCL: Only with O_CREAT. If the file does already exist,
  47.             then open() fails. See BUGS.
  48.         O_NOCTTY:
  49.         O_TRUNC: If the file exists, then it gets overwritten. This
  50.             is the default and the opposite to O_APPEND.
  51.         O_APPEND: If the file exists, then the startung position for
  52.             writes is the end of the file.
  53.         O_NONBLOCK or O_NDELAY: Opens the file in non-blocking mode.
  54.             If there is no data in the file, then read() on a
  55.             terminal will return immediately instead of waiting
  56.             until data arrives. Has no effect on write().
  57.         O_SYNC: The process will be stopped for each write() and the
  58.             data will be flushed before the write() returns.
  59.             This ensures that the data is physically written
  60.             when write() returns. If this flag is not specified,
  61.             the data is written into a buffer and flushed only
  62.             once in a while.
  63.  
  64.     RESULT
  65.     -1 for error or a file descriptor for use with read(), write(), etc.
  66.  
  67.     NOTES
  68.     If the filesystem doesn't allow to specify different access modes
  69.     for users, groups and others, then the user modes are used.
  70.  
  71.     EXAMPLE
  72.  
  73.     BUGS
  74.     The flag O_EXCL is not very reliable if the file resides on a NFS
  75.     filesystem.
  76.  
  77.     Most flags are not supported right now.
  78.  
  79.     SEE ALSO
  80.     close(), read(), write(), fopen()
  81.  
  82.     INTERNALS
  83.  
  84.     HISTORY
  85.     10.01.97 digulla created
  86.  
  87. ******************************************************************************/
  88. {
  89.     FILENODE * fn;
  90.     BPTR       fh;
  91.     int        openmode;
  92.  
  93.     if (!(fn = AllocMem (sizeof (FILENODE), MEMF_ANY)) )
  94.     {
  95.     errno = ENOMEM;
  96.     return -1;
  97.     }
  98.  
  99.     switch (flags & O_ACCMODE)
  100.     {
  101.     case O_RDONLY:
  102.     openmode = MODE_OLDFILE;
  103.     break;
  104.  
  105.     case O_WRONLY:
  106.     if (flags & O_CREAT)
  107.         openmode = MODE_NEWFILE;
  108.     else
  109.         openmode = MODE_NEWFILE;
  110.  
  111.     break;
  112.  
  113.     case O_RDWR:
  114.     openmode = MODE_READWRITE;
  115.     break;
  116.  
  117.     }
  118.  
  119.     if (!(fh = Open ((char *)pathname, openmode)) )
  120.     {
  121.     FreeMem (fn, sizeof (FILENODE));
  122.  
  123.     errno = IoErr2errno (IoErr ());
  124.     return -1;
  125.     }
  126.  
  127.     fn->File.fh = (void *)fh;
  128.     fn->fd = __stdio_fd ++;
  129.  
  130.     AddTail ((struct List *)&__stdio_files, (struct Node *)fn);
  131.  
  132.     return fn->fd;
  133. } /* open */
  134.  
  135.