Subversion Repositories Kolibri OS

Rev

Rev 5198 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* open.c -- open a file.
  2.  *
  3.  * Copyright (c) 1995 Cygnus Support
  4.  *
  5.  * The authors hereby grant permission to use, copy, modify, distribute,
  6.  * and license this software and its documentation for any purpose, provided
  7.  * that existing copyright notices are retained in all copies and that this
  8.  * notice is included verbatim in any distributions. No written agreement,
  9.  * license, or royalty fee is required for any of the authorized uses.
  10.  * Modifications to this software may be copyrighted by their authors
  11.  * and need not follow the licensing terms described here, provided that
  12.  * the new terms are clearly indicated on the first page of each file where
  13.  * they apply.
  14.  */
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <sys/kos_io.h>
  19. #include "glue.h"
  20. #include "io.h"
  21.  
  22. #undef erro
  23. extern int errno;
  24.  
  25. int open (const char * filename, int flags, ...)
  26. {
  27.     __io_handle *ioh;
  28.     fileinfo_t   info;
  29.     int iomode, rwmode, offset;
  30.     int hid;
  31.     int err;
  32.  
  33.     hid = __io_alloc();
  34.     if(hid < 0)
  35.     {
  36.         errno = EMFILE;
  37.         return (-1);
  38.     };
  39.  
  40. //    path = getfullpath(name);
  41.  
  42.     err = get_fileinfo(filename, &info);
  43.  
  44.     if( flags & O_EXCL &&
  45.         flags & O_CREAT )
  46.     {
  47.         if( !err )
  48.         {
  49.             errno = EEXIST;
  50.             return (-1);
  51.         };
  52.     }
  53.  
  54.     if( err )
  55.     {
  56.         if(flags & O_CREAT)
  57.             err=create_file(filename);
  58.         if( err )
  59.         {
  60.             errno = EACCES;
  61.             return -1;
  62.         };
  63.     };
  64.  
  65.     if( flags & O_TRUNC )
  66.         set_file_size(filename, 0);
  67.  
  68.     ioh = &__io_tab[hid];
  69.  
  70.     rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR );
  71.  
  72.     iomode = 0;
  73.     offset = 0;
  74.  
  75.     if( rwmode == O_RDWR )
  76.         iomode |= _READ | _WRITE;
  77.     else if( rwmode == O_RDONLY)
  78.         iomode |= _READ;
  79.     else if( rwmode == O_WRONLY)
  80.         iomode |= _WRITE;
  81.  
  82.     if( flags & O_APPEND )
  83.     {
  84.         iomode |= _APPEND;
  85.         offset = info.size;
  86.     };
  87.  
  88.     if( flags & (O_BINARY|O_TEXT) )
  89.     {
  90.         if( flags & O_BINARY )
  91.             iomode |= _BINARY;
  92.     } else
  93.         iomode |= _BINARY;
  94.  
  95.     ioh->name   = strdup(filename);
  96.     ioh->offset = offset;
  97.     ioh->mode   = iomode;
  98.     ioh->read   = read_file;
  99.     ioh->write  = write_file;
  100.  
  101.     return hid;
  102. };
  103.  
  104.  
  105.