home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / open.c < prev    next >
C/C++ Source or Header  |  1992-12-07  |  3KB  |  148 lines

  1. /* based upon Dale Schumacher's dLibs library */
  2. /* extensively modified by ers */
  3.  
  4. #include <compiler.h>
  5. #include <osbind.h>
  6. #include <mintbind.h>
  7. #include <limits.h>
  8. #include <fcntl.h>
  9. #include <ioctl.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <stdarg.h>
  13. #include "lib.h"
  14.  
  15. /*
  16.  * the MiNT kernel uses 0x08 for O_APPEND. For
  17.  * backwards compatibility with old .o files,
  18.  * we leave the definition in fcntl.h the same,
  19.  * but adjust the file masks here.
  20.  */
  21.  
  22. static int __umask = -1;
  23. extern int __mint;
  24.  
  25. static void _get_umask __PROTO((void));
  26.  
  27. /*
  28.  * function to set the initial value of __umask
  29.  */
  30.  
  31. static void
  32. _get_umask()
  33. {
  34.     if (__mint < 9) {
  35.         __umask = 0;
  36.     } else {
  37.         __umask = Pumask(0);
  38.         (void) Pumask(__umask);
  39.     }
  40. }
  41.  
  42. /* NOTE: we assume here that every compiler that can handle __PROTO
  43.  *       is __STDC__ and can handle the >>...<< ellipsis
  44.  *       (see also unistd.h)
  45.  */
  46.  
  47. #ifdef __STDC__
  48. int open(const char *_filename, int iomode, ...)
  49. #else
  50. int open(_filename, iomode, pmode)
  51.     const char *_filename;
  52.     int iomode;
  53.     unsigned pmode;
  54. #endif
  55. {
  56.     int rv;
  57.     int modemask;            /* which bits get passed to the OS? */
  58.     char filename[PATH_MAX];
  59.  
  60. #ifdef __STDC__
  61.     unsigned pmode;
  62.     va_list argp;
  63.     va_start(argp, iomode);
  64.     pmode = va_arg(argp, unsigned int);
  65.     va_end(argp);
  66. #endif
  67.  
  68.     _unx2dos(_filename, filename);
  69.  
  70. /* use the umask() setting to get the right permissions */
  71.     if (__umask == -1)
  72.         _get_umask();
  73.     pmode &= ~__umask;
  74.  
  75. /* set the file access modes correctly */
  76.     iomode = iomode & ~O_SHMODE;
  77.  
  78.     if (__mint >= 9) {
  79.         modemask = O_ACCMODE | O_SHMODE | O_SYNC | O_NDELAY;
  80.         iomode |= (iomode & O_EXCL) ? O_DENYRW : O_DENYNONE;
  81.         if (__mint >= 96) {
  82.             modemask |= _REALO_APPEND;
  83.             if (iomode & O_APPEND)
  84.                 iomode |= _REALO_APPEND;
  85.         }
  86.     } else {
  87.         modemask = O_ACCMODE;
  88.     }
  89.  
  90.     if(Fattrib(filename, 0, 0) >= 0)        /* file exists */
  91.     {
  92.         if((iomode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
  93.             errno = EEXIST;
  94.             return -1;
  95.         }
  96.         if(iomode & O_TRUNC)
  97.             rv = (int)Fcreate(filename, 0x00);
  98.         else
  99.             rv = (int)Fopen(filename,iomode & modemask);
  100.     }
  101.     else                    /* file doesn't exist */
  102.     {
  103.         if(iomode & O_CREAT) {
  104.             rv = (int)Fcreate(filename, 0x00);
  105.             if (rv >= 0 && __mint >= 9)
  106.                 (void)Fchmod(filename, pmode);
  107.         }
  108.         else
  109.             rv = -ENOENT;
  110.     }
  111.  
  112.     if((rv >= 0) && (iomode & O_APPEND))
  113.         (void)lseek(rv, 0L, SEEK_END);
  114.  
  115.     if(rv < (__SMALLEST_VALID_HANDLE)) {
  116.         errno = -rv;
  117.         return __SMALLEST_VALID_HANDLE - 1;
  118.     }
  119.  
  120.     (void)isatty(rv);    /* sets up tty flags under TOS */
  121.  
  122.     return(rv);
  123. }
  124.  
  125. int
  126. creat(name, mode)
  127.     const char *name;
  128.     unsigned   mode;
  129. {
  130.     return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
  131. }
  132.  
  133. /* umask -- change default file creation mask */
  134.  
  135. int umask(complmode)
  136.        int complmode;
  137. {
  138.     int old_umask;
  139.  
  140.     if (__umask == -1)
  141.         _get_umask();
  142.     old_umask = __umask;
  143.     __umask = complmode;
  144.     if (__mint >= 9)
  145.         return Pumask(complmode);
  146.     return old_umask;
  147. }
  148.