rename.c 633 B

123456789101112131415161718192021222324252627282930313233343536
  1. /* rename -- rename a file
  2. This function is in the public domain. */
  3. /*
  4. @deftypefn Supplemental int rename (const char *@var{old}, const char *@var{new})
  5. Renames a file from @var{old} to @var{new}. If @var{new} already
  6. exists, it is removed.
  7. @end deftypefn
  8. */
  9. #include "ansidecl.h"
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <errno.h>
  14. #ifdef HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. int
  18. rename (const char *zfrom, const char *zto)
  19. {
  20. if (link (zfrom, zto) < 0)
  21. {
  22. if (errno != EEXIST)
  23. return -1;
  24. if (unlink (zto) < 0
  25. || link (zfrom, zto) < 0)
  26. return -1;
  27. }
  28. return unlink (zfrom);
  29. }