/contrib/cvs/lib/rename.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 80 lines · 49 code · 9 blank · 22 comment · 17 complexity · d2e174a8b4ec0f4a42f6fbf2bbf94f57 MD5 · raw file

  1. /* rename.c -- BSD compatible directory function for System V
  2. Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details. */
  11. #ifdef HAVE_CONFIG_H
  12. #include "config.h"
  13. #endif
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #ifndef STDC_HEADERS
  18. extern int errno;
  19. #endif
  20. /* Rename file FROM to file TO.
  21. Return 0 if successful, -1 if not. */
  22. int
  23. rename (from, to)
  24. char *from;
  25. char *to;
  26. {
  27. struct stat from_stats;
  28. int pid, status;
  29. if (stat (from, &from_stats) == 0)
  30. {
  31. /* We don't check existence_error because the systems which need it
  32. have rename(). */
  33. if (CVS_UNLINK (to) && errno != ENOENT)
  34. return -1;
  35. if ((from_stats.st_mode & S_IFMT) == S_IFDIR)
  36. {
  37. #ifdef MVDIR
  38. /* I don't think MVDIR ever gets defined, but I don't think
  39. it matters, because I don't think CVS ever calls rename()
  40. on directories. */
  41. /* Need a setuid root process to link and unlink directories. */
  42. pid = fork ();
  43. switch (pid)
  44. {
  45. case -1: /* Error. */
  46. error (1, errno, "cannot fork");
  47. case 0: /* Child. */
  48. execl (MVDIR, "mvdir", from, to, (char *) 0);
  49. error (255, errno, "cannot run `%s'", MVDIR);
  50. default: /* Parent. */
  51. while (wait (&status) != pid)
  52. /* Do nothing. */ ;
  53. errno = 0; /* mvdir printed the system error message. */
  54. return status != 0 ? -1 : 0;
  55. }
  56. #else /* no MVDIR */
  57. error (1, 0, "internal error: cannot move directories");
  58. #endif /* no MVDIR */
  59. }
  60. else
  61. {
  62. /* We don't check existence_error because the systems which need it
  63. have rename(). */
  64. if (link (from, to) == 0 && (CVS_UNLINK (from) == 0 || errno == ENOENT))
  65. return 0;
  66. }
  67. }
  68. return -1;
  69. }