home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga / _open.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  2.9 KB  |  157 lines

  1. RCS_ID_C="$Id: _open.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _open.c - Unix compatible open() (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <dos/dos.h>
  17. #include <proto/dos.h>
  18. #include <proto/usergroup.h>
  19. #include <stdarg.h>
  20. #include <unistd.h>
  21.  
  22. #include <bsdsocket.h>
  23. #include "Python.h"
  24. #include "protos.h"
  25.  
  26. #include "netlib.h"
  27.  
  28. extern int (*__closefunc)(int);
  29.  
  30. __stdargs int
  31. __open(const char *name, int mode, ...)
  32. {
  33.   struct UFB *ufb;
  34.   int         fd;
  35.   int         flags;
  36.   char        newfile = TRUE;
  37.   BPTR        file;
  38.  
  39.   /*
  40.    * Set up __closefunc (which is used at cleanup)
  41.    */
  42.   __closefunc = __close;
  43.  
  44.   /*
  45.    * Check for the break signals
  46.    */
  47.   __chkabort();
  48.  
  49.   /*
  50.    * find first free ufb
  51.    */
  52.   ufb = __allocufb(&fd);
  53.   if (ufb == NULL)
  54.     return -1; /* errno is set by the __allocufb() */
  55.  
  56.   /*
  57.    * malloc space for the name & copy it
  58.    */
  59.   if ((ufb->ufbfn = malloc(strlen(name)+1)) == NULL) {
  60.     SET_OSERR(ERROR_NO_FREE_STORE);
  61.     errno = ENOMEM;
  62.     return -1;
  63.   }
  64.   strcpy(ufb->ufbfn, name);
  65.   /*
  66.    * Translate mode to ufb flags
  67.    */
  68.   switch (mode & (O_WRONLY | O_RDWR)) {
  69.   case O_RDONLY:
  70.     if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
  71.       errno = EINVAL;
  72.       return -1;
  73.     }
  74.     flags = UFB_RA;
  75.     break;
  76.   case O_WRONLY:
  77.     flags = UFB_WA;
  78.     break;
  79.   case O_RDWR:
  80.     flags = UFB_RA | UFB_WA;
  81.     break;
  82.   default:
  83.     errno = EINVAL;
  84.     return -1;
  85.   }
  86.   if (mode & O_APPEND)
  87.     flags |= UFB_APP;
  88.   if (mode & O_XLATE)
  89.     flags |= UFB_XLAT;
  90.   if (mode & O_TEMP)
  91.     flags |= UFB_TEMP;
  92.   if (mode & O_CREAT) {
  93.     BPTR lock;
  94.     if (lock = Lock((char *)name, SHARED_LOCK)) {
  95.       if (mode & O_EXCL) {
  96.     UnLock(lock);
  97.     errno = EEXIST;
  98.     free(ufb->ufbfn);
  99.     return -1;
  100.       }
  101.  
  102.       if (mode & O_TRUNC)
  103.     newfile = FALSE;
  104.       else
  105.     mode &= ~O_CREAT;
  106.  
  107.       UnLock(lock);
  108.     }
  109.   }
  110.   if (mode & O_CREAT) {
  111.     if ((file = Open((char *)name, MODE_NEWFILE)) == NULL)
  112.       goto osfail;
  113.  
  114.     if (newfile) {
  115.       va_list va;
  116.       int cmode;
  117.  
  118.       va_start(va, mode);
  119.  
  120.     /* needs usergroup.library -- I.J. */
  121.     if(!checkusergrouplib())
  122.     {
  123.         PyErr_Clear();
  124.         cmode=va_arg(va,int);        /* don't use getumask() */
  125.     }
  126.     else
  127.         cmode = va_arg(va, int) & ~getumask();
  128.       
  129.       chmod((char *)name, cmode); /* hope this doesn't fail :-) */
  130.     }
  131.   }
  132.   else {
  133.     if ((file = Open((char *)name,
  134.              (flags & UFB_WA && mode & O_LOCK) ? 
  135.              MODE_READWRITE : MODE_OLDFILE)) == NULL)
  136.       goto osfail;
  137.   }
  138.  
  139.   /*
  140.    * All done! Setting the ufb->ufbflg field to non-zero value marks
  141.    * this ufb used. 
  142.    */
  143.   ufb->ufbflg = flags;
  144.   ufb->ufbfh = (long)file;
  145.  
  146.   return fd;
  147.  
  148. osfail:
  149.   {
  150.     int code = IoErr();
  151.     if (ufb->ufbfn)
  152.       free(ufb->ufbfn);
  153.     set_errno(code);
  154.   }
  155.   return -1;
  156. }
  157.