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