Back to index

cell-binutils  2.17cvs20070401
rename.c
Go to the documentation of this file.
00001 /* rename -- rename a file
00002    This function is in the public domain. */
00003 
00004 /*
00005 
00006 @deftypefn Supplemental int rename (const char *@var{old}, const char *@var{new})
00007 
00008 Renames a file from @var{old} to @var{new}.  If @var{new} already
00009 exists, it is removed.
00010 
00011 @end deftypefn
00012 
00013 */
00014 
00015 #include "ansidecl.h"
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif
00019 #include <errno.h>
00020 #ifdef HAVE_UNISTD_H
00021 #include <unistd.h>
00022 #endif
00023 
00024 int
00025 rename (const char *zfrom, const char *zto)
00026 {
00027   if (link (zfrom, zto) < 0)
00028     {
00029       if (errno != EEXIST)
00030        return -1;
00031       if (unlink (zto) < 0
00032          || link (zfrom, zto) < 0)
00033        return -1;
00034     }
00035   return unlink (zfrom);
00036 }