home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / rename.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  2.6 KB  |  121 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ *  @;
  7.  
  8.  
  9. 1.1
  10. date    92.05.14.19.55.40;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @rename a file
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  *  This file is part of ixemul.library for the Amiga.
  27.  *  Copyright (C) 1991, 1992  Markus M. Wild
  28.  *
  29.  *  This library is free software; you can redistribute it and/or
  30.  *  modify it under the terms of the GNU Library General Public
  31.  *  License as published by the Free Software Foundation; either
  32.  *  version 2 of the License, or (at your option) any later version.
  33.  *
  34.  *  This library is distributed in the hope that it will be useful,
  35.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  37.  *  Library General Public License for more details.
  38.  *
  39.  *  You should have received a copy of the GNU Library General Public
  40.  *  License along with this library; if not, write to the Free
  41.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42.  *
  43.  *  $Id$
  44.  *
  45.  *  $Log$
  46.  */
  47.  
  48. #define KERNEL
  49. #include "ixemul.h"
  50.  
  51. #if __GNUC__ != 2
  52. #define alloca __builtin_alloca
  53. #endif
  54.  
  55. struct rename_vec {
  56.   char *source_name;
  57.   BPTR target_dir_lock;
  58.   BSTR target_name;
  59. };
  60.  
  61.  
  62. static int
  63. __rename_func (struct StandardPacket *sp, struct MsgPort *handler,
  64.                BPTR parent_lock,
  65.            BSTR name,
  66.            struct rename_vec *rv, int *no_error)
  67. {
  68.   sp->sp_Pkt.dp_Type = ACTION_RENAME_OBJECT;
  69.   sp->sp_Pkt.dp_Arg1 = parent_lock;
  70.   sp->sp_Pkt.dp_Arg2 = name;
  71.   sp->sp_Pkt.dp_Arg3 = rv->target_dir_lock;
  72.   sp->sp_Pkt.dp_Arg4 = rv->target_name;
  73.   
  74.   PutPacket (handler, sp);
  75.   __wait_sync_packet (sp);
  76.  
  77.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  78.  
  79.   /* retry if necessary */
  80.   return 1;
  81. }
  82.  
  83. static int
  84. __get_target_data (struct StandardPacket *sp, struct MsgPort *handler,
  85.                    BPTR parent_lock,
  86.                BSTR name,
  87.                struct rename_vec *rv, int *no_error)
  88. {
  89.   rv->target_dir_lock = parent_lock;
  90.   rv->target_name = name;
  91.   
  92.   sp->sp_Pkt.dp_Res1 = __plock (rv->source_name, __rename_func, rv);
  93.   sp->sp_Pkt.dp_Res2 = ((struct Process *)FindTask(0))->pr_Result2;
  94.   
  95.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  96.   
  97.   /* don't retry */
  98.   return 0;
  99. }
  100.  
  101. int
  102. rename(char *from, char *to)
  103. {
  104.   int res;
  105.   struct rename_vec rv;
  106.  
  107.   rv.source_name = from;
  108.   
  109.   res = __plock (to, __get_target_data, &rv);
  110.   
  111.   if (res == 0 && IoErr() == ERROR_OBJECT_EXISTS)
  112.     {
  113.       syscall (SYS_unlink ,to);
  114.       res = __plock (to, __get_target_data, &rv);
  115.     }
  116.  
  117.   if (!res) errno = __ioerr_to_errno (IoErr ());
  118.   return res == -1 ? 0 : -1;
  119. }
  120. @
  121.