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 / unlink.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  3KB  |  103 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.  *  unlink.c,v 1.1.1.1 1994/04/04 04:30:37 amiga Exp
  20.  *
  21.  *  unlink.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:37  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  */
  29.  
  30. #define _KERNEL
  31. #include "ixemul.h"
  32. #include "kprintf.h"
  33. #include <string.h>
  34.  
  35. static int
  36. __delete_func (struct StandardPacket *sp, struct MsgPort *handler,
  37.                BPTR parent_lock,
  38.            BSTR name,
  39.            void *dummy, int *no_error)
  40. {
  41.   sp->sp_Pkt.dp_Type = ACTION_DELETE_OBJECT;
  42.   sp->sp_Pkt.dp_Arg1 = parent_lock;
  43.   sp->sp_Pkt.dp_Arg2 = name;
  44.  
  45.   PutPacket (handler, sp);
  46.   __wait_sync_packet (sp);
  47.  
  48.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  49.  
  50.   /* stop if we failed because of symlink - reference */
  51.   return 0;
  52. }
  53.  
  54. int
  55. unlink (char *name)
  56. {
  57.   int err;
  58.   struct file *fp;
  59.   int result;
  60.   struct stat st;
  61.  
  62.   result = 0;
  63.  
  64.   if (syscall(SYS_stat, name, &st) == 0 && (st.st_mode & S_IWUSR) == 0)
  65.     if (syscall(SYS_chmod, name, st.st_mode | S_IWUSR))
  66.       return -1;
  67.   /* first try to normally delete the file, if we get an
  68.    * 'object in use' error, we'll try another way out.. */
  69.   if (__plock (name, __delete_func, 0))
  70.     return 0;
  71.  
  72.   /* so there was an error.. */
  73.   err = IoErr();
  74.   if (err == ERROR_OBJECT_IN_USE)
  75.     {
  76.       /* check whether the name is an absolute filename, if yes, we
  77.        * can walk thru our filetab to see, whether WE have this file
  78.        * open, and set its unlink-flag, if not, return the error */
  79.  
  80.       /* if we fully and consistent allow Unix-pathnames, we can test
  81.        * for a leading '/' too. */
  82.       if (index(name, ':') || ((ix.ix_flags & ix_translate_slash) && name[1] && name[0] == '/'))
  83.     {
  84.       ix_lock_base();
  85.       for (fp = ix.ix_file_tab; fp < ix.ix_fileNFILE; fp++)
  86.         if (fp->f_count && fp->f_name && !strcmp(fp->f_name, name))
  87.           {
  88.             fp->f_flags |= FUNLINK;
  89.             err = 0;
  90.           }
  91.       ix_unlock_base();
  92.     }
  93.     }
  94.   if (err)
  95.     {
  96.       errno = __ioerr_to_errno(err);
  97.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  98.       result = -1;
  99.     }
  100.  
  101.   return result;
  102. }
  103.