home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / fopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1.3 KB  |  69 lines

  1.  
  2. /* fopen */
  3.  
  4. #include <file.h>
  5. #include <errno.h>
  6. #include "std-guts.h"
  7.  
  8. struct file * fopen (name, mode)
  9. char * name;
  10. char * mode;
  11. {
  12.   int handle;
  13.   struct file * f;
  14.   int open_flags;
  15.  
  16.   open_flags = _parse_open_options(mode);
  17.   handle = open (name, open_flags, 0);        /* try to open it */
  18.   if (handle <= 0)
  19.     {
  20.     errno = handle;
  21.     return(NULL);                /* couldn't open */
  22.     }
  23.     else
  24.     {
  25.     f = (struct file * )malloc(sizeof (struct file));
  26.     f->handle = handle;
  27.     f->open_p = 1;
  28.     f->eof_p = 0;
  29.     f->last_file_error = 0;
  30.     f->mode = open_flags;
  31.     f->buf_index = 0;        /* so far... */
  32.     f->buf_max = 0;            /* nothing in buf yet */
  33.     f->file_position = 0;        /* nothing read yet */
  34. /* zzz should really check for file length here, for cases when we're
  35.    appending... */
  36.     return(f);
  37.     }
  38.  
  39. }
  40.  
  41. int _parse_open_options(options)
  42. char * options;
  43. {
  44.   int open_flags;
  45. /* this isn't quite right... */
  46.   switch (*options)
  47.     {
  48.     case 'w': 
  49.         {
  50.         if (options[1] != '+')        /* normal output open */
  51.             open_flags = O_WRONLY | O_CREAT | O_TRUNC;
  52.             else
  53.             open_flags = O_RDWR | O_CREAT;
  54.         break;
  55.         }
  56.     case 'r':
  57.         {
  58.         if (options[1] != '+')
  59.             open_flags = O_RDONLY;
  60.             else
  61.             open_flags = O_RDWR;
  62.         break;
  63.         }
  64.     case 'a': { open_flags = O_WRONLY | O_APPEND | O_CREAT; break; }
  65.     case '+': { open_flags = O_RDWR; break; }
  66.     }
  67.   return(open_flags);
  68. }
  69.