xref: /onnv-gate/usr/src/lib/libast/common/comp/rename.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin #include <ast.h>
254887Schin 
264887Schin #if _lib_rename
274887Schin 
284887Schin NoN(rename)
294887Schin 
304887Schin #else
314887Schin 
324887Schin #include <error.h>
334887Schin #include <proc.h>
344887Schin 
354887Schin #ifdef EPERM
364887Schin 
374887Schin static int
384887Schin mvdir(const char* from, const char* to)
394887Schin {
404887Schin 	char*			argv[4];
414887Schin 	int			oerrno;
424887Schin 
434887Schin 	static const char	mvdir[] = "/usr/lib/mv_dir";
444887Schin 
454887Schin 	oerrno = errno;
464887Schin 	if (!eaccess(mvdir, X_OK))
474887Schin 	{
484887Schin 		argv[0] = mvdir;
494887Schin 		argv[1] = from;
504887Schin 		argv[2] = to;
514887Schin 		argv[3] = 0;
528462SApril.Chin@Sun.COM 		if (!procrun(argv[0], argv, 0))
534887Schin 		{
544887Schin 			errno = oerrno;
554887Schin 			return 0;
564887Schin 		}
574887Schin 	}
584887Schin 	errno = EPERM;
594887Schin 	return -1;
604887Schin }
614887Schin 
624887Schin #endif
634887Schin 
644887Schin int
654887Schin rename(const char* from, const char* to)
664887Schin {
674887Schin 	int	oerrno;
684887Schin 	int	ooerrno;
694887Schin 
704887Schin 	ooerrno = errno;
714887Schin 	while (link(from, to))
724887Schin 	{
734887Schin #ifdef EPERM
744887Schin 		if (errno == EPERM)
754887Schin 		{
764887Schin 			errno = ooerrno;
774887Schin 			return mvdir(from, to);
784887Schin 		}
794887Schin #endif
804887Schin 		oerrno = errno;
814887Schin 		if (unlink(to))
824887Schin 		{
834887Schin #ifdef EPERM
844887Schin 			if (errno == EPERM)
854887Schin 			{
864887Schin 				errno = ooerrno;
874887Schin 				return mvdir(from, to);
884887Schin 			}
894887Schin #endif
904887Schin 			errno = oerrno;
914887Schin 			return -1;
924887Schin 		}
934887Schin 	}
944887Schin 	errno = ooerrno;
954887Schin 	return unlink(from);
964887Schin }
974887Schin 
984887Schin #endif
99