1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin #include <ast.h>
25*4887Schin 
26*4887Schin #if _lib_rename
27*4887Schin 
28*4887Schin NoN(rename)
29*4887Schin 
30*4887Schin #else
31*4887Schin 
32*4887Schin #include <error.h>
33*4887Schin #include <proc.h>
34*4887Schin 
35*4887Schin #ifdef EPERM
36*4887Schin 
37*4887Schin static int
38*4887Schin mvdir(const char* from, const char* to)
39*4887Schin {
40*4887Schin 	char*			argv[4];
41*4887Schin 	int			oerrno;
42*4887Schin 
43*4887Schin 	static const char	mvdir[] = "/usr/lib/mv_dir";
44*4887Schin 
45*4887Schin 	oerrno = errno;
46*4887Schin 	if (!eaccess(mvdir, X_OK))
47*4887Schin 	{
48*4887Schin 		argv[0] = mvdir;
49*4887Schin 		argv[1] = from;
50*4887Schin 		argv[2] = to;
51*4887Schin 		argv[3] = 0;
52*4887Schin 		if (!procrun(argv[0], argv))
53*4887Schin 		{
54*4887Schin 			errno = oerrno;
55*4887Schin 			return 0;
56*4887Schin 		}
57*4887Schin 	}
58*4887Schin 	errno = EPERM;
59*4887Schin 	return -1;
60*4887Schin }
61*4887Schin 
62*4887Schin #endif
63*4887Schin 
64*4887Schin int
65*4887Schin rename(const char* from, const char* to)
66*4887Schin {
67*4887Schin 	int	oerrno;
68*4887Schin 	int	ooerrno;
69*4887Schin 
70*4887Schin 	ooerrno = errno;
71*4887Schin 	while (link(from, to))
72*4887Schin 	{
73*4887Schin #ifdef EPERM
74*4887Schin 		if (errno == EPERM)
75*4887Schin 		{
76*4887Schin 			errno = ooerrno;
77*4887Schin 			return mvdir(from, to);
78*4887Schin 		}
79*4887Schin #endif
80*4887Schin 		oerrno = errno;
81*4887Schin 		if (unlink(to))
82*4887Schin 		{
83*4887Schin #ifdef EPERM
84*4887Schin 			if (errno == EPERM)
85*4887Schin 			{
86*4887Schin 				errno = ooerrno;
87*4887Schin 				return mvdir(from, to);
88*4887Schin 			}
89*4887Schin #endif
90*4887Schin 			errno = oerrno;
91*4887Schin 			return -1;
92*4887Schin 		}
93*4887Schin 	}
94*4887Schin 	errno = ooerrno;
95*4887Schin 	return unlink(from);
96*4887Schin }
97*4887Schin 
98*4887Schin #endif
99