xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/lpfsck.c (revision e4fb8a5f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5e2738c5eSjacobs  * Common Development and Distribution License (the "License").
6e2738c5eSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e2738c5eSjacobs 
227c478bd9Sstevel@tonic-gate /*
23e2738c5eSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26f928ce67Sceastha 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "stdarg.h"
317c478bd9Sstevel@tonic-gate #include "stdlib.h"
327c478bd9Sstevel@tonic-gate #include "fcntl.h"
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include "lpsched.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static void check_link();
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /**
407c478bd9Sstevel@tonic-gate  ** lpfsck()
417c478bd9Sstevel@tonic-gate  **/
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	F	0
447c478bd9Sstevel@tonic-gate #define D	1
457c478bd9Sstevel@tonic-gate #define P	2
467c478bd9Sstevel@tonic-gate #define S	3
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void		proto (int, int, ...);
497c478bd9Sstevel@tonic-gate static int		va_makepath(va_list *, char **);
507c478bd9Sstevel@tonic-gate static void		_rename (char *, char *, ...);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate void
lpfsck(void)537c478bd9Sstevel@tonic-gate lpfsck(void)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	struct stat		stbuf;
567c478bd9Sstevel@tonic-gate 	int			real_am_in_background = am_in_background;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * Force log messages to go into the log file instead of stdout.
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	am_in_background = 1;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * Most of these lines repeat the prototype file from the
667c478bd9Sstevel@tonic-gate 	 * packaging and should match those items exactly.
677c478bd9Sstevel@tonic-gate 	 * (In fact, they probably ought to be generated from that file,
687c478bd9Sstevel@tonic-gate 	 * but that work is for a rainy day...)
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * DIRECTORIES:
737c478bd9Sstevel@tonic-gate 	 */
747c478bd9Sstevel@tonic-gate proto (D, 0,  Lp_A, NULL,			    0775, Lp_Uid, Lp_Gid);
757c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_A_Classes, NULL,		    0775, Lp_Uid, Lp_Gid);
767c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_A_Forms, NULL,			    0775, Lp_Uid, Lp_Gid);
777c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_A_Interfaces, NULL,		    0775, Lp_Uid, Lp_Gid);
787c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_A_Printers, NULL,		    0775, Lp_Uid, Lp_Gid);
797c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_A_PrintWheels, NULL,		    0775, Lp_Uid, Lp_Gid);
807c478bd9Sstevel@tonic-gate proto (D, 0,  "/var/lp", NULL,			    0775, Lp_Uid, Lp_Gid);
817c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Logs, NULL,			    0775, Lp_Uid, Lp_Gid);
827c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Spooldir, NULL,		    0775, Lp_Uid, Lp_Gid);
837c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Admins, NULL,			    0775, Lp_Uid, Lp_Gid);
847c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Requests, NULL,		    0775, Lp_Uid, Lp_Gid);
857c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Requests, Local_System, NULL,	    0770, Lp_Uid, Lp_Gid);
867c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_System, NULL,			    0775, Lp_Uid, Lp_Gid);
877c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Tmp, NULL,			    0771, Lp_Uid, Lp_Gid);
887c478bd9Sstevel@tonic-gate proto (D, 1,  Lp_Tmp, Local_System, NULL,	    0775, Lp_Uid, Lp_Gid);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * DIRECTORIES: not described in the packaging
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate proto (D, 0,  Lp_Spooldir, FIFOSDIR, NULL,	    0775, Lp_Uid, Lp_Gid);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
967c478bd9Sstevel@tonic-gate 	 * THE MAIN FIFO:
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate proto (P, 1,  Lp_FIFO, NULL,			    0666, Lp_Uid, Lp_Gid);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * SYMBOLIC LINKS:
1027c478bd9Sstevel@tonic-gate 	 * Watch out! These names are given in the reverse
1037c478bd9Sstevel@tonic-gate 	 * order found in the prototype file (sorry!)
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate proto (S, 1,  Lp_Model, NULL,			"/etc/lp/model", NULL);
1067c478bd9Sstevel@tonic-gate proto (S, 1,  Lp_Logs, NULL,			"/etc/lp/logs", NULL);
1077c478bd9Sstevel@tonic-gate /*     S, 1,  Lp_Tmp, Local_System, ...    DONE BELOW */
1087c478bd9Sstevel@tonic-gate proto (S, 1,  Lp_Bin, NULL,			Lp_Spooldir, "bin", NULL);
1097c478bd9Sstevel@tonic-gate proto (S, 1,  Lp_A, NULL,			Lp_Admins, "lp", NULL);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * OTHER FILES:
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * SPECIAL CASE:
1177c478bd9Sstevel@tonic-gate 	 * If the "temp" symbolic link already exists,
1187c478bd9Sstevel@tonic-gate 	 * but is not correct, assume the machine's nodename changed.
1197c478bd9Sstevel@tonic-gate 	 * Rename directories that include the nodename, if possible,
1207c478bd9Sstevel@tonic-gate 	 * so that unprinted requests are saved. Then change the
1217c478bd9Sstevel@tonic-gate 	 * symbolic link.
1227c478bd9Sstevel@tonic-gate 	 * Watch out for a ``symbolic link'' that isn't!
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	if (Lstat(Lp_Temp, &stbuf) == 0)
1257c478bd9Sstevel@tonic-gate 	    switch (stbuf.st_mode & S_IFMT) {
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	    default:
1287c478bd9Sstevel@tonic-gate 		Unlink (Lp_Temp);
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	    case S_IFDIR:
1327c478bd9Sstevel@tonic-gate 		Rmdir (Lp_Temp);
1337c478bd9Sstevel@tonic-gate 		break;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	    case S_IFLNK:
1367c478bd9Sstevel@tonic-gate 		check_link();
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 	    }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	proto(S, 1, Lp_Tmp, Local_System, NULL,	Lp_Temp, NULL);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	am_in_background = real_am_in_background;
1437c478bd9Sstevel@tonic-gate 	return;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static void
check_link()1477c478bd9Sstevel@tonic-gate check_link()
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	int len;
1507c478bd9Sstevel@tonic-gate 	char symbolic[MAXPATHLEN + 1];
1517c478bd9Sstevel@tonic-gate 	char *real_dir;
1527c478bd9Sstevel@tonic-gate 	char *old_system;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if ((len = Readlink(Lp_Temp, symbolic, MAXPATHLEN)) <= 0) {
1557c478bd9Sstevel@tonic-gate 		Unlink(Lp_Temp);
1567c478bd9Sstevel@tonic-gate 		return;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * If the symbolic link contained trailing slashes, remove
1617c478bd9Sstevel@tonic-gate 	 * them.
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	while ((len > 1) && (symbolic[len - 1] == '/')) {
1647c478bd9Sstevel@tonic-gate 		len--;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	symbolic[len] = 0;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* check that symlink points into /var/spool/lp/tmp */
1697c478bd9Sstevel@tonic-gate 	if (strncmp(Lp_Tmp, symbolic, strlen(Lp_Tmp)) != 0) {
1707c478bd9Sstevel@tonic-gate 		Unlink(Lp_Temp);
1717c478bd9Sstevel@tonic-gate 		return;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Check that symlink points to something.
1767c478bd9Sstevel@tonic-gate 	 * There should be at least 2 characters
1777c478bd9Sstevel@tonic-gate 	 * after the string '/var/spool/lp/tmp':
1787c478bd9Sstevel@tonic-gate 	 * a '/' and another character.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	if (len <= strlen(Lp_Tmp) + 1) {
1817c478bd9Sstevel@tonic-gate 		Unlink(Lp_Temp);
1827c478bd9Sstevel@tonic-gate 		return;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	real_dir = makepath(Lp_Tmp, Local_System, NULL);
1867c478bd9Sstevel@tonic-gate 	if (!STREQU(real_dir, symbolic)) {
1877c478bd9Sstevel@tonic-gate 		if (!(old_system = strrchr(symbolic, '/')))
1887c478bd9Sstevel@tonic-gate 			old_system = symbolic;
1897c478bd9Sstevel@tonic-gate 		else
1907c478bd9Sstevel@tonic-gate 			old_system++;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		/*
1937c478bd9Sstevel@tonic-gate 		 * The "rename()" system call (buried
1947c478bd9Sstevel@tonic-gate 		 * inside the "_rename()" routine) should
1957c478bd9Sstevel@tonic-gate 		 * succeed, even though we blindly created
1967c478bd9Sstevel@tonic-gate 		 * the new directory earlier, as the only
1977c478bd9Sstevel@tonic-gate 		 * directory entries should be . and ..
1987c478bd9Sstevel@tonic-gate 		 * (although if someone already created
1997c478bd9Sstevel@tonic-gate 		 * them, we'll note the fact).
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		_rename(old_system, Local_System, Lp_Tmp, NULL);
2027c478bd9Sstevel@tonic-gate 		_rename(old_system, Local_System, Lp_Requests, NULL);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		Unlink(Lp_Temp);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	Free(real_dir);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /**
2117c478bd9Sstevel@tonic-gate  ** proto()
2127c478bd9Sstevel@tonic-gate  **/
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static void
proto(int type,int rm_ok,...)2157c478bd9Sstevel@tonic-gate proto(int type, int rm_ok, ...)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	va_list			ap;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	char			*path,
2207c478bd9Sstevel@tonic-gate 				*symbolic;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	int			exist,
2237c478bd9Sstevel@tonic-gate 				err;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	mode_t			mode;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	uid_t			uid;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	gid_t			gid;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	struct stat		stbuf;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
234f928ce67Sceastha 	va_start(ap, rm_ok);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ((err = va_makepath(&ap, &path)) < 0)
2377c478bd9Sstevel@tonic-gate 		fail ("\"%s\" is a truncated name!\n", path);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	exist = (stat(path, &stbuf) == 0);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	switch (type) {
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	case S:
2447c478bd9Sstevel@tonic-gate 		if (!exist)
2457c478bd9Sstevel@tonic-gate 			fail ("%s is missing!\n", path);
2467c478bd9Sstevel@tonic-gate 		if ((err = va_makepath(&ap, &symbolic)) < 0)
2477c478bd9Sstevel@tonic-gate 			fail ("\"%s\" is a truncated name!\n", symbolic);
2487c478bd9Sstevel@tonic-gate 		Symlink (path, symbolic);
2497c478bd9Sstevel@tonic-gate 		Free (symbolic);
2507c478bd9Sstevel@tonic-gate 		Free (path);
2517c478bd9Sstevel@tonic-gate 		return;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	case D:
2544bc0a2efScasper 		if (exist && !S_ISDIR(stbuf.st_mode)) {
2557c478bd9Sstevel@tonic-gate 			if (!rm_ok)
2567c478bd9Sstevel@tonic-gate 				fail ("%s is not a directory!\n", path);
2577c478bd9Sstevel@tonic-gate 			else {
2587c478bd9Sstevel@tonic-gate 				Unlink (path);
2597c478bd9Sstevel@tonic-gate 				exist = 0;
2607c478bd9Sstevel@tonic-gate 			}
2614bc0a2efScasper 		}
2627c478bd9Sstevel@tonic-gate 		if (!exist)
2637c478bd9Sstevel@tonic-gate 			Mkdir (path, 0);
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	case F:
2674bc0a2efScasper 		if (exist && !S_ISREG(stbuf.st_mode)) {
2687c478bd9Sstevel@tonic-gate 			if (!rm_ok)
2697c478bd9Sstevel@tonic-gate 				fail ("%s is not a file!\n", path);
2707c478bd9Sstevel@tonic-gate 			else {
2717c478bd9Sstevel@tonic-gate 				Unlink (path);
2727c478bd9Sstevel@tonic-gate 				exist = 0;
2737c478bd9Sstevel@tonic-gate 			}
2744bc0a2efScasper 		}
2757c478bd9Sstevel@tonic-gate 		if (!exist)
2767c478bd9Sstevel@tonic-gate 			Close(Creat(path, 0));
2777c478bd9Sstevel@tonic-gate 		break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	case P:
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * Either a pipe or a file.
2827c478bd9Sstevel@tonic-gate 		 */
2834bc0a2efScasper 		if (exist &&
2844bc0a2efScasper 		    !S_ISREG(stbuf.st_mode) && !S_ISFIFO(stbuf.st_mode)) {
2857c478bd9Sstevel@tonic-gate 			if (!rm_ok)
2867c478bd9Sstevel@tonic-gate 				fail ("%s is not a file or pipe!\n", path);
2877c478bd9Sstevel@tonic-gate 			else {
2887c478bd9Sstevel@tonic-gate 				Unlink (path);
2897c478bd9Sstevel@tonic-gate 				exist = 0;
2907c478bd9Sstevel@tonic-gate 			}
2914bc0a2efScasper 		}
2927c478bd9Sstevel@tonic-gate 		if (!exist)
2937c478bd9Sstevel@tonic-gate 			Close(Creat(path, 0));
2947c478bd9Sstevel@tonic-gate 		break;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	mode = va_arg(ap, mode_t);
2997c478bd9Sstevel@tonic-gate 	uid = va_arg(ap, uid_t);
3007c478bd9Sstevel@tonic-gate 	gid = va_arg(ap, gid_t);
301df1eb1adSjacobs 	(void) chownmod(path, uid, gid, mode);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	Free (path);
3047c478bd9Sstevel@tonic-gate 	return;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * va_makepath()
3097c478bd9Sstevel@tonic-gate  *
3107c478bd9Sstevel@tonic-gate  * Takes a variable length list of path components and attempts to string them
3117c478bd9Sstevel@tonic-gate  * together into a path.  It returns a heap-allocated string via the output
3127c478bd9Sstevel@tonic-gate  * parameter 'ret', and returns an integer success value: < 0 indicates failure,
3137c478bd9Sstevel@tonic-gate  * 0 indicates success.  Note that 'ret' will never be NULL (unless the system
3147c478bd9Sstevel@tonic-gate  * is so overloaded that it can't allocate a single byte), and should always be
3157c478bd9Sstevel@tonic-gate  * free()d.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static int
va_makepath(va_list * pap,char ** ret)3187c478bd9Sstevel@tonic-gate va_makepath (va_list *pap, char **ret)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	char			*component;
3217c478bd9Sstevel@tonic-gate 	char 			buf[MAXPATHLEN];
3227c478bd9Sstevel@tonic-gate 	int			buflen;
3237c478bd9Sstevel@tonic-gate 
324*e4fb8a5fSToomas Soome 	memset(buf, 0, sizeof (buf));
3257c478bd9Sstevel@tonic-gate 	while ((component = va_arg((*pap), char *)) != NULL) {
3267c478bd9Sstevel@tonic-gate 		if (strlcat(buf, component, sizeof (buf)) >= sizeof (buf) ||
3277c478bd9Sstevel@tonic-gate 			strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) {
3287c478bd9Sstevel@tonic-gate 			if ((*ret = strdup(buf)) == NULL)
3297c478bd9Sstevel@tonic-gate 				*ret = strdup("");
3307c478bd9Sstevel@tonic-gate 			return (-1);
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* remove the trailing slash */
3357c478bd9Sstevel@tonic-gate 	buflen = strlen(buf);
3367c478bd9Sstevel@tonic-gate 	if ((buflen > 1) && (buf[buflen - 1] == '/')) {
3377c478bd9Sstevel@tonic-gate 		buf[buflen - 1] = '\0';
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if ((*ret = strdup(buf)) == NULL) {
3417c478bd9Sstevel@tonic-gate 		*ret = strdup("");
3427c478bd9Sstevel@tonic-gate 		return (-1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /**
3487c478bd9Sstevel@tonic-gate  ** _rename()
3497c478bd9Sstevel@tonic-gate  **/
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static void
_rename(char * old_system,char * new_system,...)3527c478bd9Sstevel@tonic-gate _rename(char *old_system, char *new_system, ...)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	va_list			ap;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	char *			prefix;
3577c478bd9Sstevel@tonic-gate 	char *			old;
3587c478bd9Sstevel@tonic-gate 	char *			new;
3597c478bd9Sstevel@tonic-gate 	int			err;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	va_start (ap, new_system);
3637c478bd9Sstevel@tonic-gate 	if ((err = va_makepath(&ap, &prefix)) < 0)
3647c478bd9Sstevel@tonic-gate 		fail (
3657c478bd9Sstevel@tonic-gate 			"Rename failed; prefix \"%s\" is a truncated name.\n",
3667c478bd9Sstevel@tonic-gate 			prefix
3677c478bd9Sstevel@tonic-gate 		);
3687c478bd9Sstevel@tonic-gate 	va_end (ap);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	old = makepath(prefix, old_system, (char *)0);
3717c478bd9Sstevel@tonic-gate 	new = makepath(prefix, new_system, (char *)0);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (Rename(old, new) == 0)
3747c478bd9Sstevel@tonic-gate 		note ("Renamed %s to %s.\n", old, new);
3757c478bd9Sstevel@tonic-gate 	else if (errno == EEXIST)
3767c478bd9Sstevel@tonic-gate 		note (
3777c478bd9Sstevel@tonic-gate 			"Rename of %s to %s failed because %s exists.\n",
3787c478bd9Sstevel@tonic-gate 			old,
3797c478bd9Sstevel@tonic-gate 			new,
3807c478bd9Sstevel@tonic-gate 			new
3817c478bd9Sstevel@tonic-gate 		);
3827c478bd9Sstevel@tonic-gate 	else
3837c478bd9Sstevel@tonic-gate 		fail (
3847c478bd9Sstevel@tonic-gate 			"Rename of %s to %s failed (%s).\n",
3857c478bd9Sstevel@tonic-gate 			old,
3867c478bd9Sstevel@tonic-gate 			new,
3877c478bd9Sstevel@tonic-gate 			PERROR
3887c478bd9Sstevel@tonic-gate 		);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	Free (new);
3917c478bd9Sstevel@tonic-gate 	Free (old);
3927c478bd9Sstevel@tonic-gate 	Free (prefix);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	return;
3957c478bd9Sstevel@tonic-gate }
396