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

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fopen.c,v 1.1 1997/01/17 16:23:05 digulla Exp $
  4.  
  5.     Desc: ANSI C function fopen()
  6.     Lang: english
  7. */
  8. #define AROS_ALMOST_COMPATIBLE
  9. #include <exec/lists.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. #include "__stdio.h"
  14.  
  15.  
  16. /*****************************************************************************
  17.  
  18.     NAME */
  19. #include <stdio.h>
  20.  
  21.     FILE * fopen (
  22.  
  23. /*  SYNOPSIS */
  24.     const char * pathname,
  25.     const char * mode)
  26.  
  27. /*  FUNCTION
  28.     Opens a file with the specified name in the specified mode.
  29.  
  30.     INPUTS
  31.     pathname - Path and filename of the file you want to open.
  32.     mode - How to open the file:
  33.  
  34.         r: Open for reading. The stream is positioned at the
  35.             beginning of the file.
  36.  
  37.         r+: Open for reading and writing. The stream is positioned
  38.             at the beginning of the file.
  39.  
  40.         w: Open for writing. If the file doesn't exist, then
  41.             it is created. If it does already exist, then
  42.             it is truncated. The stream is positioned at the
  43.             beginning of the file.
  44.  
  45.         w+: Open for reading and writing. If the file doesn't
  46.             exist, then it is created. If it does already
  47.             exist, then it is truncated. The stream is
  48.             positioned at the beginning of the file.
  49.  
  50.         a: Open for writing. If the file doesn't exist, then
  51.             it is created. The stream is positioned at the
  52.             end of the file.
  53.  
  54.         a+: Open for reading and writing. If the file doesn't
  55.             exist, then it is created. The stream is positioned
  56.             at the end of the file.
  57.  
  58.         b: Open in binary more. This has no effect and is ignored.
  59.  
  60.     RESULT
  61.     A pointer to a FILE handle or NULL in case of an error. When NULL
  62.     is returned, then errno is set to indicate the error.
  63.  
  64.     NOTES
  65.  
  66.     EXAMPLE
  67.  
  68.     BUGS
  69.     Most modes are not supported right now.
  70.  
  71.     SEE ALSO
  72.     fclose(), fread(), fwrite(), open(), fgets(), fgetc(),
  73.     fputs(), fputc(), getc(), putc()
  74.  
  75.     INTERNALS
  76.  
  77.     HISTORY
  78.     17.01.97 digulla created
  79.  
  80. ******************************************************************************/
  81. {
  82.     FILENODE * fn;
  83.     int        fd;
  84.     int        openmode;
  85.  
  86.     while (*mode)
  87.     {
  88.     switch (*mode)
  89.     {
  90.     case 'r': openmode = O_RDONLY; break;
  91.     case 'w': openmode = O_WRONLY | O_CREAT; break;
  92.     case 'a': openmode = O_WRONLY | O_APPEND | O_CREAT; break;
  93.     case '+':
  94.         openmode = O_RDWR | (openmode & ~O_ACCMODE);
  95.         break;
  96.  
  97.     default:
  98.         /* ignore */
  99.         break;
  100.     }
  101.     }
  102.  
  103.     if ((fd = open (pathname, openmode, 644)) == -1)
  104.     return NULL;
  105.  
  106.     fn = (FILENODE *)GetTail (&__stdio_files);
  107.  
  108.     return FILENODE2FILE(fn);
  109. } /* fopen */
  110.  
  111.