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-10-01  |  8KB  |  282 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: open.c,v 1.4 1994/06/19 15:14:07 rluebbert Exp $
  21.  *
  22.  *  $Log: open.c,v $
  23.  *  Revision 1.4  1994/06/19  15:14:07  rluebbert
  24.  *  *** empty log message ***
  25.  *
  26.  *  Revision 1.2  1992/07/28  00:32:04  mwild
  27.  *  pass convert_dir the original signal mask, to check for pending signals
  28.  *
  29.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  30.  *  Initial revision
  31.  *
  32.  */
  33.  
  34. #define _KERNEL
  35. #include "ixemul.h"
  36. #include "kprintf.h"
  37.  
  38. #include <string.h>
  39.  
  40. /* standard functions.. could get overridden in a network environment */
  41. extern int __ioctl (), __fselect (), __close ();
  42.  
  43. /* "normal" functions, means do half-async writes & sync reads */
  44. extern int __write (), __read (), __open ();
  45.  
  46. /* incore functions */
  47. extern int __mread (), __mclose ();
  48.  
  49. extern int __ioerr_to_errno ();
  50.  
  51.  
  52. int
  53. open (char *name, int mode, int perms)
  54. {
  55.   int fd;
  56.   struct file *f;
  57.   BPTR fh;
  58.   int late_stat;
  59.   int omask, error;
  60.   char ptymask = 0;
  61.   int ptyindex = 0;
  62.   int amode = 0, i;
  63.  
  64.   if (name == NULL)     /* sanity check */
  65.     return EACCES;
  66.   mode = FFLAGS(mode);
  67.  
  68.   /* inhibit signals */
  69.   omask = syscall (SYS_sigsetmask, ~0);
  70.  
  71.   error = falloc (&f, &fd);
  72.   if (error)
  73.     {
  74.       syscall (SYS_sigsetmask, omask);
  75.       errno = error;
  76.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  77.       return -1;
  78.     }
  79.   /* we now got the file, ie. since its count is > 0, no other process
  80.    * will get it with falloc() */
  81.  
  82.   /* init those fields */
  83.   f->f_stb_dirty = 0;
  84.   late_stat = 0;
  85.   if (stat(name, &f->f_stb) < 0)
  86.     {
  87.       /* there can mainly be two reasons for stat() to fail. Either the
  88.        * file really doesn't exist (ENOENT), or then the filesystem/handler
  89.        * doesn't support file locks. */
  90.  
  91.       /* if we should get out of here without an error, init the stat
  92.        * buffer after having opened the file with __fstat, this sets some
  93.        * reasonable parameters (see end of function). */
  94.       late_stat = 1;
  95.  
  96.       if ((errno == ENOENT) && (mode & O_CREAT))
  97.     {
  98.       /* can't set permissions on an open file, so this has to be done
  99.        * by 'close' */
  100.       f->f_stb.st_mode = (perms & ~u.u_cmask);
  101.       f->f_stb_dirty = 1;
  102.     }
  103.     }
  104.  
  105.   f->f_flags = mode & FMASK;
  106.   f->f_ttyflags = IXTTY_ICRNL | IXTTY_OPOST | IXTTY_ONLCR;
  107.  
  108.   /* initialise the packet. The only thing needed at this time is its
  109.    * header, filling in of port, action & args will be done when it's
  110.    * used */
  111.   __init_std_packet (&f->f_sp);
  112.   __init_std_packet (&f->f_select_sp);
  113.  
  114.   /* check for case-sensitive filename */
  115.   if ((mode & O_CASE) && !late_stat && filenamecmp(name))
  116.     {
  117.       error = ENOENT;
  118.       goto error;
  119.     }
  120.  
  121.   /* ok, so lets try to open the file... */
  122.  
  123.   /* do this *only* if the stat() above was successful !! */
  124.   if (!late_stat && S_ISDIR (f->f_stb.st_mode) && !(mode & FWRITE))
  125.     {
  126.       if (convert_dir (f, name, omask))
  127.         goto ret_ok;
  128.       else
  129.     goto error;
  130.     }
  131.  
  132.   /* filter invalid modes */
  133.   switch (mode & (O_CREAT|O_TRUNC|O_EXCL))
  134.     {
  135.     case O_EXCL:
  136.     case O_EXCL|O_TRUNC:
  137.       /* can never succeed ! */
  138.       error = EINVAL;
  139.       goto error;
  140.  
  141.     case O_CREAT|O_EXCL:
  142.     case O_CREAT|O_EXCL|O_TRUNC:
  143.       if (! late_stat)
  144.         {
  145.       error = EEXIST;
  146.       goto error;
  147.     }
  148.       break;
  149.     }
  150.  
  151.   amode = (mode & O_CREAT) ? MODE_READWRITE : MODE_OLDFILE;
  152.  
  153.   if (!strcmp(name, "/dev/tty"))
  154.     name = "*";
  155.   else if ((i = is_pseudoterminal(name)))
  156.     {
  157.       char *orig_name = name;
  158.       char mask;
  159.  
  160.       name = "/fifo/ptyXX/rweksm";
  161.       memcpy(name + 7, orig_name + i + 1, 4);
  162.       name[17] = (orig_name[i] == 'p' ? 'm' : 'c');
  163.       mask = (name[17] == 'm' ? IX_PTY_MASTER : IX_PTY_SLAVE);
  164.       ptyindex = (name[9] - 'p') * 16 + name[10] - (name[10] >= 'a' ? 'a' - 10 : '0');
  165.       ix_lock_base();
  166.       if (ix.ix_ptys[ptyindex] & mask)
  167.         {
  168.           ix_unlock_base();
  169.           error = EIO;
  170.           goto error;
  171.         }
  172.       ptymask = mask;
  173.       ix.ix_ptys[ptyindex] |= (mask & IX_PTY_OPEN); /* mark pty in use */
  174.       ix_unlock_base();
  175.     }
  176.   do
  177.   {
  178.     if (!strcmp(name, "*") || !strcasecmp(name, "console:"))
  179.       /* Temporary patch for KingCON 1.3, which seems to have problems with
  180.      ACTION_FINDINPUT of "*"/"console:" when "dp_Port" of the packet is
  181.      not set to sender's "pr_MsgPort" - that's what IXEmul makes on
  182.      clients' startup during initialization of "stderr". */
  183.       fh = Open(name, amode);
  184.     else
  185.       fh = __open (name, amode);
  186.  
  187.     if (! fh)
  188.       {
  189.         int err = IoErr();
  190.  
  191.         /* For those handlers that do not understand MODE_READWRITE (e.g. PAR: ) */
  192.         if (err == ERROR_ACTION_NOT_KNOWN && amode == MODE_READWRITE)
  193.         {
  194.           amode = MODE_NEWFILE;
  195.         }
  196.         else
  197.         {
  198.           error = __ioerr_to_errno (err);
  199.           goto error;
  200.         }
  201.       }
  202.   } while (!fh);
  203.  
  204.   /* now.. we're lucky, we actually opened the file! */
  205.   f->f_fh = (struct FileHandle *) BTOCPTR(fh);
  206.  
  207.   if (mode & FWRITE)
  208.     f->f_write  = __write;
  209.   if (mode & FREAD)
  210.     f->f_read   = __read;
  211.  
  212.   f->f_ioctl  = __ioctl;
  213.   f->f_select = __fselect;
  214.   f->f_close  = __close;
  215.   f->f_type   = DTYPE_FILE;
  216.  
  217.   /*
  218.    * have to use kmalloc() instead of malloc(), because this is no task-private
  219.    * data, it could (in the future) be shared by other tasks 
  220.    */
  221.   f->f_name = (void *) kmalloc (strlen (name) + 1);
  222.   if (f->f_name)
  223.     strcpy (f->f_name, name);
  224.  
  225. ret_ok:
  226.   /* ok, we're almost done. If desired, init the stat buffer to the
  227.    * information we can get from an open file descriptor */
  228.   if (late_stat) __fstat (f);
  229.   
  230.   /* if the file qualifies, try to change it into a DTYPE_MEM file */
  231.   if (!late_stat && f->f_type == DTYPE_FILE 
  232.       && f->f_stb.st_size < ix.ix_membuf_limit && mode == FREAD)
  233.     {
  234.       void *buf;
  235.       
  236.       /* try to obtain the needed memory */
  237.       buf = (void *) kmalloc (f->f_stb.st_size);
  238.       if (buf)
  239.     if (syscall (SYS_read, fd, buf, f->f_stb.st_size) == f->f_stb.st_size)
  240.       {
  241.         __Close (CTOBPTR (f->f_fh));
  242.         f->f_type         = DTYPE_MEM;
  243.         f->f_mf.mf_offset     = 0;
  244.         f->f_mf.mf_buffer     = buf;
  245.         f->f_read        = __mread;
  246.         f->f_close         = __mclose;
  247.         f->f_ioctl        = 0;
  248.         f->f_select        = 0;
  249.       }
  250.     else
  251.       kfree (buf);
  252.     }
  253.  
  254.   syscall (SYS_sigsetmask, omask);
  255.  
  256.   if (error)
  257.     {
  258.       errno = error;
  259.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  260.     }
  261.   if (!error && (mode & O_TRUNC) && (amode != MODE_NEWFILE))
  262.     syscall(SYS_ftruncate, fd, 0);
  263.  
  264.   /* return the descriptor */
  265.   return fd;
  266.  
  267. error:
  268.   /* free the file */
  269.   u.u_ofile[fd] = 0;
  270.   if (ptymask)
  271.     {
  272.       ix_lock_base();
  273.       ix.ix_ptys[ptyindex] &= ~(ptymask & IX_PTY_OPEN);
  274.       ix_unlock_base();
  275.     }
  276.   f->f_count--;
  277.   syscall (SYS_sigsetmask, omask);
  278.   errno = error;
  279.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  280.   return -1;
  281. }
  282.