home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / __open.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  118 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #define _KERNEL
  21. #include <string.h>
  22. #include "ixemul.h"
  23.  
  24. #include <utility/tagitem.h>
  25. #include <dos/dostags.h>
  26.  
  27. struct open_vec {
  28.   int action;
  29.   struct FileHandle *fh;
  30. };
  31.  
  32. static int
  33. __open_func (struct StandardPacket *sp, struct MsgPort *handler,
  34.              BPTR parent_lock,
  35.          BSTR name,
  36.          struct open_vec *ov, int *no_error)
  37. {
  38.   sp->sp_Pkt.dp_Type = ov->action;
  39.   sp->sp_Pkt.dp_Arg1 = CTOBPTR (ov->fh);
  40.   sp->sp_Pkt.dp_Arg2 = parent_lock;
  41.   sp->sp_Pkt.dp_Arg3 = name;
  42.  
  43.   PutPacket (handler, sp);
  44.   __wait_sync_packet (sp);
  45.  
  46.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  47.  
  48.   /* handler is supposed to supply this, but if it didn't... */
  49.   if (*no_error && !ov->fh->fh_Type) ov->fh->fh_Type = handler;
  50.  
  51.   /* continue if we failed because of symlink - reference */
  52.   return 1;
  53. }
  54.  
  55. BPTR
  56. __open (char *name, int action)
  57. {
  58.   unsigned long *fh;
  59.   struct open_vec ov;
  60.   BPTR res;
  61.   struct Process *this_proc = NULL;
  62.   APTR oldwin = NULL;
  63.   char buf[32], *s, *p = buf;
  64.  
  65.   if ((name[0] == '/' && strnicmp(name, "/dev/", 5)) ||
  66.       (index(name, ':') && strnicmp(name, "dev:", 4)))
  67.   {
  68.     s = (name[0] == '/') ? name + 1 : name;
  69.     while (*s && *s != '/' && *s != ':')
  70.       *p++ = *s++;
  71.     *p++ = ':';
  72.     *p = '\0';
  73.     if (ix.ix_flags & ix_no_insert_disk_requester)
  74.     {
  75.       this_proc = (struct Process *)FindTask (0);
  76.       oldwin = this_proc->pr_WindowPtr;
  77.       this_proc->pr_WindowPtr = (APTR)-1;
  78.     }
  79.     if (!IsFileSystem(buf))     /* if NAME is not a file system but */
  80.                                 /* something like a pipe or console, then */
  81.                                 /* do NOT use __plock() but call Open directly */
  82.     {
  83.       if (!*s)
  84.         res = Open(buf, action);
  85.       else if (*s == '/')
  86.       {
  87.         *s = ':';
  88.         res = Open(name + 1, action);
  89.         *s = '/';
  90.       }
  91.       else
  92.         res = Open(name, action);
  93.       if (ix.ix_flags & ix_no_insert_disk_requester)
  94.         this_proc->pr_WindowPtr = oldwin;
  95.       return res;
  96.     }
  97.     if (ix.ix_flags & ix_no_insert_disk_requester)
  98.       this_proc->pr_WindowPtr = oldwin;
  99.   }
  100.   
  101.   /* needed to get berserk ReadArgs() to behave sane, GRRR */
  102.   fh = AllocDosObjectTags (DOS_FILEHANDLE, ADO_FH_Mode, action, TAG_DONE);
  103.   if (! fh)
  104.     return 0;
  105.   ov.fh = (struct FileHandle *) fh;
  106.   ov.action = action;
  107.  
  108.   res = __plock (name, __open_func, &ov);
  109.   /* check for opening of NIL:, in which case we just return
  110.      a filehandle with a zero handle field */
  111.   if (! res && IoErr() == 4242)
  112.     res = -1;
  113.   
  114.   if (! res)
  115.     FreeDosObject (DOS_FILEHANDLE, fh);
  116.   return res ? CTOBPTR(ov.fh) : 0;
  117. }
  118.