xref: /titanic_41/usr/src/ucbcmd/install.d/install.c (revision cc6c5292fa8a241fe50604cf6a918edfbf7cd7d2)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/param.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <grp.h>
487c478bd9Sstevel@tonic-gate #include <pwd.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	DEF_GROUP	"staff"		/* default group */
557c478bd9Sstevel@tonic-gate #define	DEF_OWNER	"root"		/* default owner */
567c478bd9Sstevel@tonic-gate #define	DEF_MODE	0755		/* default mode */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate char *group = DEF_GROUP;
597c478bd9Sstevel@tonic-gate char *owner = DEF_OWNER;
607c478bd9Sstevel@tonic-gate int mode    = DEF_MODE;
617c478bd9Sstevel@tonic-gate int sflag = 0;
627c478bd9Sstevel@tonic-gate struct passwd *pp;
637c478bd9Sstevel@tonic-gate struct group *gp;
647c478bd9Sstevel@tonic-gate extern int errno;
657c478bd9Sstevel@tonic-gate int copy();
667c478bd9Sstevel@tonic-gate void usage();
677c478bd9Sstevel@tonic-gate 
68*cc6c5292Schin int
main(int argc,char ** argv)69*cc6c5292Schin main(int argc, char **argv)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	extern char	*optarg;
727c478bd9Sstevel@tonic-gate 	extern int	optind;
737c478bd9Sstevel@tonic-gate 	struct stat	stb;
747c478bd9Sstevel@tonic-gate 	char	*dirname;
757c478bd9Sstevel@tonic-gate 	int	ch;
767c478bd9Sstevel@tonic-gate 	int	i;
777c478bd9Sstevel@tonic-gate 	int	rc;
787c478bd9Sstevel@tonic-gate 	int	dflag = 0;
797c478bd9Sstevel@tonic-gate 	int	gflag = 0;
807c478bd9Sstevel@tonic-gate 	int	oflag = 0;
817c478bd9Sstevel@tonic-gate 	int	mflag = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
867c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF)
917c478bd9Sstevel@tonic-gate 		switch((char)ch) {
927c478bd9Sstevel@tonic-gate 		case 'c':
937c478bd9Sstevel@tonic-gate 			break;	/* always do "copy" */
947c478bd9Sstevel@tonic-gate 		case 'd':
957c478bd9Sstevel@tonic-gate 			dflag++;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case 'g':
987c478bd9Sstevel@tonic-gate 			gflag++;
997c478bd9Sstevel@tonic-gate 			group = optarg;
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 		case 'm':
1027c478bd9Sstevel@tonic-gate 			mflag++;
1037c478bd9Sstevel@tonic-gate 			mode = atoo(optarg);
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 'o':
1067c478bd9Sstevel@tonic-gate 			oflag++;
1077c478bd9Sstevel@tonic-gate 			owner = optarg;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		case 's':
1107c478bd9Sstevel@tonic-gate 			sflag++;
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case '?':
1137c478bd9Sstevel@tonic-gate 		default:
1147c478bd9Sstevel@tonic-gate 			usage();
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	argc -= optind;
1177c478bd9Sstevel@tonic-gate 	argv += optind;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* get group and owner id's */
1207c478bd9Sstevel@tonic-gate 	if (!(gp = getgrnam(group))) {
1217c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown group %s.\n"), group);
1227c478bd9Sstevel@tonic-gate 		exit(1);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (!(pp = getpwnam(owner))) {
1257c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown user %s.\n"), owner);
1267c478bd9Sstevel@tonic-gate 		exit(1);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (dflag) {		/* install a directory */
1307c478bd9Sstevel@tonic-gate 		int exists = 0;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (argc != 1)
1337c478bd9Sstevel@tonic-gate 			usage();
1347c478bd9Sstevel@tonic-gate 		dirname = argv[0];
1357c478bd9Sstevel@tonic-gate 		if (mkdirp(dirname, 0777) < 0) {
1367c478bd9Sstevel@tonic-gate 			exists = errno == EEXIST;
1377c478bd9Sstevel@tonic-gate 			if (!exists) {
1387c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno));
1397c478bd9Sstevel@tonic-gate 				exit(1);
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		if (stat(dirname, &stb) < 0) {
1437c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno));
1447c478bd9Sstevel@tonic-gate 			exit(1);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		if ((stb.st_mode&S_IFMT) != S_IFDIR) {
1477c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s is not a directory\n"), dirname);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		/* make sure directory setgid bit is inherited */
1507c478bd9Sstevel@tonic-gate 		mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID);
1517c478bd9Sstevel@tonic-gate 		if (mflag && chmod(dirname, mode)) {
1527c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno));
1537c478bd9Sstevel@tonic-gate 			if (!exists)
1547c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1557c478bd9Sstevel@tonic-gate 			exit(1) ;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 		if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) {
1587c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno));
1597c478bd9Sstevel@tonic-gate 			if (!exists)
1607c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1617c478bd9Sstevel@tonic-gate 			exit(1) ;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) {
1647c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno));
1657c478bd9Sstevel@tonic-gate 			if (!exists)
1667c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1677c478bd9Sstevel@tonic-gate 			exit(1) ;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		exit(0);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (argc < 2)
1737c478bd9Sstevel@tonic-gate 		usage();
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate         if (argc > 2) {		/* last arg must be a directory */
1767c478bd9Sstevel@tonic-gate                 if (stat(argv[argc-1], &stb) < 0)
1777c478bd9Sstevel@tonic-gate                         usage();
1787c478bd9Sstevel@tonic-gate                 if ((stb.st_mode&S_IFMT) != S_IFDIR)
1797c478bd9Sstevel@tonic-gate                         usage();
1807c478bd9Sstevel@tonic-gate         }
1817c478bd9Sstevel@tonic-gate         rc = 0;
1827c478bd9Sstevel@tonic-gate         for (i = 0; i < argc-1; i++)
1837c478bd9Sstevel@tonic-gate                 rc |= install(argv[i], argv[argc-1]);
184*cc6c5292Schin         return (rc);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int
install(from,to)1887c478bd9Sstevel@tonic-gate install(from, to)
1897c478bd9Sstevel@tonic-gate 	char *from, *to;
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int to_fd;
1927c478bd9Sstevel@tonic-gate 	int devnull;
1937c478bd9Sstevel@tonic-gate 	int status = 0;
1947c478bd9Sstevel@tonic-gate 	char *path;
1957c478bd9Sstevel@tonic-gate 	struct stat from_sb, to_sb;
1967c478bd9Sstevel@tonic-gate 	static char pbuf[MAXPATHLEN];
1977c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN + 10];
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* check source */
2007c478bd9Sstevel@tonic-gate 	if (stat(from, &from_sb)) {
2017c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno));
2027c478bd9Sstevel@tonic-gate 		return (1);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	/* special case for removing files */
2057c478bd9Sstevel@tonic-gate 	devnull = !strcmp(from, "/dev/null");
2067c478bd9Sstevel@tonic-gate 	if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) {
2077c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from);
2087c478bd9Sstevel@tonic-gate 		return (1);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* build target path, find out if target is same as source */
2127c478bd9Sstevel@tonic-gate 	if (!stat(path = to, &to_sb)) {
2137c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) == S_IFDIR) {
2147c478bd9Sstevel@tonic-gate 			char *C, *strrchr();
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 			(void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from);
2177c478bd9Sstevel@tonic-gate 			if (stat(path, &to_sb))
2187c478bd9Sstevel@tonic-gate 				goto nocompare;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) != S_IFREG) {
2217c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path);
2227c478bd9Sstevel@tonic-gate 			return (1);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
2257c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path);
2267c478bd9Sstevel@tonic-gate 			return (1);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		/* unlink now... avoid ETXTBSY errors later */
2297c478bd9Sstevel@tonic-gate 		(void) unlink(path);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate nocompare:
2337c478bd9Sstevel@tonic-gate 	/* open target, set mode, owner, group */
2347c478bd9Sstevel@tonic-gate 	if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) {
2357c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno));
2367c478bd9Sstevel@tonic-gate 		return (1);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	if (fchmod(to_fd, mode)) {
2397c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno));
2407c478bd9Sstevel@tonic-gate 		status = 1;
2417c478bd9Sstevel@tonic-gate 		close(to_fd);
2427c478bd9Sstevel@tonic-gate 		goto inst_done;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (!devnull) {
2457c478bd9Sstevel@tonic-gate 		status = copy(from, to_fd, path);  /* copy */
2467c478bd9Sstevel@tonic-gate 		close(to_fd);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	if (sflag) {
2497c478bd9Sstevel@tonic-gate 		sprintf(buf, "strip %s", path);
2507c478bd9Sstevel@tonic-gate 		system(buf);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) {
2537c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno));
2547c478bd9Sstevel@tonic-gate 		status = 1;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate inst_done:
2587c478bd9Sstevel@tonic-gate 	if (status)
2597c478bd9Sstevel@tonic-gate 		(void) unlink(path);
2607c478bd9Sstevel@tonic-gate 	return (status);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * copy --
2657c478bd9Sstevel@tonic-gate  *	copy from one file to another
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate int
copy(from_name,to_fd,to_name)2687c478bd9Sstevel@tonic-gate copy(from_name, to_fd, to_name)
2697c478bd9Sstevel@tonic-gate 	int to_fd;
2707c478bd9Sstevel@tonic-gate 	char *from_name, *to_name;
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	int n, from_fd;
2737c478bd9Sstevel@tonic-gate 	int status = 0;
2747c478bd9Sstevel@tonic-gate 	char buf[MAXBSIZE];
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
2777c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno));
2787c478bd9Sstevel@tonic-gate 		return (1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	while ((n = read(from_fd, buf, sizeof(buf))) > 0)
2817c478bd9Sstevel@tonic-gate 		if (write(to_fd, buf, n) != n) {
2827c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno));
2837c478bd9Sstevel@tonic-gate 		status = 1;
2847c478bd9Sstevel@tonic-gate 		goto copy_done;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	if (n == -1) {
2877c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno));
2887c478bd9Sstevel@tonic-gate 		status = 1;
2897c478bd9Sstevel@tonic-gate 		goto copy_done;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate copy_done:
2937c478bd9Sstevel@tonic-gate 	(void) close(from_fd);
2947c478bd9Sstevel@tonic-gate 	return (status);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * atoo --
2997c478bd9Sstevel@tonic-gate  *      octal string to int
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate int
atoo(str)3027c478bd9Sstevel@tonic-gate atoo(str)
303*cc6c5292Schin         char   *str;
3047c478bd9Sstevel@tonic-gate {
305*cc6c5292Schin         int    val;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate         for (val = 0; isdigit(*str); ++str)
3087c478bd9Sstevel@tonic-gate                 val = val * 8 + *str - '0';
3097c478bd9Sstevel@tonic-gate         return(val);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * usage --
3157c478bd9Sstevel@tonic-gate  *	print a usage message and die
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate void
usage()3187c478bd9Sstevel@tonic-gate usage()
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ...  destination\n"), stderr);
3217c478bd9Sstevel@tonic-gate 	fputs(gettext("       install  -d   [-g group] [-m mode] [-o owner] dir\n"), stderr);
3227c478bd9Sstevel@tonic-gate 	exit(1);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * mkdirp --
3277c478bd9Sstevel@tonic-gate  *	make a directory and parents if needed
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate int
mkdirp(dir,mode)3307c478bd9Sstevel@tonic-gate mkdirp(dir, mode)
3317c478bd9Sstevel@tonic-gate 	char *dir;
3327c478bd9Sstevel@tonic-gate 	int mode;
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	int err;
3357c478bd9Sstevel@tonic-gate 	char *slash;
3367c478bd9Sstevel@tonic-gate 	char *strrchr();
3377c478bd9Sstevel@tonic-gate 	extern int errno;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (mkdir(dir, mode) == 0)
3407c478bd9Sstevel@tonic-gate 		return (0);
3417c478bd9Sstevel@tonic-gate 	if (errno != ENOENT)
3427c478bd9Sstevel@tonic-gate 		return (-1);
3437c478bd9Sstevel@tonic-gate 	slash = strrchr(dir, '/');
3447c478bd9Sstevel@tonic-gate 	if (slash == NULL)
3457c478bd9Sstevel@tonic-gate 		return (-1);
3467c478bd9Sstevel@tonic-gate 	*slash = '\0';
3477c478bd9Sstevel@tonic-gate 	err = mkdirp(dir, 0777);
3487c478bd9Sstevel@tonic-gate 	*slash = '/';
3497c478bd9Sstevel@tonic-gate 	if (err)
3507c478bd9Sstevel@tonic-gate 		return (err);
3517c478bd9Sstevel@tonic-gate 	return mkdir(dir, mode);
3527c478bd9Sstevel@tonic-gate }
353