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 #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <grp.h>
467c478bd9Sstevel@tonic-gate #include <pwd.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	DEF_GROUP	"staff"		/* default group */
537c478bd9Sstevel@tonic-gate #define	DEF_OWNER	"root"		/* default owner */
547c478bd9Sstevel@tonic-gate #define	DEF_MODE	0755		/* default mode */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate char *group = DEF_GROUP;
577c478bd9Sstevel@tonic-gate char *owner = DEF_OWNER;
587c478bd9Sstevel@tonic-gate int mode    = DEF_MODE;
597c478bd9Sstevel@tonic-gate int sflag = 0;
607c478bd9Sstevel@tonic-gate struct passwd *pp;
617c478bd9Sstevel@tonic-gate struct group *gp;
627c478bd9Sstevel@tonic-gate extern int errno;
637c478bd9Sstevel@tonic-gate int copy();
647c478bd9Sstevel@tonic-gate void usage();
657c478bd9Sstevel@tonic-gate 
66cc6c5292Schin int
main(int argc,char ** argv)67cc6c5292Schin main(int argc, char **argv)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	extern char	*optarg;
707c478bd9Sstevel@tonic-gate 	extern int	optind;
717c478bd9Sstevel@tonic-gate 	struct stat	stb;
727c478bd9Sstevel@tonic-gate 	char	*dirname;
737c478bd9Sstevel@tonic-gate 	int	ch;
747c478bd9Sstevel@tonic-gate 	int	i;
757c478bd9Sstevel@tonic-gate 	int	rc;
767c478bd9Sstevel@tonic-gate 	int	dflag = 0;
777c478bd9Sstevel@tonic-gate 	int	gflag = 0;
787c478bd9Sstevel@tonic-gate 	int	oflag = 0;
797c478bd9Sstevel@tonic-gate 	int	mflag = 0;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
847c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF)
897c478bd9Sstevel@tonic-gate 		switch((char)ch) {
907c478bd9Sstevel@tonic-gate 		case 'c':
917c478bd9Sstevel@tonic-gate 			break;	/* always do "copy" */
927c478bd9Sstevel@tonic-gate 		case 'd':
937c478bd9Sstevel@tonic-gate 			dflag++;
947c478bd9Sstevel@tonic-gate 			break;
957c478bd9Sstevel@tonic-gate 		case 'g':
967c478bd9Sstevel@tonic-gate 			gflag++;
977c478bd9Sstevel@tonic-gate 			group = optarg;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case 'm':
1007c478bd9Sstevel@tonic-gate 			mflag++;
1017c478bd9Sstevel@tonic-gate 			mode = atoo(optarg);
1027c478bd9Sstevel@tonic-gate 			break;
1037c478bd9Sstevel@tonic-gate 		case 'o':
1047c478bd9Sstevel@tonic-gate 			oflag++;
1057c478bd9Sstevel@tonic-gate 			owner = optarg;
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 		case 's':
1087c478bd9Sstevel@tonic-gate 			sflag++;
1097c478bd9Sstevel@tonic-gate 			break;
1107c478bd9Sstevel@tonic-gate 		case '?':
1117c478bd9Sstevel@tonic-gate 		default:
1127c478bd9Sstevel@tonic-gate 			usage();
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 	argc -= optind;
1157c478bd9Sstevel@tonic-gate 	argv += optind;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* get group and owner id's */
1187c478bd9Sstevel@tonic-gate 	if (!(gp = getgrnam(group))) {
1197c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown group %s.\n"), group);
1207c478bd9Sstevel@tonic-gate 		exit(1);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (!(pp = getpwnam(owner))) {
1237c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown user %s.\n"), owner);
1247c478bd9Sstevel@tonic-gate 		exit(1);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (dflag) {		/* install a directory */
1287c478bd9Sstevel@tonic-gate 		int exists = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		if (argc != 1)
1317c478bd9Sstevel@tonic-gate 			usage();
1327c478bd9Sstevel@tonic-gate 		dirname = argv[0];
1337c478bd9Sstevel@tonic-gate 		if (mkdirp(dirname, 0777) < 0) {
1347c478bd9Sstevel@tonic-gate 			exists = errno == EEXIST;
1357c478bd9Sstevel@tonic-gate 			if (!exists) {
1367c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno));
1377c478bd9Sstevel@tonic-gate 				exit(1);
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		if (stat(dirname, &stb) < 0) {
1417c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno));
1427c478bd9Sstevel@tonic-gate 			exit(1);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		if ((stb.st_mode&S_IFMT) != S_IFDIR) {
145*8c0b080cSToomas Soome 			fprintf(stderr, gettext("install: %s is not a directory\n"), dirname);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		/* make sure directory setgid bit is inherited */
1487c478bd9Sstevel@tonic-gate 		mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID);
1497c478bd9Sstevel@tonic-gate 		if (mflag && chmod(dirname, mode)) {
1507c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno));
1517c478bd9Sstevel@tonic-gate 			if (!exists)
1527c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1537c478bd9Sstevel@tonic-gate 			exit(1) ;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) {
1567c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno));
1577c478bd9Sstevel@tonic-gate 			if (!exists)
1587c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1597c478bd9Sstevel@tonic-gate 			exit(1) ;
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) {
1627c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno));
1637c478bd9Sstevel@tonic-gate 			if (!exists)
1647c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
1657c478bd9Sstevel@tonic-gate 			exit(1) ;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		exit(0);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (argc < 2)
1717c478bd9Sstevel@tonic-gate 		usage();
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate         if (argc > 2) {		/* last arg must be a directory */
1747c478bd9Sstevel@tonic-gate                 if (stat(argv[argc-1], &stb) < 0)
1757c478bd9Sstevel@tonic-gate                         usage();
176*8c0b080cSToomas Soome                 if ((stb.st_mode&S_IFMT) != S_IFDIR)
1777c478bd9Sstevel@tonic-gate                         usage();
1787c478bd9Sstevel@tonic-gate         }
1797c478bd9Sstevel@tonic-gate         rc = 0;
1807c478bd9Sstevel@tonic-gate         for (i = 0; i < argc-1; i++)
1817c478bd9Sstevel@tonic-gate                 rc |= install(argv[i], argv[argc-1]);
182cc6c5292Schin         return (rc);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate int
install(from,to)1867c478bd9Sstevel@tonic-gate install(from, to)
1877c478bd9Sstevel@tonic-gate 	char *from, *to;
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int to_fd;
1907c478bd9Sstevel@tonic-gate 	int devnull;
1917c478bd9Sstevel@tonic-gate 	int status = 0;
1927c478bd9Sstevel@tonic-gate 	char *path;
1937c478bd9Sstevel@tonic-gate 	struct stat from_sb, to_sb;
1947c478bd9Sstevel@tonic-gate 	static char pbuf[MAXPATHLEN];
1957c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN + 10];
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* check source */
1987c478bd9Sstevel@tonic-gate 	if (stat(from, &from_sb)) {
1997c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno));
2007c478bd9Sstevel@tonic-gate 		return (1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	/* special case for removing files */
2037c478bd9Sstevel@tonic-gate 	devnull = !strcmp(from, "/dev/null");
2047c478bd9Sstevel@tonic-gate 	if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) {
2057c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from);
2067c478bd9Sstevel@tonic-gate 		return (1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* build target path, find out if target is same as source */
2107c478bd9Sstevel@tonic-gate 	if (!stat(path = to, &to_sb)) {
2117c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) == S_IFDIR) {
2127c478bd9Sstevel@tonic-gate 			char *C, *strrchr();
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			(void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from);
2157c478bd9Sstevel@tonic-gate 			if (stat(path, &to_sb))
2167c478bd9Sstevel@tonic-gate 				goto nocompare;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) != S_IFREG) {
2197c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path);
2207c478bd9Sstevel@tonic-gate 			return (1);
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 		if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
2237c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path);
2247c478bd9Sstevel@tonic-gate 			return (1);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		/* unlink now... avoid ETXTBSY errors later */
2277c478bd9Sstevel@tonic-gate 		(void) unlink(path);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate nocompare:
2317c478bd9Sstevel@tonic-gate 	/* open target, set mode, owner, group */
2327c478bd9Sstevel@tonic-gate 	if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) {
2337c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno));
2347c478bd9Sstevel@tonic-gate 		return (1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	if (fchmod(to_fd, mode)) {
2377c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno));
2387c478bd9Sstevel@tonic-gate 		status = 1;
2397c478bd9Sstevel@tonic-gate 		close(to_fd);
2407c478bd9Sstevel@tonic-gate 		goto inst_done;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 	if (!devnull) {
2437c478bd9Sstevel@tonic-gate 		status = copy(from, to_fd, path);  /* copy */
2447c478bd9Sstevel@tonic-gate 		close(to_fd);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if (sflag) {
2477c478bd9Sstevel@tonic-gate 		sprintf(buf, "strip %s", path);
2487c478bd9Sstevel@tonic-gate 		system(buf);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) {
2517c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno));
2527c478bd9Sstevel@tonic-gate 		status = 1;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate inst_done:
2567c478bd9Sstevel@tonic-gate 	if (status)
2577c478bd9Sstevel@tonic-gate 		(void) unlink(path);
2587c478bd9Sstevel@tonic-gate 	return (status);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * copy --
2637c478bd9Sstevel@tonic-gate  *	copy from one file to another
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate int
copy(from_name,to_fd,to_name)2667c478bd9Sstevel@tonic-gate copy(from_name, to_fd, to_name)
2677c478bd9Sstevel@tonic-gate 	int to_fd;
2687c478bd9Sstevel@tonic-gate 	char *from_name, *to_name;
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	int n, from_fd;
2717c478bd9Sstevel@tonic-gate 	int status = 0;
2727c478bd9Sstevel@tonic-gate 	char buf[MAXBSIZE];
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
2757c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno));
2767c478bd9Sstevel@tonic-gate 		return (1);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	while ((n = read(from_fd, buf, sizeof(buf))) > 0)
2797c478bd9Sstevel@tonic-gate 		if (write(to_fd, buf, n) != n) {
2807c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno));
2817c478bd9Sstevel@tonic-gate 		status = 1;
2827c478bd9Sstevel@tonic-gate 		goto copy_done;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	if (n == -1) {
2857c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno));
2867c478bd9Sstevel@tonic-gate 		status = 1;
2877c478bd9Sstevel@tonic-gate 		goto copy_done;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate copy_done:
2917c478bd9Sstevel@tonic-gate 	(void) close(from_fd);
2927c478bd9Sstevel@tonic-gate 	return (status);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * atoo --
2977c478bd9Sstevel@tonic-gate  *      octal string to int
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate int
atoo(str)3007c478bd9Sstevel@tonic-gate atoo(str)
301cc6c5292Schin         char   *str;
302*8c0b080cSToomas Soome {
303cc6c5292Schin         int    val;
304*8c0b080cSToomas Soome 
3057c478bd9Sstevel@tonic-gate         for (val = 0; isdigit(*str); ++str)
3067c478bd9Sstevel@tonic-gate                 val = val * 8 + *str - '0';
3077c478bd9Sstevel@tonic-gate         return(val);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * usage --
3137c478bd9Sstevel@tonic-gate  *	print a usage message and die
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate void
usage()3167c478bd9Sstevel@tonic-gate usage()
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ...  destination\n"), stderr);
3197c478bd9Sstevel@tonic-gate 	fputs(gettext("       install  -d   [-g group] [-m mode] [-o owner] dir\n"), stderr);
3207c478bd9Sstevel@tonic-gate 	exit(1);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * mkdirp --
3257c478bd9Sstevel@tonic-gate  *	make a directory and parents if needed
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate int
mkdirp(dir,mode)3287c478bd9Sstevel@tonic-gate mkdirp(dir, mode)
3297c478bd9Sstevel@tonic-gate 	char *dir;
3307c478bd9Sstevel@tonic-gate 	int mode;
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	int err;
3337c478bd9Sstevel@tonic-gate 	char *slash;
3347c478bd9Sstevel@tonic-gate 	char *strrchr();
3357c478bd9Sstevel@tonic-gate 	extern int errno;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (mkdir(dir, mode) == 0)
3387c478bd9Sstevel@tonic-gate 		return (0);
3397c478bd9Sstevel@tonic-gate 	if (errno != ENOENT)
3407c478bd9Sstevel@tonic-gate 		return (-1);
3417c478bd9Sstevel@tonic-gate 	slash = strrchr(dir, '/');
3427c478bd9Sstevel@tonic-gate 	if (slash == NULL)
3437c478bd9Sstevel@tonic-gate 		return (-1);
3447c478bd9Sstevel@tonic-gate 	*slash = '\0';
3457c478bd9Sstevel@tonic-gate 	err = mkdirp(dir, 0777);
3467c478bd9Sstevel@tonic-gate 	*slash = '/';
3477c478bd9Sstevel@tonic-gate 	if (err)
3487c478bd9Sstevel@tonic-gate 		return (err);
3497c478bd9Sstevel@tonic-gate 	return mkdir(dir, mode);
3507c478bd9Sstevel@tonic-gate }
351