home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntlib24 / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  2.6 KB  |  136 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. static int __umask = -1;
  16. extern int __mint;
  17.  
  18. static void _get_umask __PROTO((void));
  19.  
  20. /*
  21.  * function to set the initial value of __umask
  22.  */
  23.  
  24. static void
  25. _get_umask()
  26. {
  27.     if (__mint < 9) {
  28.         __umask = 0;
  29.     } else {
  30.         __umask = Pumask(0);
  31.         (void) Pumask(__umask);
  32.     }
  33. }
  34.  
  35. /* NOTE: we assume here that every compiler that can handle __PROTO
  36.  *       is __STDC__ and can handle the >>...<< ellipsis
  37.  *       (see also unistd.h)
  38.  */
  39.  
  40. #ifdef __STDC__
  41. int open(const char *_filename, int iomode, ...)
  42. #else
  43. int open(_filename, iomode, pmode)
  44.     const char *_filename;
  45.     int iomode;
  46.     unsigned pmode;
  47. #endif
  48. {
  49.     int rv;
  50.     int modemask;            /* which bits get passed to the OS? */
  51.     char filename[PATH_MAX];
  52.  
  53. #ifdef __STDC__
  54.     unsigned pmode;
  55.     va_list argp;
  56.     va_start(argp, iomode);
  57.     pmode = va_arg(argp, unsigned int);
  58.     va_end(argp);
  59. #endif
  60.  
  61.     _unx2dos(_filename, filename);
  62.  
  63. /* use the umask() setting to get the right permissions */
  64.     if (__umask == -1)
  65.         _get_umask();
  66.     pmode &= ~__umask;
  67.  
  68. /* set the file access modes correctly */
  69.     iomode = iomode & ~O_SHMODE;
  70.  
  71.     if (__mint >= 9) {
  72.         modemask = O_ACCMODE | O_SHMODE | O_SYNC | O_NDELAY;
  73.         iomode |= (iomode & O_EXCL) ? O_DENYRW : O_DENYNONE;
  74.     } else {
  75.         modemask = O_ACCMODE;
  76.     }
  77.  
  78.     if(Fattrib(filename, 0, 0) >= 0)        /* file exists */
  79.     {
  80.         if((iomode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
  81.             errno = EEXIST;
  82.             return -1;
  83.         }
  84.         if(iomode & O_TRUNC)
  85.             rv = (int)Fcreate(filename, 0x00);
  86.         else
  87.             rv = (int)Fopen(filename,iomode & modemask);
  88.     }
  89.     else                    /* file doesn't exist */
  90.     {
  91.         if(iomode & O_CREAT) {
  92.             rv = (int)Fcreate(filename, 0x00);
  93.             if (rv >= 0 && __mint >= 9)
  94.                 (void)Fchmod(filename, pmode);
  95.         }
  96.         else
  97.             rv = -ENOENT;
  98.     }
  99.  
  100.     if((rv >= 0) && (iomode & O_APPEND))
  101.         (void)lseek(rv, 0L, SEEK_END);
  102.  
  103.     if(rv < (__SMALLEST_VALID_HANDLE)) {
  104.         errno = -rv;
  105.         return __SMALLEST_VALID_HANDLE - 1;
  106.     }
  107.  
  108.     (void)isatty(rv);    /* sets up tty flags under TOS */
  109.  
  110.     return(rv);
  111. }
  112.  
  113. int
  114. creat(name, mode)
  115.     const char *name;
  116.     unsigned   mode;
  117. {
  118.     return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
  119. }
  120.  
  121. /* umask -- change default file creation mask */
  122.  
  123. int umask(complmode)
  124.        int complmode;
  125. {
  126.     int old_umask;
  127.  
  128.     if (__umask == -1)
  129.         _get_umask();
  130.     old_umask = __umask;
  131.     __umask = complmode;
  132.     if (__mint >= 9)
  133.         return Pumask(complmode);
  134.     return old_umask;
  135. }
  136.