xinstall.c revision 1.1 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.1 cgd #include <paths.h>
52 1.1 cgd #include "pathnames.h"
53 1.1 cgd
54 1.1 cgd static struct passwd *pp;
55 1.1 cgd static struct group *gp;
56 1.1 cgd static int docopy, dostrip, mode = 0755;
57 1.1 cgd static char *group, *owner, pathbuf[MAXPATHLEN];
58 1.1 cgd
59 1.1 cgd main(argc, argv)
60 1.1 cgd int argc;
61 1.1 cgd char **argv;
62 1.1 cgd {
63 1.1 cgd extern char *optarg;
64 1.1 cgd extern int optind;
65 1.1 cgd struct stat from_sb, to_sb;
66 1.1 cgd mode_t *set, *setmode();
67 1.1 cgd int ch, no_target;
68 1.1 cgd char *to_name;
69 1.1 cgd
70 1.1 cgd while ((ch = getopt(argc, argv, "cg:m:o:s")) != EOF)
71 1.1 cgd switch((char)ch) {
72 1.1 cgd case 'c':
73 1.1 cgd docopy = 1;
74 1.1 cgd break;
75 1.1 cgd case 'g':
76 1.1 cgd group = optarg;
77 1.1 cgd break;
78 1.1 cgd case 'm':
79 1.1 cgd if (!(set = setmode(optarg))) {
80 1.1 cgd (void)fprintf(stderr,
81 1.1 cgd "install: invalid file mode.\n");
82 1.1 cgd exit(1);
83 1.1 cgd }
84 1.1 cgd mode = getmode(set, 0);
85 1.1 cgd break;
86 1.1 cgd case 'o':
87 1.1 cgd owner = optarg;
88 1.1 cgd break;
89 1.1 cgd case 's':
90 1.1 cgd dostrip = 1;
91 1.1 cgd break;
92 1.1 cgd case '?':
93 1.1 cgd default:
94 1.1 cgd usage();
95 1.1 cgd }
96 1.1 cgd argc -= optind;
97 1.1 cgd argv += optind;
98 1.1 cgd if (argc < 2)
99 1.1 cgd usage();
100 1.1 cgd
101 1.1 cgd /* get group and owner id's */
102 1.1 cgd if (group && !(gp = getgrnam(group))) {
103 1.1 cgd fprintf(stderr, "install: unknown group %s.\n", group);
104 1.1 cgd exit(1);
105 1.1 cgd }
106 1.1 cgd if (owner && !(pp = getpwnam(owner))) {
107 1.1 cgd fprintf(stderr, "install: unknown user %s.\n", owner);
108 1.1 cgd exit(1);
109 1.1 cgd }
110 1.1 cgd
111 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
112 1.1 cgd if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) {
113 1.1 cgd for (; *argv != to_name; ++argv)
114 1.1 cgd install(*argv, to_name, 1);
115 1.1 cgd exit(0);
116 1.1 cgd }
117 1.1 cgd
118 1.1 cgd /* can't do file1 file2 directory/file */
119 1.1 cgd if (argc != 2)
120 1.1 cgd usage();
121 1.1 cgd
122 1.1 cgd if (!no_target) {
123 1.1 cgd if (stat(*argv, &from_sb)) {
124 1.1 cgd fprintf(stderr, "install: can't find %s.\n", *argv);
125 1.1 cgd exit(1);
126 1.1 cgd }
127 1.1 cgd if ((to_sb.st_mode & S_IFMT) != S_IFREG) {
128 1.1 cgd fprintf(stderr, "install: %s isn't a regular file.\n", to_name);
129 1.1 cgd exit(1);
130 1.1 cgd }
131 1.1 cgd if (to_sb.st_dev == from_sb.st_dev && to_sb.st_ino == from_sb.st_ino) {
132 1.1 cgd fprintf(stderr, "install: %s and %s are the same file.\n", *argv, to_name);
133 1.1 cgd exit(1);
134 1.1 cgd }
135 1.1 cgd /* unlink now... avoid ETXTBSY errors later */
136 1.1 cgd (void)unlink(to_name);
137 1.1 cgd }
138 1.1 cgd install(*argv, to_name, 0);
139 1.1 cgd exit(0);
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * install --
144 1.1 cgd * build a path name and install the file
145 1.1 cgd */
146 1.1 cgd install(from_name, to_name, isdir)
147 1.1 cgd char *from_name, *to_name;
148 1.1 cgd int isdir;
149 1.1 cgd {
150 1.1 cgd struct stat from_sb;
151 1.1 cgd int devnull, from_fd, to_fd;
152 1.1 cgd char *C, *rindex();
153 1.1 cgd
154 1.1 cgd /* if try to install NULL file to a directory, fails */
155 1.1 cgd if (isdir || strcmp(from_name, _PATH_DEVNULL)) {
156 1.1 cgd if (stat(from_name, &from_sb)) {
157 1.1 cgd fprintf(stderr, "install: can't find %s.\n", from_name);
158 1.1 cgd exit(1);
159 1.1 cgd }
160 1.1 cgd if ((from_sb.st_mode & S_IFMT) != S_IFREG) {
161 1.1 cgd fprintf(stderr, "install: %s isn't a regular file.\n", from_name);
162 1.1 cgd exit(1);
163 1.1 cgd }
164 1.1 cgd /* build the target path */
165 1.1 cgd if (isdir) {
166 1.1 cgd (void)sprintf(pathbuf, "%s/%s", to_name, (C = rindex(from_name, '/')) ? ++C : from_name);
167 1.1 cgd to_name = pathbuf;
168 1.1 cgd }
169 1.1 cgd devnull = 0;
170 1.1 cgd } else
171 1.1 cgd devnull = 1;
172 1.1 cgd
173 1.1 cgd /* unlink now... avoid ETXTBSY errors later */
174 1.1 cgd (void)unlink(to_name);
175 1.1 cgd
176 1.1 cgd /* create target */
177 1.1 cgd if ((to_fd = open(to_name, O_CREAT|O_WRONLY|O_TRUNC, 0600)) < 0) {
178 1.1 cgd error(to_name);
179 1.1 cgd exit(1);
180 1.1 cgd }
181 1.1 cgd if (!devnull) {
182 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
183 1.1 cgd (void)unlink(to_name);
184 1.1 cgd error(from_name);
185 1.1 cgd exit(1);
186 1.1 cgd }
187 1.1 cgd copy(from_fd, from_name, to_fd, to_name);
188 1.1 cgd (void)close(from_fd);
189 1.1 cgd }
190 1.1 cgd if (dostrip)
191 1.1 cgd strip(to_name);
192 1.1 cgd /*
193 1.1 cgd * set owner, group, mode for target; do the chown first,
194 1.1 cgd * chown may lose the setuid bits.
195 1.1 cgd */
196 1.1 cgd if ((group || owner) &&
197 1.1 cgd fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
198 1.1 cgd fchmod(to_fd, mode)) {
199 1.1 cgd error(to_name);
200 1.1 cgd bad(to_name);
201 1.1 cgd }
202 1.1 cgd (void)close(to_fd);
203 1.1 cgd if (!docopy && !devnull && unlink(from_name)) {
204 1.1 cgd error(from_name);
205 1.1 cgd exit(1);
206 1.1 cgd }
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * copy --
211 1.1 cgd * copy from one file to another
212 1.1 cgd */
213 1.1 cgd copy(from_fd, from_name, to_fd, to_name)
214 1.1 cgd register int from_fd, to_fd;
215 1.1 cgd char *from_name, *to_name;
216 1.1 cgd {
217 1.1 cgd register int n;
218 1.1 cgd char buf[MAXBSIZE];
219 1.1 cgd
220 1.1 cgd while ((n = read(from_fd, buf, sizeof(buf))) > 0)
221 1.1 cgd if (write(to_fd, buf, n) != n) {
222 1.1 cgd error(to_name);
223 1.1 cgd bad(to_name);
224 1.1 cgd }
225 1.1 cgd if (n == -1) {
226 1.1 cgd error(from_name);
227 1.1 cgd bad(to_name);
228 1.1 cgd }
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd /*
232 1.1 cgd * strip --
233 1.1 cgd * use strip(1) to strip the target file
234 1.1 cgd */
235 1.1 cgd strip(to_name)
236 1.1 cgd char *to_name;
237 1.1 cgd {
238 1.1 cgd int status;
239 1.1 cgd
240 1.1 cgd switch (vfork()) {
241 1.1 cgd case -1:
242 1.1 cgd error("fork");
243 1.1 cgd bad(to_name);
244 1.1 cgd case 0:
245 1.1 cgd execl(_PATH_STRIP, "strip", to_name, (char *)NULL);
246 1.1 cgd error(_PATH_STRIP);
247 1.1 cgd _exit(1);
248 1.1 cgd default:
249 1.1 cgd if (wait(&status) == -1 || status)
250 1.1 cgd bad(to_name);
251 1.1 cgd }
252 1.1 cgd }
253 1.1 cgd
254 1.1 cgd /*
255 1.1 cgd * error --
256 1.1 cgd * print out an error message
257 1.1 cgd */
258 1.1 cgd error(s)
259 1.1 cgd char *s;
260 1.1 cgd {
261 1.1 cgd extern int errno;
262 1.1 cgd char *strerror();
263 1.1 cgd
264 1.1 cgd (void)fprintf(stderr, "install: %s: %s\n", s, strerror(errno));
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * bad --
269 1.1 cgd * remove created target and die
270 1.1 cgd */
271 1.1 cgd bad(fname)
272 1.1 cgd char *fname;
273 1.1 cgd {
274 1.1 cgd (void)unlink(fname);
275 1.1 cgd exit(1);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd /*
279 1.1 cgd * usage --
280 1.1 cgd * print a usage message and die
281 1.1 cgd */
282 1.1 cgd usage()
283 1.1 cgd {
284 1.1 cgd (void)fprintf(stderr,
285 1.1 cgd "usage: install [-cs] [-g group] [-m mode] [-o owner] file1 file2;\n\tor file1 ... fileN directory\n");
286 1.1 cgd exit(1);
287 1.1 cgd }
288