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