dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / comp / rename.c
blobc46c2389d58aa68ddf3e50418df7a36234ee8eed
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
24 #include <ast.h>
26 #if _lib_rename
28 NoN(rename)
30 #else
32 #include <error.h>
33 #include <proc.h>
35 #ifdef EPERM
37 static int
38 mvdir(const char* from, const char* to)
40 char* argv[4];
41 int oerrno;
43 static const char mvdir[] = "/usr/lib/mv_dir";
45 oerrno = errno;
46 if (!eaccess(mvdir, X_OK))
48 argv[0] = mvdir;
49 argv[1] = from;
50 argv[2] = to;
51 argv[3] = 0;
52 if (!procrun(argv[0], argv, 0))
54 errno = oerrno;
55 return 0;
58 errno = EPERM;
59 return -1;
62 #endif
64 int
65 rename(const char* from, const char* to)
67 int oerrno;
68 int ooerrno;
70 ooerrno = errno;
71 while (link(from, to))
73 #ifdef EPERM
74 if (errno == EPERM)
76 errno = ooerrno;
77 return mvdir(from, to);
79 #endif
80 oerrno = errno;
81 if (unlink(to))
83 #ifdef EPERM
84 if (errno == EPERM)
86 errno = ooerrno;
87 return mvdir(from, to);
89 #endif
90 errno = oerrno;
91 return -1;
94 errno = ooerrno;
95 return unlink(from);
98 #endif