home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / __close.c < prev    next >
C/C++ Source or Header  |  1996-12-20  |  3KB  |  101 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.  *  $Id: __close.c,v 1.2 1994/06/19 15:18:48 rluebbert Exp $
  20.  *
  21.  *  $Log: __close.c,v $
  22.  *  Revision 1.2  1994/06/19  15:18:48  rluebbert
  23.  *  *** empty log message ***
  24.  *
  25.  */
  26.  
  27. #define _KERNEL
  28. #include "ixemul.h"
  29. #include "kprintf.h"
  30. #include "select.h"
  31. #include <libraries/fifo.h>
  32. #include <rexx/rexxio.h>
  33.  
  34. int
  35. __close(struct file *f)
  36. {
  37.   if (f->f_count == 1)
  38.     {
  39.       if (!memcmp(f->f_name, "/fifo/pty", 9) && SELPKT_IN_USE(f))
  40.         {
  41.           SendPacket3(f, __srwport, ACTION_STACK, f->f_fh->fh_Arg1, (int)"\n", 1);
  42.           __wait_sync_packet(&f->f_sp);
  43.         }
  44.       __wait_select_packet(&f->f_select_sp);
  45.     }
  46.  
  47.   ix_lock_base ();
  48.  
  49.   f->f_count--;
  50.  
  51.   KPRINTF (("__close: closing file $%lx, f_count now %ld.\n", f, f->f_count));
  52.   if (f->f_count > 0) goto unlock;
  53.  
  54.   if (!(f->f_flags & FEXTOPEN))
  55.     {
  56.       __Close (CTOBPTR (f->f_fh));
  57.  
  58.       if (!memcmp(f->f_name, "/fifo/pty", 9))
  59.         {
  60.       int i = (f->f_name[9] - 'p') * 16 + f->f_name[10] - (f->f_name[10] >= 'a' ? 'a' - 10 : '0');
  61.             char mask = (f->f_name[17] == 'm' ? IX_PTY_MASTER : IX_PTY_SLAVE);
  62.       
  63.       ix.ix_ptys[i] &= ~(mask & IX_PTY_OPEN);
  64.       ix.ix_ptys[i] |= mask & IX_PTY_CLOSE;
  65.       if (!(ix.ix_ptys[i] & IX_PTY_OPEN))
  66.         ix.ix_ptys[i] = 0;  /* both slave and master are closed, so free this pty */
  67.         }
  68.     }
  69.  
  70.   /* if we have to set some modes, do it now */
  71.   if (f->f_stb_dirty & FSDF_MODE)
  72.     {
  73.       syscall (SYS_chmod, f->f_name, f->f_stb.st_mode);
  74.       /* should later also set modification time */
  75.     }
  76.  
  77.   /* if we have to set some ids, do it now */
  78.   if (f->f_stb_dirty & FSDF_OWNER)
  79.     {
  80.       syscall (SYS_chown, f->f_name, f->f_stb.st_uid, f->f_stb.st_gid);
  81.       /* should later also set modification time */
  82.     }
  83.  
  84.   if (f->f_flags & FUNLINK) syscall (SYS_unlink, f->f_name);
  85.  
  86.   /* NOTE: the FEXTOPEN flag tells us two things: 1) we shouldn't
  87.    * perform a Close() on the filehandle, and 2) the name in the
  88.    * file structure wasn't allocated by malloc(), so we shouldn't 
  89.    * free() it either. */
  90.   if (!(f->f_flags & FEXTOPEN) && f->f_name) 
  91.     {
  92.       KPRINTF (("f->f_name(%s) ", f->f_name));
  93.       kfree (f->f_name);
  94.     }
  95.  
  96. unlock:
  97.   ix_unlock_base ();
  98.  
  99.   return 0;
  100. }
  101.