133965Sjdp/* rename -- rename a file
233965Sjdp   This function is in the public domain. */
333965Sjdp
489857Sobrien/*
533965Sjdp
689857Sobrien@deftypefn Supplemental int rename (const char *@var{old}, const char *@var{new})
789857Sobrien
889857SobrienRenames a file from @var{old} to @var{new}.  If @var{new} already
989857Sobrienexists, it is removed.
1089857Sobrien
1189857Sobrien@end deftypefn
1289857Sobrien
1389857Sobrien*/
1489857Sobrien
15130561Sobrien#include "ansidecl.h"
1677298Sobrien#ifdef HAVE_CONFIG_H
1777298Sobrien#include "config.h"
1877298Sobrien#endif
1933965Sjdp#include <errno.h>
2077298Sobrien#ifdef HAVE_UNISTD_H
2177298Sobrien#include <unistd.h>
2277298Sobrien#endif
2333965Sjdp
2433965Sjdpint
25218822Sdimrename (const char *zfrom, const char *zto)
2633965Sjdp{
2733965Sjdp  if (link (zfrom, zto) < 0)
2833965Sjdp    {
2933965Sjdp      if (errno != EEXIST)
3033965Sjdp	return -1;
3133965Sjdp      if (unlink (zto) < 0
3233965Sjdp	  || link (zfrom, zto) < 0)
3333965Sjdp	return -1;
3433965Sjdp    }
3533965Sjdp  return unlink (zfrom);
3633965Sjdp}
37