xref: /openbsd-src/gnu/usr.bin/cvs/lib/rename.c (revision 43c1707e6f6829177cb1974ee6615ce6c1307689)
11e72d8d2Sderaadt /* rename.c -- BSD compatible directory function for System V
21e72d8d2Sderaadt    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
31e72d8d2Sderaadt 
41e72d8d2Sderaadt    This program is free software; you can redistribute it and/or modify
51e72d8d2Sderaadt    it under the terms of the GNU General Public License as published by
61e72d8d2Sderaadt    the Free Software Foundation; either version 2, or (at your option)
71e72d8d2Sderaadt    any later version.
81e72d8d2Sderaadt 
91e72d8d2Sderaadt    This program is distributed in the hope that it will be useful,
101e72d8d2Sderaadt    but WITHOUT ANY WARRANTY; without even the implied warranty of
111e72d8d2Sderaadt    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12461cc63eStholo    GNU General Public License for more details.  */
131e72d8d2Sderaadt 
141e72d8d2Sderaadt #ifdef HAVE_CONFIG_H
151e72d8d2Sderaadt #include "config.h"
161e72d8d2Sderaadt #endif
171e72d8d2Sderaadt 
181e72d8d2Sderaadt #include <sys/types.h>
191e72d8d2Sderaadt #include <sys/stat.h>
201e72d8d2Sderaadt #include <errno.h>
211e72d8d2Sderaadt #ifndef STDC_HEADERS
221e72d8d2Sderaadt extern int errno;
231e72d8d2Sderaadt #endif
241e72d8d2Sderaadt 
251e72d8d2Sderaadt /* Rename file FROM to file TO.
261e72d8d2Sderaadt    Return 0 if successful, -1 if not. */
271e72d8d2Sderaadt 
281e72d8d2Sderaadt int
rename(from,to)291e72d8d2Sderaadt rename (from, to)
301e72d8d2Sderaadt      char *from;
311e72d8d2Sderaadt      char *to;
321e72d8d2Sderaadt {
331e72d8d2Sderaadt   struct stat from_stats;
341e72d8d2Sderaadt   int pid, status;
351e72d8d2Sderaadt 
361e72d8d2Sderaadt   if (stat (from, &from_stats) == 0)
371e72d8d2Sderaadt     {
3813571821Stholo       /* We don't check existence_error because the systems which need it
3913571821Stholo 	 have rename().  */
40*43c1707eStholo       if (CVS_UNLINK (to) && errno != ENOENT)
411e72d8d2Sderaadt 	return -1;
421e72d8d2Sderaadt       if ((from_stats.st_mode & S_IFMT) == S_IFDIR)
431e72d8d2Sderaadt 	{
441e72d8d2Sderaadt #ifdef MVDIR
451e72d8d2Sderaadt 	  /* I don't think MVDIR ever gets defined, but I don't think
461e72d8d2Sderaadt 	     it matters, because I don't think CVS ever calls rename()
471e72d8d2Sderaadt 	     on directories.  */
481e72d8d2Sderaadt 
491e72d8d2Sderaadt 	  /* Need a setuid root process to link and unlink directories. */
501e72d8d2Sderaadt 	  pid = fork ();
511e72d8d2Sderaadt 	  switch (pid)
521e72d8d2Sderaadt 	    {
531e72d8d2Sderaadt 	    case -1:		/* Error. */
541e72d8d2Sderaadt 	      error (1, errno, "cannot fork");
551e72d8d2Sderaadt 
561e72d8d2Sderaadt 	    case 0:		/* Child. */
571e72d8d2Sderaadt 	      execl (MVDIR, "mvdir", from, to, (char *) 0);
581e72d8d2Sderaadt 	      error (255, errno, "cannot run `%s'", MVDIR);
591e72d8d2Sderaadt 
601e72d8d2Sderaadt 	    default:		/* Parent. */
611e72d8d2Sderaadt 	      while (wait (&status) != pid)
621e72d8d2Sderaadt 		/* Do nothing. */ ;
631e72d8d2Sderaadt 
641e72d8d2Sderaadt 	      errno = 0;	/* mvdir printed the system error message. */
651e72d8d2Sderaadt 	      return status != 0 ? -1 : 0;
661e72d8d2Sderaadt 	    }
671e72d8d2Sderaadt #else /* no MVDIR */
681e72d8d2Sderaadt 	  error (1, 0, "internal error: cannot move directories");
691e72d8d2Sderaadt #endif /* no MVDIR */
701e72d8d2Sderaadt 	}
711e72d8d2Sderaadt       else
721e72d8d2Sderaadt 	{
7313571821Stholo 	  /* We don't check existence_error because the systems which need it
7413571821Stholo 	     have rename().  */
75*43c1707eStholo 	  if (link (from, to) == 0 && (CVS_UNLINK (from) == 0 || errno == ENOENT))
761e72d8d2Sderaadt 	    return 0;
771e72d8d2Sderaadt 	}
781e72d8d2Sderaadt     }
791e72d8d2Sderaadt   return -1;
801e72d8d2Sderaadt }
81