xref: /openbsd-src/gnu/lib/libiberty/src/rename.c (revision 150b7e42cfa21e6546d96ae514ca23e80d970ac7)
100bf4279Sespie /* rename -- rename a file
200bf4279Sespie    This function is in the public domain. */
300bf4279Sespie 
437c53322Sespie /*
537c53322Sespie 
637c53322Sespie @deftypefn Supplemental int rename (const char *@var{old}, const char *@var{new})
737c53322Sespie 
837c53322Sespie Renames a file from @var{old} to @var{new}.  If @var{new} already
937c53322Sespie exists, it is removed.
1037c53322Sespie 
1137c53322Sespie @end deftypefn
1237c53322Sespie 
1337c53322Sespie */
1400bf4279Sespie 
15707f648cSespie #include "ansidecl.h"
16f5dd06f4Sespie #ifdef HAVE_CONFIG_H
17f5dd06f4Sespie #include "config.h"
18f5dd06f4Sespie #endif
1900bf4279Sespie #include <errno.h>
20f5dd06f4Sespie #ifdef HAVE_UNISTD_H
21f5dd06f4Sespie #include <unistd.h>
22f5dd06f4Sespie #endif
2300bf4279Sespie 
2400bf4279Sespie int
rename(const char * zfrom,const char * zto)25*150b7e42Smiod rename (const char *zfrom, const char *zto)
2600bf4279Sespie {
2700bf4279Sespie   if (link (zfrom, zto) < 0)
2800bf4279Sespie     {
2900bf4279Sespie       if (errno != EEXIST)
3000bf4279Sespie 	return -1;
3100bf4279Sespie       if (unlink (zto) < 0
3200bf4279Sespie 	  || link (zfrom, zto) < 0)
3300bf4279Sespie 	return -1;
3400bf4279Sespie     }
3500bf4279Sespie   return unlink (zfrom);
3600bf4279Sespie }
37