xinstall.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1987 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1987 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)xinstall.c 5.24 (Berkeley) 7/1/90";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/stat.h>
46 1.1 cgd #include <sys/file.h>
47 1.1 cgd #include <grp.h>
48 1.1 cgd #include <pwd.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <ctype.h>
51 1.2 jtc #include <errno.h>
52 1.1 cgd #include <paths.h>
53 1.1 cgd #include "pathnames.h"
54 1.1 cgd
55 1.1 cgd static struct passwd *pp;
56 1.1 cgd static struct group *gp;
57 1.2 jtc static int docopy, dostrip, dodir, mode = 0755;
58 1.1 cgd static char *group, *owner, pathbuf[MAXPATHLEN];
59 1.1 cgd
60 1.1 cgd main(argc, argv)
61 1.1 cgd int argc;
62 1.1 cgd char **argv;
63 1.1 cgd {
64 1.1 cgd extern char *optarg;
65 1.1 cgd extern int optind;
66 1.1 cgd struct stat from_sb, to_sb;
67 1.1 cgd mode_t *set, *setmode();
68 1.1 cgd int ch, no_target;
69 1.1 cgd char *to_name;
70 1.1 cgd
71 1.2 jtc while ((ch = getopt(argc, argv, "cg:m:o:sd")) != EOF)
72 1.1 cgd switch((char)ch) {
73 1.1 cgd case 'c':
74 1.1 cgd docopy = 1;
75 1.1 cgd break;
76 1.1 cgd case 'g':
77 1.1 cgd group = optarg;
78 1.1 cgd break;
79 1.1 cgd case 'm':
80 1.1 cgd if (!(set = setmode(optarg))) {
81 1.1 cgd (void)fprintf(stderr,
82 1.1 cgd "install: invalid file mode.\n");
83 1.1 cgd exit(1);
84 1.1 cgd }
85 1.1 cgd mode = getmode(set, 0);
86 1.1 cgd break;
87 1.1 cgd case 'o':
88 1.1 cgd owner = optarg;
89 1.1 cgd break;
90 1.1 cgd case 's':
91 1.1 cgd dostrip = 1;
92 1.1 cgd break;
93 1.2 jtc case 'd':
94 1.2 jtc dodir = 1;
95 1.2 jtc break;
96 1.1 cgd case '?':
97 1.1 cgd default:
98 1.1 cgd usage();
99 1.1 cgd }
100 1.1 cgd argc -= optind;
101 1.1 cgd argv += optind;
102 1.2 jtc
103 1.2 jtc /* copy and strip options make no sense when creating directories */
104 1.2 jtc if ((docopy || dostrip) && dodir)
105 1.2 jtc usage();
106 1.2 jtc
107 1.2 jtc /* must have at least two arguments, except when creating directories */
108 1.2 jtc if (argc < 2 && !dodir)
109 1.1 cgd usage();
110 1.1 cgd
111 1.1 cgd /* get group and owner id's */
112 1.1 cgd if (group && !(gp = getgrnam(group))) {
113 1.1 cgd fprintf(stderr, "install: unknown group %s.\n", group);
114 1.1 cgd exit(1);
115 1.1 cgd }
116 1.1 cgd if (owner && !(pp = getpwnam(owner))) {
117 1.1 cgd fprintf(stderr, "install: unknown user %s.\n", owner);
118 1.1 cgd exit(1);
119 1.1 cgd }
120 1.1 cgd
121 1.2 jtc if (dodir) {
122 1.2 jtc for (; *argv != NULL; ++argv)
123 1.2 jtc build(*argv);
124 1.2 jtc exit (0);
125 1.2 jtc /* NOTREACHED */
126 1.2 jtc }
127 1.2 jtc
128 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
129 1.2 jtc if (!no_target && S_ISDIR(to_sb.st_mode)) {
130 1.1 cgd for (; *argv != to_name; ++argv)
131 1.1 cgd install(*argv, to_name, 1);
132 1.1 cgd exit(0);
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd /* can't do file1 file2 directory/file */
136 1.1 cgd if (argc != 2)
137 1.1 cgd usage();
138 1.1 cgd
139 1.1 cgd if (!no_target) {
140 1.1 cgd if (stat(*argv, &from_sb)) {
141 1.1 cgd fprintf(stderr, "install: can't find %s.\n", *argv);
142 1.1 cgd exit(1);
143 1.1 cgd }
144 1.2 jtc if (!S_ISREG(to_sb.st_mode)) {
145 1.1 cgd fprintf(stderr, "install: %s isn't a regular file.\n", to_name);
146 1.1 cgd exit(1);
147 1.1 cgd }
148 1.1 cgd if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
149 1.1 cgd fprintf(stderr, "install: %s and %s are the same file.\n", *argv, to_name);
150 1.1 cgd exit(1);
151 1.1 cgd }
152 1.1 cgd /* unlink now... avoid ETXTBSY errors later */
153 1.1 cgd (void)unlink(to_name);
154 1.1 cgd }
155 1.1 cgd install(*argv, to_name, 0);
156 1.1 cgd exit(0);
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd * install --
161 1.1 cgd * build a path name and install the file
162 1.1 cgd */
163 1.1 cgd install(from_name, to_name, isdir)
164 1.1 cgd char *from_name, *to_name;
165 1.1 cgd int isdir;
166 1.1 cgd {
167 1.1 cgd struct stat from_sb;
168 1.1 cgd int devnull, from_fd, to_fd;
169 1.1 cgd char *C, *rindex();
170 1.1 cgd
171 1.1 cgd /* if try to install NULL file to a directory, fails */
172 1.1 cgd if (isdir || strcmp(from_name, _PATH_DEVNULL)) {
173 1.1 cgd if (stat(from_name, &from_sb)) {
174 1.1 cgd fprintf(stderr, "install: can't find %s.\n", from_name);
175 1.1 cgd exit(1);
176 1.1 cgd }
177 1.2 jtc if (!S_ISREG(from_sb.st_mode)) {
178 1.1 cgd fprintf(stderr, "install: %s isn't a regular file.\n", from_name);
179 1.1 cgd exit(1);
180 1.1 cgd }
181 1.1 cgd /* build the target path */
182 1.1 cgd if (isdir) {
183 1.1 cgd (void)sprintf(pathbuf, "%s/%s", to_name, (C = rindex(from_name, '/')) ? ++C : from_name);
184 1.1 cgd to_name = pathbuf;
185 1.1 cgd }
186 1.1 cgd devnull = 0;
187 1.1 cgd } else
188 1.1 cgd devnull = 1;
189 1.1 cgd
190 1.1 cgd /* unlink now... avoid ETXTBSY errors later */
191 1.1 cgd (void)unlink(to_name);
192 1.1 cgd
193 1.1 cgd /* create target */
194 1.1 cgd if ((to_fd = open(to_name, O_CREAT|O_WRONLY|O_TRUNC, 0600)) < 0) {
195 1.1 cgd error(to_name);
196 1.1 cgd exit(1);
197 1.1 cgd }
198 1.1 cgd if (!devnull) {
199 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
200 1.1 cgd (void)unlink(to_name);
201 1.1 cgd error(from_name);
202 1.1 cgd exit(1);
203 1.1 cgd }
204 1.1 cgd copy(from_fd, from_name, to_fd, to_name);
205 1.1 cgd (void)close(from_fd);
206 1.1 cgd }
207 1.1 cgd if (dostrip)
208 1.1 cgd strip(to_name);
209 1.1 cgd /*
210 1.1 cgd * set owner, group, mode for target; do the chown first,
211 1.1 cgd * chown may lose the setuid bits.
212 1.1 cgd */
213 1.1 cgd if ((group || owner) &&
214 1.1 cgd fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
215 1.1 cgd fchmod(to_fd, mode)) {
216 1.1 cgd error(to_name);
217 1.1 cgd bad(to_name);
218 1.1 cgd }
219 1.1 cgd (void)close(to_fd);
220 1.1 cgd if (!docopy && !devnull && unlink(from_name)) {
221 1.1 cgd error(from_name);
222 1.1 cgd exit(1);
223 1.1 cgd }
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd /*
227 1.1 cgd * copy --
228 1.1 cgd * copy from one file to another
229 1.1 cgd */
230 1.1 cgd copy(from_fd, from_name, to_fd, to_name)
231 1.1 cgd register int from_fd, to_fd;
232 1.1 cgd char *from_name, *to_name;
233 1.1 cgd {
234 1.1 cgd register int n;
235 1.1 cgd char buf[MAXBSIZE];
236 1.1 cgd
237 1.1 cgd while ((n = read(from_fd, buf, sizeof(buf))) > 0)
238 1.1 cgd if (write(to_fd, buf, n) != n) {
239 1.1 cgd error(to_name);
240 1.1 cgd bad(to_name);
241 1.1 cgd }
242 1.1 cgd if (n == -1) {
243 1.1 cgd error(from_name);
244 1.1 cgd bad(to_name);
245 1.1 cgd }
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd /*
249 1.1 cgd * strip --
250 1.1 cgd * use strip(1) to strip the target file
251 1.1 cgd */
252 1.1 cgd strip(to_name)
253 1.1 cgd char *to_name;
254 1.1 cgd {
255 1.1 cgd int status;
256 1.1 cgd
257 1.1 cgd switch (vfork()) {
258 1.1 cgd case -1:
259 1.1 cgd error("fork");
260 1.1 cgd bad(to_name);
261 1.1 cgd case 0:
262 1.1 cgd execl(_PATH_STRIP, "strip", to_name, (char *)NULL);
263 1.1 cgd error(_PATH_STRIP);
264 1.1 cgd _exit(1);
265 1.1 cgd default:
266 1.1 cgd if (wait(&status) == -1 || status)
267 1.1 cgd bad(to_name);
268 1.1 cgd }
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd /*
272 1.2 jtc * build --
273 1.2 jtc * build directory heirarchy
274 1.2 jtc */
275 1.2 jtc build(path)
276 1.2 jtc char *path;
277 1.2 jtc {
278 1.2 jtc register char *p;
279 1.2 jtc struct stat sb;
280 1.2 jtc int create, ch;
281 1.2 jtc
282 1.2 jtc for (create = 0, p = path;; ++p)
283 1.2 jtc if (!*p || *p == '/') {
284 1.2 jtc ch = *p;
285 1.2 jtc *p = '\0';
286 1.2 jtc if (stat(path, &sb)) {
287 1.2 jtc if (errno != ENOENT || mkdir(path, 0777) < 0) {
288 1.2 jtc error(path);
289 1.2 jtc return(1);
290 1.2 jtc }
291 1.2 jtc create = 1;
292 1.2 jtc }
293 1.2 jtc if (!(*p = ch))
294 1.2 jtc break;
295 1.2 jtc }
296 1.2 jtc
297 1.2 jtc if ((group || owner) &&
298 1.2 jtc chown(path, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
299 1.2 jtc chmod(path, mode)) {
300 1.2 jtc error(path);
301 1.2 jtc }
302 1.2 jtc
303 1.2 jtc return(0);
304 1.2 jtc }
305 1.2 jtc
306 1.2 jtc /*
307 1.1 cgd * error --
308 1.1 cgd * print out an error message
309 1.1 cgd */
310 1.1 cgd error(s)
311 1.1 cgd char *s;
312 1.1 cgd {
313 1.1 cgd extern int errno;
314 1.1 cgd char *strerror();
315 1.1 cgd
316 1.1 cgd (void)fprintf(stderr, "install: %s: %s\n", s, strerror(errno));
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * bad --
321 1.1 cgd * remove created target and die
322 1.1 cgd */
323 1.1 cgd bad(fname)
324 1.1 cgd char *fname;
325 1.1 cgd {
326 1.1 cgd (void)unlink(fname);
327 1.1 cgd exit(1);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * usage --
332 1.1 cgd * print a usage message and die
333 1.1 cgd */
334 1.1 cgd usage()
335 1.1 cgd {
336 1.2 jtc (void)fprintf(stderr, "\
337 1.2 jtc usage: install [-cs] [-g group] [-m mode] [-o owner] file1 file2\n\
338 1.2 jtc install [-cs] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\
339 1.2 jtc install -d [-g group] [-m mode] [-o owner] directory ...\n");
340 1.1 cgd exit(1);
341 1.1 cgd }
342