xinstall.c revision 1.74 1 1.74 lukem /* $NetBSD: xinstall.c,v 1.74 2002/12/19 08:30:39 lukem Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1987, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.65 tv #if HAVE_CONFIG_H
37 1.65 tv #include "config.h"
38 1.65 tv #else
39 1.65 tv #define HAVE_FUTIMES 1
40 1.65 tv #define HAVE_STRUCT_STAT_ST_FLAGS 1
41 1.65 tv #endif
42 1.65 tv
43 1.20 mrg #include <sys/cdefs.h>
44 1.67 tv #if defined(__COPYRIGHT) && !defined(lint)
45 1.20 mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
46 1.20 mrg The Regents of the University of California. All rights reserved.\n");
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.67 tv #if defined(__RCSID) && !defined(lint)
50 1.5 jtc #if 0
51 1.5 jtc static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
52 1.20 mrg #else
53 1.74 lukem __RCSID("$NetBSD: xinstall.c,v 1.74 2002/12/19 08:30:39 lukem Exp $");
54 1.5 jtc #endif
55 1.1 cgd #endif /* not lint */
56 1.1 cgd
57 1.1 cgd #include <sys/param.h>
58 1.5 jtc #include <sys/mman.h>
59 1.1 cgd #include <sys/stat.h>
60 1.61 simonb #include <sys/wait.h>
61 1.5 jtc
62 1.5 jtc #include <ctype.h>
63 1.67 tv #include <err.h>
64 1.5 jtc #include <errno.h>
65 1.5 jtc #include <fcntl.h>
66 1.1 cgd #include <grp.h>
67 1.67 tv #include <libgen.h>
68 1.5 jtc #include <paths.h>
69 1.1 cgd #include <pwd.h>
70 1.1 cgd #include <stdio.h>
71 1.5 jtc #include <stdlib.h>
72 1.5 jtc #include <string.h>
73 1.5 jtc #include <unistd.h>
74 1.50 lukem #include <vis.h>
75 1.5 jtc
76 1.1 cgd #include "pathnames.h"
77 1.29 mrg #include "stat_flags.h"
78 1.74 lukem #include "mtree.h"
79 1.1 cgd
80 1.28 wsanchez #define STRIP_ARGS_MAX 32
81 1.31 hubertf #define BACKUP_SUFFIX ".old"
82 1.28 wsanchez
83 1.50 lukem int dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
84 1.50 lukem dounpriv;
85 1.50 lukem int numberedbackup;
86 1.50 lukem int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
87 1.50 lukem char pathbuf[MAXPATHLEN];
88 1.65 tv id_t uid, gid;
89 1.54 lukem char *group, *owner, *fflags, *tags;
90 1.50 lukem FILE *metafp;
91 1.50 lukem char *metafile;
92 1.50 lukem u_long fileflags;
93 1.50 lukem char *stripArgs;
94 1.69 lukem char *afterinstallcmd;
95 1.50 lukem char *suffix = BACKUP_SUFFIX;
96 1.5 jtc
97 1.16 christos #define LN_ABSOLUTE 0x01
98 1.16 christos #define LN_RELATIVE 0x02
99 1.16 christos #define LN_HARD 0x04
100 1.16 christos #define LN_SYMBOLIC 0x08
101 1.16 christos #define LN_MIXED 0x10
102 1.16 christos
103 1.5 jtc #define DIRECTORY 0x01 /* Tell install it's a directory. */
104 1.5 jtc #define SETFLAGS 0x02 /* Tell install to set flags. */
105 1.50 lukem #define HASUID 0x04 /* Tell install the uid was given */
106 1.50 lukem #define HASGID 0x08 /* Tell install the gid was given */
107 1.5 jtc
108 1.69 lukem void afterinstall(const char *, const char *, int);
109 1.50 lukem void backup(const char *);
110 1.48 simonb void copy(int, char *, int, char *, off_t);
111 1.61 simonb int do_link(char *, char *);
112 1.61 simonb void do_symlink(char *, char *);
113 1.50 lukem void install(char *, char *, u_int);
114 1.50 lukem void install_dir(char *, u_int);
115 1.50 lukem int main(int, char *[]);
116 1.48 simonb void makelink(char *, char *);
117 1.61 simonb void metadata_log(const char *, const char *, struct timeval *, const char *);
118 1.50 lukem int parseid(char *, id_t *);
119 1.48 simonb void strip(char *);
120 1.48 simonb void usage(void);
121 1.61 simonb char *xbasename(char *);
122 1.61 simonb char *xdirname(char *);
123 1.1 cgd
124 1.5 jtc int
125 1.48 simonb main(int argc, char *argv[])
126 1.1 cgd {
127 1.53 lukem struct stat from_sb, to_sb;
128 1.53 lukem void *set;
129 1.53 lukem u_int iflags;
130 1.53 lukem int ch, no_target;
131 1.53 lukem char *p, *to_name;
132 1.43 cgd
133 1.43 cgd setprogname(argv[0]);
134 1.1 cgd
135 1.5 jtc iflags = 0;
136 1.74 lukem while ((ch = getopt(argc, argv, "a:cbB:df:g:l:m:M:N:o:prsS:T:U")) != -1)
137 1.1 cgd switch((char)ch) {
138 1.69 lukem case 'a':
139 1.69 lukem afterinstallcmd = strdup(optarg);
140 1.69 lukem if (afterinstallcmd == NULL)
141 1.69 lukem errx(1, "%s", strerror(ENOMEM));
142 1.69 lukem break;
143 1.31 hubertf case 'B':
144 1.31 hubertf suffix = optarg;
145 1.35 hubertf numberedbackup = 0;
146 1.35 hubertf {
147 1.35 hubertf /* Check if given suffix really generates
148 1.35 hubertf different suffixes - catch e.g. ".%" */
149 1.35 hubertf char suffix_expanded0[FILENAME_MAX],
150 1.35 hubertf suffix_expanded1[FILENAME_MAX];
151 1.35 hubertf (void)snprintf(suffix_expanded0, FILENAME_MAX,
152 1.35 hubertf suffix, 0);
153 1.35 hubertf (void)snprintf(suffix_expanded1, FILENAME_MAX,
154 1.35 hubertf suffix, 1);
155 1.35 hubertf if (strcmp(suffix_expanded0, suffix_expanded1)
156 1.35 hubertf != 0)
157 1.35 hubertf numberedbackup = 1;
158 1.35 hubertf }
159 1.31 hubertf /* fall through; -B implies -b */
160 1.61 simonb /*FALLTHROUGH*/
161 1.31 hubertf case 'b':
162 1.31 hubertf dobackup = 1;
163 1.31 hubertf break;
164 1.1 cgd case 'c':
165 1.1 cgd docopy = 1;
166 1.1 cgd break;
167 1.26 christos case 'd':
168 1.26 christos dodir = 1;
169 1.26 christos break;
170 1.65 tv #if !HAVE_CONFIG_H
171 1.5 jtc case 'f':
172 1.52 tv fflags = optarg;
173 1.5 jtc break;
174 1.65 tv #endif
175 1.1 cgd case 'g':
176 1.1 cgd group = optarg;
177 1.1 cgd break;
178 1.16 christos case 'l':
179 1.16 christos for (p = optarg; *p; p++)
180 1.16 christos switch (*p) {
181 1.16 christos case 's':
182 1.16 christos dolink &= ~(LN_HARD|LN_MIXED);
183 1.16 christos dolink |= LN_SYMBOLIC;
184 1.16 christos break;
185 1.16 christos case 'h':
186 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_MIXED);
187 1.16 christos dolink |= LN_HARD;
188 1.16 christos break;
189 1.16 christos case 'm':
190 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_HARD);
191 1.16 christos dolink |= LN_MIXED;
192 1.16 christos break;
193 1.16 christos case 'a':
194 1.16 christos dolink &= ~LN_RELATIVE;
195 1.16 christos dolink |= LN_ABSOLUTE;
196 1.16 christos break;
197 1.16 christos case 'r':
198 1.16 christos dolink &= ~LN_ABSOLUTE;
199 1.16 christos dolink |= LN_RELATIVE;
200 1.16 christos break;
201 1.16 christos default:
202 1.16 christos errx(1, "%c: invalid link type", *p);
203 1.61 simonb /* NOTREACHED */
204 1.16 christos }
205 1.16 christos break;
206 1.26 christos case 'm':
207 1.26 christos if (!(set = setmode(optarg)))
208 1.26 christos errx(1, "%s: invalid file mode", optarg);
209 1.26 christos mode = getmode(set, 0);
210 1.42 enami free(set);
211 1.26 christos break;
212 1.50 lukem case 'M':
213 1.50 lukem metafile = optarg;
214 1.50 lukem break;
215 1.74 lukem case 'N':
216 1.74 lukem if (! setup_getid(optarg))
217 1.74 lukem errx(1,
218 1.74 lukem "Unable to use user and group databases in `%s'",
219 1.74 lukem optarg);
220 1.74 lukem break;
221 1.26 christos case 'o':
222 1.26 christos owner = optarg;
223 1.26 christos break;
224 1.26 christos case 'p':
225 1.26 christos dopreserve = 1;
226 1.26 christos break;
227 1.33 christos case 'r':
228 1.33 christos dorename = 1;
229 1.33 christos break;
230 1.28 wsanchez case 'S':
231 1.49 simonb stripArgs = strdup(optarg);
232 1.49 simonb if (stripArgs == NULL)
233 1.49 simonb errx(1, "%s", strerror(ENOMEM));
234 1.28 wsanchez /* fall through; -S implies -s */
235 1.61 simonb /*FALLTHROUGH*/
236 1.26 christos case 's':
237 1.26 christos dostrip = 1;
238 1.26 christos break;
239 1.54 lukem case 'T':
240 1.54 lukem tags = optarg;
241 1.54 lukem break;
242 1.38 sommerfe case 'U':
243 1.50 lukem dounpriv = 1;
244 1.38 sommerfe break;
245 1.1 cgd case '?':
246 1.1 cgd default:
247 1.1 cgd usage();
248 1.1 cgd }
249 1.1 cgd argc -= optind;
250 1.1 cgd argv += optind;
251 1.2 jtc
252 1.23 tv /* strip and link options make no sense when creating directories */
253 1.23 tv if ((dostrip || dolink) && dodir)
254 1.16 christos usage();
255 1.16 christos
256 1.16 christos /* strip and flags make no sense with links */
257 1.52 tv if ((dostrip || fflags) && dolink)
258 1.2 jtc usage();
259 1.2 jtc
260 1.2 jtc /* must have at least two arguments, except when creating directories */
261 1.2 jtc if (argc < 2 && !dodir)
262 1.1 cgd usage();
263 1.1 cgd
264 1.1 cgd /* get group and owner id's */
265 1.52 tv if (group && !dounpriv) {
266 1.74 lukem if (gid_from_group(group, &gid) == -1 && ! parseid(group, &gid))
267 1.41 cgd errx(1, "unknown group %s", group);
268 1.50 lukem iflags |= HASGID;
269 1.50 lukem }
270 1.52 tv if (owner && !dounpriv) {
271 1.74 lukem if (uid_from_user(owner, &uid) == -1 && ! parseid(owner, &uid))
272 1.41 cgd errx(1, "unknown user %s", owner);
273 1.50 lukem iflags |= HASUID;
274 1.50 lukem }
275 1.50 lukem
276 1.65 tv #if !HAVE_CONFIG_H
277 1.52 tv if (fflags && !dounpriv) {
278 1.52 tv if (string_to_flags(&fflags, &fileflags, NULL))
279 1.52 tv errx(1, "%s: invalid flag", fflags);
280 1.52 tv iflags |= SETFLAGS;
281 1.52 tv }
282 1.65 tv #endif
283 1.52 tv
284 1.50 lukem if (metafile) {
285 1.50 lukem if ((metafp = fopen(metafile, "a")) == NULL)
286 1.50 lukem warn("open %s", metafile);
287 1.41 cgd }
288 1.1 cgd
289 1.2 jtc if (dodir) {
290 1.2 jtc for (; *argv != NULL; ++argv)
291 1.50 lukem install_dir(*argv, iflags);
292 1.2 jtc exit (0);
293 1.5 jtc }
294 1.2 jtc
295 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
296 1.2 jtc if (!no_target && S_ISDIR(to_sb.st_mode)) {
297 1.1 cgd for (; *argv != to_name; ++argv)
298 1.50 lukem install(*argv, to_name, iflags | DIRECTORY);
299 1.1 cgd exit(0);
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd /* can't do file1 file2 directory/file */
303 1.1 cgd if (argc != 2)
304 1.1 cgd usage();
305 1.1 cgd
306 1.1 cgd if (!no_target) {
307 1.61 simonb /* makelink() handles checks for links */
308 1.61 simonb if (!dolink) {
309 1.61 simonb if (stat(*argv, &from_sb))
310 1.66 tv err(1, "%s: stat", *argv);
311 1.61 simonb if (!S_ISREG(to_sb.st_mode))
312 1.61 simonb errx(1, "%s: not a regular file", to_name);
313 1.61 simonb if (to_sb.st_dev == from_sb.st_dev &&
314 1.61 simonb to_sb.st_ino == from_sb.st_ino)
315 1.61 simonb errx(1, "%s and %s are the same file", *argv,
316 1.61 simonb to_name);
317 1.61 simonb }
318 1.5 jtc /*
319 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
320 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
321 1.5 jtc * it might work.
322 1.5 jtc */
323 1.65 tv #if !HAVE_CONFIG_H
324 1.5 jtc #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
325 1.5 jtc if (to_sb.st_flags & NOCHANGEBITS)
326 1.5 jtc (void)chflags(to_name,
327 1.5 jtc to_sb.st_flags & ~(NOCHANGEBITS));
328 1.65 tv #endif
329 1.32 hubertf if (dobackup)
330 1.32 hubertf backup(to_name);
331 1.34 christos else if (!dorename)
332 1.31 hubertf (void)unlink(to_name);
333 1.1 cgd }
334 1.50 lukem install(*argv, to_name, iflags);
335 1.1 cgd exit(0);
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd /*
339 1.50 lukem * parseid --
340 1.50 lukem * parse uid or gid from arg into id, returning non-zero if successful
341 1.50 lukem */
342 1.50 lukem int
343 1.50 lukem parseid(char *name, id_t *id)
344 1.50 lukem {
345 1.50 lukem char *ep;
346 1.50 lukem
347 1.50 lukem errno = 0;
348 1.50 lukem *id = (id_t)strtoul(name, &ep, 10);
349 1.50 lukem if (errno || *ep != '\0')
350 1.50 lukem return (0);
351 1.50 lukem return (1);
352 1.50 lukem }
353 1.50 lukem
354 1.50 lukem /*
355 1.61 simonb * do_link --
356 1.61 simonb * make a hard link, obeying dorename if set
357 1.61 simonb * return -1 on failure
358 1.61 simonb */
359 1.61 simonb int
360 1.61 simonb do_link(char *from_name, char *to_name)
361 1.61 simonb {
362 1.61 simonb char tmpl[MAXPATHLEN];
363 1.61 simonb int ret;
364 1.61 simonb
365 1.61 simonb if (dorename) {
366 1.61 simonb (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
367 1.61 simonb xdirname(to_name));
368 1.62 perry /* This usage is safe. The linker will bitch anyway. */
369 1.61 simonb if (mktemp(tmpl) == NULL)
370 1.66 tv err(1, "%s: mktemp", tmpl);
371 1.61 simonb ret = link(from_name, tmpl);
372 1.61 simonb if (ret == 0) {
373 1.61 simonb ret = rename(tmpl, to_name);
374 1.61 simonb if (ret < 0)
375 1.61 simonb /* remove temporary link before exiting */
376 1.61 simonb (void)unlink(tmpl);
377 1.61 simonb }
378 1.61 simonb return (ret);
379 1.61 simonb } else
380 1.61 simonb return (link(from_name, to_name));
381 1.61 simonb }
382 1.61 simonb
383 1.61 simonb /*
384 1.61 simonb * do_symlink --
385 1.61 simonb * make a symbolic link, obeying dorename if set
386 1.61 simonb * exit on failure
387 1.61 simonb */
388 1.61 simonb void
389 1.61 simonb do_symlink(char *from_name, char *to_name)
390 1.61 simonb {
391 1.61 simonb char tmpl[MAXPATHLEN];
392 1.61 simonb
393 1.61 simonb if (dorename) {
394 1.61 simonb (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
395 1.61 simonb xdirname(to_name));
396 1.62 perry /* This usage is safe. The linker will bitch anyway. */
397 1.61 simonb if (mktemp(tmpl) == NULL)
398 1.66 tv err(1, "%s: mktemp", tmpl);
399 1.61 simonb
400 1.61 simonb if (symlink(from_name, tmpl) == -1)
401 1.61 simonb err(1, "symlink %s -> %s", from_name, tmpl);
402 1.61 simonb if (rename(tmpl, to_name) == -1) {
403 1.61 simonb /* remove temporary link before exiting */
404 1.61 simonb (void)unlink(tmpl);
405 1.61 simonb err(1, "%s: rename", to_name);
406 1.61 simonb }
407 1.61 simonb } else {
408 1.61 simonb if (symlink(from_name, to_name) == -1)
409 1.61 simonb err(1, "symlink %s -> %s", from_name, to_name);
410 1.61 simonb }
411 1.61 simonb }
412 1.61 simonb
413 1.61 simonb /*
414 1.16 christos * makelink --
415 1.16 christos * make a link from source to destination
416 1.16 christos */
417 1.16 christos void
418 1.48 simonb makelink(char *from_name, char *to_name)
419 1.16 christos {
420 1.53 lukem char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
421 1.68 lukem struct stat to_sb;
422 1.16 christos
423 1.16 christos /* Try hard links first */
424 1.16 christos if (dolink & (LN_HARD|LN_MIXED)) {
425 1.61 simonb if (do_link(from_name, to_name) == -1) {
426 1.16 christos if ((dolink & LN_HARD) || errno != EXDEV)
427 1.16 christos err(1, "link %s -> %s", from_name, to_name);
428 1.68 lukem } else {
429 1.68 lukem if (stat(to_name, &to_sb))
430 1.68 lukem err(1, "%s: stat", to_name);
431 1.69 lukem if (S_ISREG(to_sb.st_mode)) {
432 1.69 lukem /* XXX: only metalog hardlinked files */
433 1.69 lukem int omode;
434 1.69 lukem char *oowner, *ogroup, *offlags;
435 1.69 lukem
436 1.69 lukem /* XXX: use underlying perms */
437 1.69 lukem omode = mode;
438 1.69 lukem mode = (to_sb.st_mode & 0777);
439 1.69 lukem oowner = owner;
440 1.69 lukem owner = NULL;
441 1.69 lukem ogroup = group;
442 1.69 lukem group = NULL;
443 1.69 lukem offlags = fflags;
444 1.69 lukem fflags = NULL;
445 1.68 lukem metadata_log(to_name, "file", NULL, NULL);
446 1.69 lukem mode = omode;
447 1.69 lukem owner = oowner;
448 1.69 lukem group = ogroup;
449 1.69 lukem fflags = offlags;
450 1.69 lukem }
451 1.16 christos return;
452 1.57 lukem }
453 1.16 christos }
454 1.16 christos
455 1.16 christos /* Symbolic links */
456 1.16 christos if (dolink & LN_ABSOLUTE) {
457 1.16 christos /* Convert source path to absolute */
458 1.16 christos if (realpath(from_name, src) == NULL)
459 1.66 tv err(1, "%s: realpath", from_name);
460 1.61 simonb do_symlink(src, to_name);
461 1.59 perry metadata_log(to_name, "link", NULL, src);
462 1.16 christos return;
463 1.16 christos }
464 1.16 christos
465 1.16 christos if (dolink & LN_RELATIVE) {
466 1.61 simonb char *cp, *d, *s;
467 1.16 christos
468 1.16 christos /* Resolve pathnames */
469 1.16 christos if (realpath(from_name, src) == NULL)
470 1.66 tv err(1, "%s: realpath", from_name);
471 1.61 simonb
472 1.61 simonb /*
473 1.61 simonb * The last component of to_name may be a symlink,
474 1.61 simonb * so use realpath to resolve only the directory.
475 1.61 simonb */
476 1.63 perry cp = xdirname(to_name);
477 1.61 simonb if (realpath(cp, dst) == NULL)
478 1.66 tv err(1, "%s: realpath", cp);
479 1.61 simonb /* .. and add the last component */
480 1.61 simonb if (strcmp(dst, "/") != 0) {
481 1.61 simonb if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
482 1.61 simonb errx(1, "resolved pathname too long");
483 1.61 simonb }
484 1.61 simonb cp = xbasename(to_name);
485 1.61 simonb if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
486 1.61 simonb errx(1, "resolved pathname too long");
487 1.16 christos
488 1.16 christos /* trim common path components */
489 1.16 christos for (s = src, d = dst; *s == *d; s++, d++)
490 1.16 christos continue;
491 1.16 christos while (*s != '/')
492 1.16 christos s--, d--;
493 1.16 christos
494 1.16 christos /* count the number of directories we need to backtrack */
495 1.16 christos for (++d, lnk[0] = '\0'; *d; d++)
496 1.16 christos if (*d == '/')
497 1.61 simonb (void)strcat(lnk, "../");
498 1.16 christos
499 1.61 simonb (void)strcat(lnk, ++s);
500 1.16 christos
501 1.61 simonb do_symlink(lnk, dst);
502 1.59 perry metadata_log(dst, "link", NULL, lnk);
503 1.16 christos return;
504 1.16 christos }
505 1.16 christos
506 1.16 christos /*
507 1.16 christos * If absolute or relative was not specified,
508 1.16 christos * try the names the user provided
509 1.16 christos */
510 1.61 simonb do_symlink(from_name, to_name);
511 1.59 perry metadata_log(to_name, "link", NULL, from_name);
512 1.16 christos }
513 1.16 christos
514 1.16 christos /*
515 1.1 cgd * install --
516 1.1 cgd * build a path name and install the file
517 1.1 cgd */
518 1.5 jtc void
519 1.50 lukem install(char *from_name, char *to_name, u_int flags)
520 1.1 cgd {
521 1.65 tv struct stat from_sb;
522 1.65 tv #if !HAVE_CONFIG_H
523 1.65 tv struct stat to_sb;
524 1.65 tv #endif
525 1.53 lukem struct timeval tv[2];
526 1.60 dillo int devnull, from_fd, to_fd, serrno, tmpmode;
527 1.53 lukem char *p, tmpl[MAXPATHLEN], *oto_name;
528 1.5 jtc
529 1.71 lukem if (!dolink) {
530 1.71 lukem /* ensure that from_sb & tv are sane if !dolink */
531 1.71 lukem if (stat(from_name, &from_sb))
532 1.71 lukem err(1, "%s: stat", from_name);
533 1.70 lukem #ifdef BSD4_4
534 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
535 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
536 1.70 lukem #else
537 1.71 lukem tv[0].tv_sec = from_sb.st_atime;
538 1.71 lukem tv[0].tv_usec = 0;
539 1.71 lukem tv[1].tv_sec = from_sb.st_mtime;
540 1.71 lukem tv[1].tv_usec = 0;
541 1.70 lukem #endif
542 1.71 lukem }
543 1.70 lukem
544 1.5 jtc if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
545 1.56 perry if (!dolink) {
546 1.56 perry if (!S_ISREG(from_sb.st_mode))
547 1.56 perry errx(1, "%s: not a regular file", from_name);
548 1.56 perry }
549 1.5 jtc /* Build the target path. */
550 1.5 jtc if (flags & DIRECTORY) {
551 1.5 jtc (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
552 1.5 jtc to_name,
553 1.21 lukem (p = strrchr(from_name, '/')) ? ++p : from_name);
554 1.1 cgd to_name = pathbuf;
555 1.1 cgd }
556 1.1 cgd devnull = 0;
557 1.5 jtc } else {
558 1.65 tv #if HAVE_STRUCT_STAT_ST_FLAGS
559 1.5 jtc from_sb.st_flags = 0; /* XXX */
560 1.65 tv #endif
561 1.1 cgd devnull = 1;
562 1.5 jtc }
563 1.1 cgd
564 1.5 jtc /*
565 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
566 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
567 1.5 jtc * it might work.
568 1.5 jtc */
569 1.65 tv #if !HAVE_CONFIG_H
570 1.5 jtc if (stat(to_name, &to_sb) == 0 &&
571 1.5 jtc to_sb.st_flags & (NOCHANGEBITS))
572 1.5 jtc (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
573 1.65 tv #endif
574 1.33 christos if (dorename) {
575 1.64 msaitoh (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
576 1.61 simonb xdirname(to_name));
577 1.33 christos oto_name = to_name;
578 1.33 christos to_name = tmpl;
579 1.33 christos } else {
580 1.37 christos oto_name = NULL; /* pacify gcc */
581 1.33 christos if (dobackup)
582 1.33 christos backup(to_name);
583 1.33 christos else
584 1.33 christos (void)unlink(to_name);
585 1.33 christos }
586 1.16 christos
587 1.16 christos if (dolink) {
588 1.61 simonb makelink(from_name, dorename ? oto_name : to_name);
589 1.16 christos return;
590 1.16 christos }
591 1.1 cgd
592 1.5 jtc /* Create target. */
593 1.33 christos if (dorename) {
594 1.33 christos if ((to_fd = mkstemp(to_name)) == -1)
595 1.66 tv err(1, "%s: mkstemp", to_name);
596 1.33 christos } else {
597 1.33 christos if ((to_fd = open(to_name,
598 1.33 christos O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
599 1.66 tv err(1, "%s: open", to_name);
600 1.33 christos }
601 1.1 cgd if (!devnull) {
602 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
603 1.1 cgd (void)unlink(to_name);
604 1.66 tv err(1, "%s: open", from_name);
605 1.1 cgd }
606 1.5 jtc copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
607 1.1 cgd (void)close(from_fd);
608 1.1 cgd }
609 1.9 jonathan
610 1.9 jonathan if (dostrip) {
611 1.1 cgd strip(to_name);
612 1.9 jonathan
613 1.9 jonathan /*
614 1.9 jonathan * Re-open our fd on the target, in case we used a strip
615 1.9 jonathan * that does not work in-place -- like gnu binutils strip.
616 1.9 jonathan */
617 1.9 jonathan close(to_fd);
618 1.9 jonathan if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
619 1.69 lukem err(1, "stripping %s", to_name);
620 1.69 lukem }
621 1.69 lukem
622 1.69 lukem if (afterinstallcmd != NULL) {
623 1.69 lukem afterinstall(afterinstallcmd, to_name, 1);
624 1.69 lukem
625 1.69 lukem /*
626 1.69 lukem * Re-open our fd on the target, in case we used an
627 1.69 lukem * after-install command that does not work in-place
628 1.69 lukem */
629 1.69 lukem close(to_fd);
630 1.69 lukem if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
631 1.69 lukem err(1, "running after install command on %s", to_name);
632 1.9 jonathan }
633 1.9 jonathan
634 1.41 cgd /*
635 1.41 cgd * Set owner, group, mode for target; do the chown first,
636 1.41 cgd * chown may lose the setuid bits.
637 1.41 cgd */
638 1.50 lukem if (!dounpriv &&
639 1.50 lukem (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
640 1.41 cgd serrno = errno;
641 1.41 cgd (void)unlink(to_name);
642 1.41 cgd errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
643 1.1 cgd }
644 1.60 dillo tmpmode = mode;
645 1.58 tv if (dounpriv)
646 1.60 dillo tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
647 1.60 dillo if (fchmod(to_fd, tmpmode) == -1) {
648 1.5 jtc serrno = errno;
649 1.5 jtc (void)unlink(to_name);
650 1.5 jtc errx(1, "%s: chmod: %s", to_name, strerror(serrno));
651 1.5 jtc }
652 1.5 jtc
653 1.5 jtc /*
654 1.26 christos * Preserve the date of the source file.
655 1.26 christos */
656 1.26 christos if (dopreserve) {
657 1.65 tv #if HAVE_FUTIMES
658 1.65 tv if (futimes(to_fd, tv) == -1)
659 1.65 tv warn("%s: futimes", to_name);
660 1.65 tv #else
661 1.65 tv if (utimes(to_name, tv) == -1)
662 1.66 tv warn("%s: utimes", to_name);
663 1.65 tv #endif
664 1.5 jtc }
665 1.5 jtc
666 1.1 cgd (void)close(to_fd);
667 1.33 christos
668 1.50 lukem if (dorename) {
669 1.33 christos if (rename(to_name, oto_name) == -1)
670 1.33 christos err(1, "%s: rename", to_name);
671 1.50 lukem to_name = oto_name;
672 1.50 lukem }
673 1.33 christos
674 1.5 jtc if (!docopy && !devnull && unlink(from_name))
675 1.66 tv err(1, "%s: unlink", from_name);
676 1.50 lukem
677 1.50 lukem /*
678 1.50 lukem * If provided a set of flags, set them, otherwise, preserve the
679 1.50 lukem * flags, except for the dump flag.
680 1.50 lukem */
681 1.65 tv #if !HAVE_CONFIG_H
682 1.50 lukem if (!dounpriv && chflags(to_name,
683 1.51 lukem flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
684 1.51 lukem {
685 1.50 lukem if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
686 1.50 lukem warn("%s: chflags", to_name);
687 1.50 lukem }
688 1.65 tv #endif
689 1.50 lukem
690 1.59 perry metadata_log(to_name, "file", tv, NULL);
691 1.1 cgd }
692 1.1 cgd
693 1.1 cgd /*
694 1.1 cgd * copy --
695 1.1 cgd * copy from one file to another
696 1.1 cgd */
697 1.5 jtc void
698 1.48 simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
699 1.1 cgd {
700 1.53 lukem ssize_t nr, nw;
701 1.53 lukem int serrno;
702 1.53 lukem char *p;
703 1.53 lukem char buf[MAXBSIZE];
704 1.1 cgd
705 1.5 jtc /*
706 1.28 wsanchez * There's no reason to do anything other than close the file
707 1.28 wsanchez * now if it's empty, so let's not bother.
708 1.5 jtc */
709 1.28 wsanchez if (size > 0) {
710 1.45 chs
711 1.28 wsanchez /*
712 1.45 chs * Mmap and write if less than 8M (the limit is so we
713 1.45 chs * don't totally trash memory on big files). This is
714 1.45 chs * really a minor hack, but it wins some CPU back.
715 1.28 wsanchez */
716 1.45 chs
717 1.28 wsanchez if (size <= 8 * 1048576) {
718 1.28 wsanchez if ((p = mmap(NULL, (size_t)size, PROT_READ,
719 1.45 chs MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
720 1.45 chs == MAP_FAILED) {
721 1.45 chs goto mmap_failed;
722 1.36 thorpej }
723 1.44 cgd #ifdef MADV_SEQUENTIAL
724 1.39 christos if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
725 1.39 christos && errno != EOPNOTSUPP)
726 1.38 sommerfe warnx("madvise: %s", strerror(errno));
727 1.44 cgd #endif
728 1.38 sommerfe
729 1.36 thorpej if (write(to_fd, p, size) != size) {
730 1.36 thorpej serrno = errno;
731 1.36 thorpej (void)unlink(to_name);
732 1.66 tv errx(1, "%s: write: %s",
733 1.36 thorpej to_name, strerror(serrno));
734 1.36 thorpej }
735 1.28 wsanchez } else {
736 1.45 chs mmap_failed:
737 1.36 thorpej while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
738 1.28 wsanchez if ((nw = write(to_fd, buf, nr)) != nr) {
739 1.28 wsanchez serrno = errno;
740 1.28 wsanchez (void)unlink(to_name);
741 1.66 tv errx(1, "%s: write: %s", to_name,
742 1.45 chs strerror(nw > 0 ? EIO : serrno));
743 1.28 wsanchez }
744 1.36 thorpej }
745 1.28 wsanchez if (nr != 0) {
746 1.5 jtc serrno = errno;
747 1.5 jtc (void)unlink(to_name);
748 1.66 tv errx(1, "%s: read: %s", from_name, strerror(serrno));
749 1.5 jtc }
750 1.1 cgd }
751 1.1 cgd }
752 1.1 cgd }
753 1.1 cgd
754 1.1 cgd /*
755 1.1 cgd * strip --
756 1.1 cgd * use strip(1) to strip the target file
757 1.1 cgd */
758 1.5 jtc void
759 1.48 simonb strip(char *to_name)
760 1.1 cgd {
761 1.53 lukem int serrno, status;
762 1.53 lukem char *stripprog;
763 1.1 cgd
764 1.1 cgd switch (vfork()) {
765 1.1 cgd case -1:
766 1.5 jtc serrno = errno;
767 1.5 jtc (void)unlink(to_name);
768 1.22 thorpej errx(1, "vfork: %s", strerror(serrno));
769 1.61 simonb /*NOTREACHED*/
770 1.1 cgd case 0:
771 1.18 thorpej stripprog = getenv("STRIP");
772 1.18 thorpej if (stripprog == NULL)
773 1.18 thorpej stripprog = _PATH_STRIP;
774 1.28 wsanchez
775 1.28 wsanchez if (stripArgs) {
776 1.50 lukem /*
777 1.50 lukem * build up a command line and let /bin/sh
778 1.50 lukem * parse the arguments
779 1.50 lukem */
780 1.28 wsanchez char* cmd = (char*)malloc(sizeof(char)*
781 1.28 wsanchez (3+strlen(stripprog)+
782 1.28 wsanchez strlen(stripArgs)+
783 1.28 wsanchez strlen(to_name)));
784 1.49 simonb
785 1.49 simonb if (cmd == NULL)
786 1.49 simonb errx(1, "%s", strerror(ENOMEM));
787 1.28 wsanchez
788 1.28 wsanchez sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
789 1.28 wsanchez
790 1.28 wsanchez execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
791 1.28 wsanchez } else
792 1.40 cgd execlp(stripprog, "strip", to_name, NULL);
793 1.28 wsanchez
794 1.66 tv warn("%s: exec of strip", stripprog);
795 1.22 thorpej _exit(1);
796 1.61 simonb /*NOTREACHED*/
797 1.1 cgd default:
798 1.1 cgd if (wait(&status) == -1 || status)
799 1.5 jtc (void)unlink(to_name);
800 1.1 cgd }
801 1.32 hubertf }
802 1.32 hubertf
803 1.32 hubertf /*
804 1.69 lukem * afterinstall --
805 1.69 lukem * run provided command on the target file or directory after it's been
806 1.69 lukem * installed and stripped, but before permissions are set or it's renamed
807 1.69 lukem */
808 1.69 lukem void
809 1.69 lukem afterinstall(const char *command, const char *to_name, int errunlink)
810 1.69 lukem {
811 1.69 lukem int serrno, status;
812 1.69 lukem char *cmd;
813 1.69 lukem
814 1.69 lukem switch (vfork()) {
815 1.69 lukem case -1:
816 1.69 lukem serrno = errno;
817 1.69 lukem if (errunlink)
818 1.69 lukem (void)unlink(to_name);
819 1.69 lukem errx(1, "vfork: %s", strerror(serrno));
820 1.69 lukem /*NOTREACHED*/
821 1.69 lukem case 0:
822 1.69 lukem /*
823 1.69 lukem * build up a command line and let /bin/sh
824 1.69 lukem * parse the arguments
825 1.69 lukem */
826 1.69 lukem cmd = (char*)malloc(sizeof(char)*
827 1.69 lukem (2+strlen(command)+
828 1.69 lukem strlen(to_name)));
829 1.69 lukem
830 1.69 lukem if (cmd == NULL)
831 1.69 lukem errx(1, "%s", strerror(ENOMEM));
832 1.69 lukem
833 1.69 lukem sprintf(cmd, "%s %s", command, to_name);
834 1.69 lukem
835 1.69 lukem execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
836 1.69 lukem
837 1.69 lukem warn("%s: exec of after install command", command);
838 1.69 lukem _exit(1);
839 1.69 lukem /*NOTREACHED*/
840 1.69 lukem default:
841 1.69 lukem if ((wait(&status) == -1 || status) && errunlink)
842 1.69 lukem (void)unlink(to_name);
843 1.69 lukem }
844 1.69 lukem }
845 1.69 lukem
846 1.69 lukem /*
847 1.61 simonb * backup --
848 1.61 simonb * backup file "to_name" to to_name.suffix
849 1.61 simonb * if suffix contains a "%", it's taken as a printf(3) pattern
850 1.61 simonb * used for a numbered backup.
851 1.32 hubertf */
852 1.32 hubertf void
853 1.48 simonb backup(const char *to_name)
854 1.32 hubertf {
855 1.61 simonb char bname[FILENAME_MAX];
856 1.32 hubertf
857 1.35 hubertf if (numberedbackup) {
858 1.32 hubertf /* Do numbered backup */
859 1.32 hubertf int cnt;
860 1.32 hubertf char suffix_expanded[FILENAME_MAX];
861 1.32 hubertf
862 1.32 hubertf cnt=0;
863 1.32 hubertf do {
864 1.50 lukem (void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
865 1.50 lukem cnt);
866 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
867 1.61 simonb suffix_expanded);
868 1.32 hubertf cnt++;
869 1.61 simonb } while (access(bname, F_OK) == 0);
870 1.32 hubertf } else {
871 1.32 hubertf /* Do simple backup */
872 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
873 1.32 hubertf }
874 1.32 hubertf
875 1.61 simonb (void)rename(to_name, bname);
876 1.1 cgd }
877 1.1 cgd
878 1.1 cgd /*
879 1.5 jtc * install_dir --
880 1.47 wiz * build directory hierarchy
881 1.2 jtc */
882 1.7 jtc void
883 1.50 lukem install_dir(char *path, u_int flags)
884 1.2 jtc {
885 1.53 lukem char *p;
886 1.53 lukem struct stat sb;
887 1.53 lukem int ch;
888 1.2 jtc
889 1.4 cgd for (p = path;; ++p)
890 1.4 cgd if (!*p || (p != path && *p == '/')) {
891 1.2 jtc ch = *p;
892 1.2 jtc *p = '\0';
893 1.2 jtc if (stat(path, &sb)) {
894 1.2 jtc if (errno != ENOENT || mkdir(path, 0777) < 0) {
895 1.66 tv err(1, "%s: mkdir", path);
896 1.2 jtc }
897 1.2 jtc }
898 1.2 jtc if (!(*p = ch))
899 1.4 cgd break;
900 1.2 jtc }
901 1.2 jtc
902 1.69 lukem if (afterinstallcmd != NULL)
903 1.69 lukem afterinstall(afterinstallcmd, path, 0);
904 1.69 lukem
905 1.50 lukem if (!dounpriv && (
906 1.50 lukem ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
907 1.50 lukem || chmod(path, mode) == -1 )) {
908 1.66 tv warn("%s: chown/chmod", path);
909 1.41 cgd }
910 1.59 perry metadata_log(path, "dir", NULL, NULL);
911 1.2 jtc }
912 1.2 jtc
913 1.2 jtc /*
914 1.50 lukem * metadata_log --
915 1.50 lukem * if metafp is not NULL, output mtree(8) full path name and settings to
916 1.50 lukem * metafp, to allow permissions to be set correctly by other tools.
917 1.50 lukem */
918 1.50 lukem void
919 1.59 perry metadata_log(const char *path, const char *type, struct timeval *tv,
920 1.57 lukem const char *link)
921 1.50 lukem {
922 1.72 yamt static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
923 1.53 lukem char *buf;
924 1.73 lukem struct flock metalog_lock;
925 1.50 lukem
926 1.50 lukem if (!metafp)
927 1.50 lukem return;
928 1.50 lukem buf = (char *)malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
929 1.50 lukem if (buf == NULL) {
930 1.50 lukem warnx("%s", strerror(ENOMEM));
931 1.50 lukem return;
932 1.50 lukem }
933 1.73 lukem /* lock log file */
934 1.73 lukem metalog_lock.l_start = 0;
935 1.73 lukem metalog_lock.l_len = 0;
936 1.73 lukem metalog_lock.l_whence = SEEK_SET;
937 1.73 lukem metalog_lock.l_type = F_WRLCK;
938 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
939 1.50 lukem warn("can't lock %s", metafile);
940 1.50 lukem return;
941 1.50 lukem }
942 1.50 lukem
943 1.50 lukem strsvis(buf, path, VIS_CSTYLE, extra); /* encode name */
944 1.50 lukem fprintf(metafp, ".%s%s type=%s mode=%#o", /* print details */
945 1.59 perry buf[0] == '/' ? "" : "/", buf, type, mode);
946 1.57 lukem if (link)
947 1.57 lukem fprintf(metafp, " link=%s", link);
948 1.52 tv if (owner)
949 1.52 tv fprintf(metafp, " uname=%s", owner);
950 1.52 tv if (group)
951 1.52 tv fprintf(metafp, " gname=%s", group);
952 1.52 tv if (fflags)
953 1.52 tv fprintf(metafp, " flags=%s", fflags);
954 1.54 lukem if (tags)
955 1.54 lukem fprintf(metafp, " tags=%s", tags);
956 1.57 lukem if (tv != NULL && dopreserve)
957 1.50 lukem fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
958 1.50 lukem fputc('\n', metafp);
959 1.50 lukem fflush(metafp); /* flush output */
960 1.73 lukem /* unlock log file */
961 1.73 lukem metalog_lock.l_type = F_UNLCK;
962 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
963 1.50 lukem warn("can't unlock %s", metafile);
964 1.50 lukem }
965 1.50 lukem free(buf);
966 1.50 lukem }
967 1.50 lukem
968 1.61 simonb /*
969 1.61 simonb * xbasename --
970 1.61 simonb * libc basename(3) that returns a pointer to a static buffer
971 1.61 simonb * instead of overwriting that passed-in string.
972 1.61 simonb */
973 1.61 simonb char *
974 1.61 simonb xbasename(char *path)
975 1.61 simonb {
976 1.61 simonb static char tmp[MAXPATHLEN];
977 1.61 simonb
978 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
979 1.61 simonb return (basename(tmp));
980 1.61 simonb }
981 1.61 simonb
982 1.61 simonb /*
983 1.61 simonb * xdirname --
984 1.61 simonb * libc dirname(3) that returns a pointer to a static buffer
985 1.61 simonb * instead of overwriting that passed-in string.
986 1.61 simonb */
987 1.61 simonb char *
988 1.61 simonb xdirname(char *path)
989 1.61 simonb {
990 1.61 simonb static char tmp[MAXPATHLEN];
991 1.50 lukem
992 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
993 1.61 simonb return (dirname(tmp));
994 1.61 simonb }
995 1.50 lukem
996 1.50 lukem /*
997 1.1 cgd * usage --
998 1.1 cgd * print a usage message and die
999 1.1 cgd */
1000 1.5 jtc void
1001 1.48 simonb usage(void)
1002 1.1 cgd {
1003 1.69 lukem const char *prog;
1004 1.69 lukem
1005 1.69 lukem prog = getprogname();
1006 1.53 lukem
1007 1.69 lukem (void)fprintf(stderr,
1008 1.69 lukem "usage: %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
1009 1.74 lukem " [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group] [-l linkflags]\n"
1010 1.69 lukem " [-S stripflags] file1 file2\n"
1011 1.69 lukem " %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
1012 1.74 lukem " [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group] [-l linkflags]\n"
1013 1.69 lukem " [-S stripflags] file1 ... fileN directory\n"
1014 1.69 lukem " %s -d [-Up] [-M log] [-T tags] [-a afterinstallcmd] [-m mode]\n"
1015 1.74 lukem " [-N dbdir] [-o owner] [-g group] directory ...\n",
1016 1.69 lukem prog, prog, prog);
1017 1.6 jtc exit(1);
1018 1.1 cgd }
1019