home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1.6 KB  |  85 lines

  1. /*
  2.  *        Cross Development System for Atari ST 
  3.  *     Copyright (c) 1988, Memorial University of Newfoundland
  4.  *
  5.  *  Tries to emulate unix open3.  The mode argument is ignored, and O_NDELAY
  6.  * doesn't do anything.  Also O_APPEND does not garentee that things will
  7.  * be written to the end - all we can do is seek to the end to start with.
  8.  *
  9.  * $Header: open.c,v 1.2 88/01/29 17:31:36 m68k Exp $
  10.  *
  11.  * $Log:    open.c,v $
  12.  *
  13.  * Modified by jrd for native gcc 1.2
  14.  *
  15.  * Revision 1.2  88/02/24  17:31:36  m68k
  16.  * 
  17.  */
  18. #include    <types.h>
  19. #include    <file.h>
  20. #include    <errno.h>
  21. #include    <osbind.h>
  22.  
  23. #ifdef DEBUG
  24. extern int stderr;
  25. #endif
  26.  
  27. int open(path, flags, mode)
  28. char    * path;
  29. short    flags;
  30. u_short    mode;
  31. {
  32.   int        fd;
  33.   int        exists;
  34.   extern    int    errno;
  35.  
  36.   switch (flags & 0x3) 
  37.     {
  38.     case O_RDONLY:
  39.         fd = Fopen(path, O_RDONLY);
  40.         break;
  41.  
  42.     case O_WRONLY:
  43.     case O_RDWR:
  44.         fd = 0;
  45.         exists = Fattrib(path, 0, 0) >= 0;
  46.         if (flags & O_CREAT) 
  47.             {
  48.             if ((flags & O_EXCL) && exists)
  49.                 fd = EEXIST;
  50.             }
  51.             else
  52.             if (!exists)
  53.                 fd = ENOENT;
  54.         if (!fd) 
  55.             {
  56.             if ((flags & O_TRUNC) || !exists) 
  57.                 {
  58.                 if ((fd = Fcreate(path, 0)) >= 0
  59.                     && (flags & 0x3) == O_RDWR)
  60.                     {
  61.                     (void) close(fd);
  62.                     fd = Fopen(path, O_RDWR);
  63.                     }
  64.                 }
  65.                 else
  66.             if ((fd = Fopen(path, flags & 0x3)) >= 0
  67.                 && (flags & O_APPEND))
  68.                     (void) Fseek(0L, fd, L_XTND);
  69.             }
  70.         break;
  71.  
  72.     default:
  73.         fd = EINVAL;
  74.     }
  75.   if (fd < 0) 
  76.     {
  77.     errno = fd;
  78.     fd = -1;
  79.     }
  80. #ifdef DEBUG
  81. fprintf(stderr, "open('%s', %X)->%d\n", path, flags, fd);
  82. #endif
  83.   return fd;
  84. }
  85.