1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
46*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
47*7c478bd9Sstevel@tonic-gate #include <grp.h>
48*7c478bd9Sstevel@tonic-gate #include <pwd.h>
49*7c478bd9Sstevel@tonic-gate #include <stdio.h>
50*7c478bd9Sstevel@tonic-gate #include <ctype.h>
51*7c478bd9Sstevel@tonic-gate #include <errno.h>
52*7c478bd9Sstevel@tonic-gate #include <locale.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #define	DEF_GROUP	"staff"		/* default group */
55*7c478bd9Sstevel@tonic-gate #define	DEF_OWNER	"root"		/* default owner */
56*7c478bd9Sstevel@tonic-gate #define	DEF_MODE	0755		/* default mode */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate char *group = DEF_GROUP;
59*7c478bd9Sstevel@tonic-gate char *owner = DEF_OWNER;
60*7c478bd9Sstevel@tonic-gate int mode    = DEF_MODE;
61*7c478bd9Sstevel@tonic-gate int sflag = 0;
62*7c478bd9Sstevel@tonic-gate struct passwd *pp;
63*7c478bd9Sstevel@tonic-gate struct group *gp;
64*7c478bd9Sstevel@tonic-gate extern int errno;
65*7c478bd9Sstevel@tonic-gate int copy();
66*7c478bd9Sstevel@tonic-gate void usage();
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate main(argc, argv)
69*7c478bd9Sstevel@tonic-gate 	int	argc;
70*7c478bd9Sstevel@tonic-gate 	char	**argv;
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	extern char	*optarg;
73*7c478bd9Sstevel@tonic-gate 	extern int	optind;
74*7c478bd9Sstevel@tonic-gate 	struct stat	stb;
75*7c478bd9Sstevel@tonic-gate 	char	*dirname;
76*7c478bd9Sstevel@tonic-gate 	int	ch;
77*7c478bd9Sstevel@tonic-gate 	int	i;
78*7c478bd9Sstevel@tonic-gate 	int	rc;
79*7c478bd9Sstevel@tonic-gate 	int	dflag = 0;
80*7c478bd9Sstevel@tonic-gate 	int	gflag = 0;
81*7c478bd9Sstevel@tonic-gate 	int	oflag = 0;
82*7c478bd9Sstevel@tonic-gate 	int	mflag = 0;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
87*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
88*7c478bd9Sstevel@tonic-gate #endif
89*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dcg:o:m:s")) != EOF)
92*7c478bd9Sstevel@tonic-gate 		switch((char)ch) {
93*7c478bd9Sstevel@tonic-gate 		case 'c':
94*7c478bd9Sstevel@tonic-gate 			break;	/* always do "copy" */
95*7c478bd9Sstevel@tonic-gate 		case 'd':
96*7c478bd9Sstevel@tonic-gate 			dflag++;
97*7c478bd9Sstevel@tonic-gate 			break;
98*7c478bd9Sstevel@tonic-gate 		case 'g':
99*7c478bd9Sstevel@tonic-gate 			gflag++;
100*7c478bd9Sstevel@tonic-gate 			group = optarg;
101*7c478bd9Sstevel@tonic-gate 			break;
102*7c478bd9Sstevel@tonic-gate 		case 'm':
103*7c478bd9Sstevel@tonic-gate 			mflag++;
104*7c478bd9Sstevel@tonic-gate 			mode = atoo(optarg);
105*7c478bd9Sstevel@tonic-gate 			break;
106*7c478bd9Sstevel@tonic-gate 		case 'o':
107*7c478bd9Sstevel@tonic-gate 			oflag++;
108*7c478bd9Sstevel@tonic-gate 			owner = optarg;
109*7c478bd9Sstevel@tonic-gate 			break;
110*7c478bd9Sstevel@tonic-gate 		case 's':
111*7c478bd9Sstevel@tonic-gate 			sflag++;
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 		case '?':
114*7c478bd9Sstevel@tonic-gate 		default:
115*7c478bd9Sstevel@tonic-gate 			usage();
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 	argc -= optind;
118*7c478bd9Sstevel@tonic-gate 	argv += optind;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* get group and owner id's */
121*7c478bd9Sstevel@tonic-gate 	if (!(gp = getgrnam(group))) {
122*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown group %s.\n"), group);
123*7c478bd9Sstevel@tonic-gate 		exit(1);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	if (!(pp = getpwnam(owner))) {
126*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: unknown user %s.\n"), owner);
127*7c478bd9Sstevel@tonic-gate 		exit(1);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (dflag) {		/* install a directory */
131*7c478bd9Sstevel@tonic-gate 		int exists = 0;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		if (argc != 1)
134*7c478bd9Sstevel@tonic-gate 			usage();
135*7c478bd9Sstevel@tonic-gate 		dirname = argv[0];
136*7c478bd9Sstevel@tonic-gate 		if (mkdirp(dirname, 0777) < 0) {
137*7c478bd9Sstevel@tonic-gate 			exists = errno == EEXIST;
138*7c478bd9Sstevel@tonic-gate 			if (!exists) {
139*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("install: mkdir: %s: %s\n"), dirname, strerror(errno));
140*7c478bd9Sstevel@tonic-gate 				exit(1);
141*7c478bd9Sstevel@tonic-gate 			}
142*7c478bd9Sstevel@tonic-gate 		}
143*7c478bd9Sstevel@tonic-gate 		if (stat(dirname, &stb) < 0) {
144*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: stat: %s: %s\n"), dirname, strerror(errno));
145*7c478bd9Sstevel@tonic-gate 			exit(1);
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 		if ((stb.st_mode&S_IFMT) != S_IFDIR) {
148*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s is not a directory\n"), dirname);
149*7c478bd9Sstevel@tonic-gate 		}
150*7c478bd9Sstevel@tonic-gate 		/* make sure directory setgid bit is inherited */
151*7c478bd9Sstevel@tonic-gate 		mode = (mode & ~S_ISGID) | (stb.st_mode & S_ISGID);
152*7c478bd9Sstevel@tonic-gate 		if (mflag && chmod(dirname, mode)) {
153*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chmod: %s: %s\n"), dirname, strerror(errno));
154*7c478bd9Sstevel@tonic-gate 			if (!exists)
155*7c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
156*7c478bd9Sstevel@tonic-gate 			exit(1) ;
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 		if (oflag && chown(dirname, pp->pw_uid, -1) && errno != EPERM) {
159*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chown: %s: %s\n"), dirname, strerror(errno));
160*7c478bd9Sstevel@tonic-gate 			if (!exists)
161*7c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
162*7c478bd9Sstevel@tonic-gate 			exit(1) ;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 		if (gflag && chown(dirname, -1, gp->gr_gid) && errno != EPERM) {
165*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: chgrp: %s: %s\n"), dirname, strerror(errno));
166*7c478bd9Sstevel@tonic-gate 			if (!exists)
167*7c478bd9Sstevel@tonic-gate 				(void) unlink(dirname);
168*7c478bd9Sstevel@tonic-gate 			exit(1) ;
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 		exit(0);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (argc < 2)
174*7c478bd9Sstevel@tonic-gate 		usage();
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate         if (argc > 2) {		/* last arg must be a directory */
177*7c478bd9Sstevel@tonic-gate                 if (stat(argv[argc-1], &stb) < 0)
178*7c478bd9Sstevel@tonic-gate                         usage();
179*7c478bd9Sstevel@tonic-gate                 if ((stb.st_mode&S_IFMT) != S_IFDIR)
180*7c478bd9Sstevel@tonic-gate                         usage();
181*7c478bd9Sstevel@tonic-gate         }
182*7c478bd9Sstevel@tonic-gate         rc = 0;
183*7c478bd9Sstevel@tonic-gate         for (i = 0; i < argc-1; i++)
184*7c478bd9Sstevel@tonic-gate                 rc |= install(argv[i], argv[argc-1]);
185*7c478bd9Sstevel@tonic-gate         exit(rc);
186*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate int
190*7c478bd9Sstevel@tonic-gate install(from, to)
191*7c478bd9Sstevel@tonic-gate 	char *from, *to;
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	int to_fd;
194*7c478bd9Sstevel@tonic-gate 	int devnull;
195*7c478bd9Sstevel@tonic-gate 	int status = 0;
196*7c478bd9Sstevel@tonic-gate 	char *path;
197*7c478bd9Sstevel@tonic-gate 	struct stat from_sb, to_sb;
198*7c478bd9Sstevel@tonic-gate 	static char pbuf[MAXPATHLEN];
199*7c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN + 10];
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	/* check source */
202*7c478bd9Sstevel@tonic-gate 	if (stat(from, &from_sb)) {
203*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), from, strerror(errno));
204*7c478bd9Sstevel@tonic-gate 		return (1);
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	/* special case for removing files */
207*7c478bd9Sstevel@tonic-gate 	devnull = !strcmp(from, "/dev/null");
208*7c478bd9Sstevel@tonic-gate 	if (!devnull && !((from_sb.st_mode&S_IFMT) == S_IFREG)) {
209*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s isn't a regular file.\n"), from);
210*7c478bd9Sstevel@tonic-gate 		return (1);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/* build target path, find out if target is same as source */
214*7c478bd9Sstevel@tonic-gate 	if (!stat(path = to, &to_sb)) {
215*7c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) == S_IFDIR) {
216*7c478bd9Sstevel@tonic-gate 			char *C, *strrchr();
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 			(void) sprintf(path = pbuf, "%s/%s", to, (C = strrchr(from, '/')) ? ++C : from);
219*7c478bd9Sstevel@tonic-gate 			if (stat(path, &to_sb))
220*7c478bd9Sstevel@tonic-gate 				goto nocompare;
221*7c478bd9Sstevel@tonic-gate 		}
222*7c478bd9Sstevel@tonic-gate 		if ((to_sb.st_mode&S_IFMT) != S_IFREG) {
223*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s isn't a regular file.\n"), path);
224*7c478bd9Sstevel@tonic-gate 			return (1);
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 		if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
227*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: %s and %s are the same file.\n"), from, path);
228*7c478bd9Sstevel@tonic-gate 			return (1);
229*7c478bd9Sstevel@tonic-gate 		}
230*7c478bd9Sstevel@tonic-gate 		/* unlink now... avoid ETXTBSY errors later */
231*7c478bd9Sstevel@tonic-gate 		(void) unlink(path);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate nocompare:
235*7c478bd9Sstevel@tonic-gate 	/* open target, set mode, owner, group */
236*7c478bd9Sstevel@tonic-gate 	if ((to_fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0)) < 0) {
237*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: %s: %s\n"), path, strerror(errno));
238*7c478bd9Sstevel@tonic-gate 		return (1);
239*7c478bd9Sstevel@tonic-gate 	}
240*7c478bd9Sstevel@tonic-gate 	if (fchmod(to_fd, mode)) {
241*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chmod: %s: %s\n"), path, strerror(errno));
242*7c478bd9Sstevel@tonic-gate 		status = 1;
243*7c478bd9Sstevel@tonic-gate 		close(to_fd);
244*7c478bd9Sstevel@tonic-gate 		goto inst_done;
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	if (!devnull) {
247*7c478bd9Sstevel@tonic-gate 		status = copy(from, to_fd, path);  /* copy */
248*7c478bd9Sstevel@tonic-gate 		close(to_fd);
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	if (sflag) {
251*7c478bd9Sstevel@tonic-gate 		sprintf(buf, "strip %s", path);
252*7c478bd9Sstevel@tonic-gate 		system(buf);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 	if (chown(path, pp->pw_uid, gp->gr_gid) && errno != EPERM) {
255*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: chown: %s: %s\n"), path, strerror(errno));
256*7c478bd9Sstevel@tonic-gate 		status = 1;
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate inst_done:
260*7c478bd9Sstevel@tonic-gate 	if (status)
261*7c478bd9Sstevel@tonic-gate 		(void) unlink(path);
262*7c478bd9Sstevel@tonic-gate 	return (status);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * copy --
267*7c478bd9Sstevel@tonic-gate  *	copy from one file to another
268*7c478bd9Sstevel@tonic-gate  */
269*7c478bd9Sstevel@tonic-gate int
270*7c478bd9Sstevel@tonic-gate copy(from_name, to_fd, to_name)
271*7c478bd9Sstevel@tonic-gate 	int to_fd;
272*7c478bd9Sstevel@tonic-gate 	char *from_name, *to_name;
273*7c478bd9Sstevel@tonic-gate {
274*7c478bd9Sstevel@tonic-gate 	int n, from_fd;
275*7c478bd9Sstevel@tonic-gate 	int status = 0;
276*7c478bd9Sstevel@tonic-gate 	char buf[MAXBSIZE];
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
279*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: open: %s: %s\n"), from_name, strerror(errno));
280*7c478bd9Sstevel@tonic-gate 		return (1);
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate 	while ((n = read(from_fd, buf, sizeof(buf))) > 0)
283*7c478bd9Sstevel@tonic-gate 		if (write(to_fd, buf, n) != n) {
284*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("install: write: %s: %s\n"), to_name, strerror(errno));
285*7c478bd9Sstevel@tonic-gate 		status = 1;
286*7c478bd9Sstevel@tonic-gate 		goto copy_done;
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 	if (n == -1) {
289*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("install: read: %s: %s\n"), from_name, strerror(errno));
290*7c478bd9Sstevel@tonic-gate 		status = 1;
291*7c478bd9Sstevel@tonic-gate 		goto copy_done;
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate copy_done:
295*7c478bd9Sstevel@tonic-gate 	(void) close(from_fd);
296*7c478bd9Sstevel@tonic-gate 	return (status);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate /*
300*7c478bd9Sstevel@tonic-gate  * atoo --
301*7c478bd9Sstevel@tonic-gate  *      octal string to int
302*7c478bd9Sstevel@tonic-gate  */
303*7c478bd9Sstevel@tonic-gate int
304*7c478bd9Sstevel@tonic-gate atoo(str)
305*7c478bd9Sstevel@tonic-gate         register char   *str;
306*7c478bd9Sstevel@tonic-gate {
307*7c478bd9Sstevel@tonic-gate         register int    val;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate         for (val = 0; isdigit(*str); ++str)
310*7c478bd9Sstevel@tonic-gate                 val = val * 8 + *str - '0';
311*7c478bd9Sstevel@tonic-gate         return(val);
312*7c478bd9Sstevel@tonic-gate }
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate /*
316*7c478bd9Sstevel@tonic-gate  * usage --
317*7c478bd9Sstevel@tonic-gate  *	print a usage message and die
318*7c478bd9Sstevel@tonic-gate  */
319*7c478bd9Sstevel@tonic-gate void
320*7c478bd9Sstevel@tonic-gate usage()
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	fputs(gettext("usage: install [-cs] [-g group] [-m mode] [-o owner] file ...  destination\n"), stderr);
323*7c478bd9Sstevel@tonic-gate 	fputs(gettext("       install  -d   [-g group] [-m mode] [-o owner] dir\n"), stderr);
324*7c478bd9Sstevel@tonic-gate 	exit(1);
325*7c478bd9Sstevel@tonic-gate }
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate /*
328*7c478bd9Sstevel@tonic-gate  * mkdirp --
329*7c478bd9Sstevel@tonic-gate  *	make a directory and parents if needed
330*7c478bd9Sstevel@tonic-gate  */
331*7c478bd9Sstevel@tonic-gate int
332*7c478bd9Sstevel@tonic-gate mkdirp(dir, mode)
333*7c478bd9Sstevel@tonic-gate 	char *dir;
334*7c478bd9Sstevel@tonic-gate 	int mode;
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	int err;
337*7c478bd9Sstevel@tonic-gate 	char *slash;
338*7c478bd9Sstevel@tonic-gate 	char *strrchr();
339*7c478bd9Sstevel@tonic-gate 	extern int errno;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if (mkdir(dir, mode) == 0)
342*7c478bd9Sstevel@tonic-gate 		return (0);
343*7c478bd9Sstevel@tonic-gate 	if (errno != ENOENT)
344*7c478bd9Sstevel@tonic-gate 		return (-1);
345*7c478bd9Sstevel@tonic-gate 	slash = strrchr(dir, '/');
346*7c478bd9Sstevel@tonic-gate 	if (slash == NULL)
347*7c478bd9Sstevel@tonic-gate 		return (-1);
348*7c478bd9Sstevel@tonic-gate 	*slash = '\0';
349*7c478bd9Sstevel@tonic-gate 	err = mkdirp(dir, 0777);
350*7c478bd9Sstevel@tonic-gate 	*slash = '/';
351*7c478bd9Sstevel@tonic-gate 	if (err)
352*7c478bd9Sstevel@tonic-gate 		return (err);
353*7c478bd9Sstevel@tonic-gate 	return mkdir(dir, mode);
354*7c478bd9Sstevel@tonic-gate }
355