Merge branch 'vendor/GCC44' into gcc441
[dragonfly.git] / contrib / libarchive / cpio / cpio.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27
28 #include "cpio_platform.h"
29 __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle Exp $");
30
31 #include <sys/types.h>
32 #include <archive.h>
33 #include <archive_entry.h>
34
35 #ifdef HAVE_SYS_MKDEV_H
36 #include <sys/mkdev.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50 #ifdef HAVE_GRP_H
51 #include <grp.h>
52 #endif
53 #ifdef HAVE_PWD_H
54 #include <pwd.h>
55 #endif
56 #ifdef HAVE_STDARG_H
57 #include <stdarg.h>
58 #endif
59 #include <stdio.h>
60 #ifdef HAVE_STDLIB_H
61 #include <stdlib.h>
62 #endif
63 #ifdef HAVE_STRING_H
64 #include <string.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69 #ifdef HAVE_SYS_TIME_H
70 #include <sys/time.h>
71 #endif
72 #ifdef HAVE_TIME_H
73 #include <time.h>
74 #endif
75
76 #include "cpio.h"
77 #include "matching.h"
78
79 /* Fixed size of uname/gname caches. */
80 #define name_cache_size 101
81
82 struct name_cache {
83         int     probes;
84         int     hits;
85         size_t  size;
86         struct {
87                 id_t id;
88                 char *name;
89         } cache[name_cache_size];
90 };
91
92 static int      copy_data(struct archive *, struct archive *);
93 static const char *cpio_rename(const char *name);
94 static int      entry_to_archive(struct cpio *, struct archive_entry *);
95 static int      file_to_archive(struct cpio *, const char *);
96 static void     free_cache(struct name_cache *cache);
97 static void     list_item_verbose(struct cpio *, struct archive_entry *);
98 static void     long_help(void);
99 static const char *lookup_gname(struct cpio *, gid_t gid);
100 static int      lookup_gname_helper(struct cpio *,
101                     const char **name, id_t gid);
102 static const char *lookup_uname(struct cpio *, uid_t uid);
103 static int      lookup_uname_helper(struct cpio *,
104                     const char **name, id_t uid);
105 static void     mode_in(struct cpio *);
106 static void     mode_list(struct cpio *);
107 static void     mode_out(struct cpio *);
108 static void     mode_pass(struct cpio *, const char *);
109 static int      restore_time(struct cpio *, struct archive_entry *,
110                     const char *, int fd);
111 static void     usage(void);
112 static void     version(void);
113
114 int
115 main(int argc, char *argv[])
116 {
117         static char buff[16384];
118         struct cpio _cpio; /* Allocated on stack. */
119         struct cpio *cpio;
120         int uid, gid;
121         int opt;
122
123         cpio = &_cpio;
124         memset(cpio, 0, sizeof(*cpio));
125         cpio->buff = buff;
126         cpio->buff_size = sizeof(buff);
127 #if defined(_WIN32) && !defined(__CYGWIN__)
128         /* Make sure open() function will be used with a binary mode. */
129         /* on cygwin, we need something similar, but instead link against */
130         /* a special startup object, binmode.o */
131         _set_fmode(_O_BINARY);
132 #endif
133
134         /* Need cpio_progname before calling cpio_warnc. */
135         if (*argv == NULL)
136                 cpio_progname = "bsdcpio";
137         else {
138 #if defined(_WIN32) && !defined(__CYGWIN__)
139                 cpio_progname = strrchr(*argv, '\\');
140 #else
141                 cpio_progname = strrchr(*argv, '/');
142 #endif
143                 if (cpio_progname != NULL)
144                         cpio_progname++;
145                 else
146                         cpio_progname = *argv;
147         }
148
149         cpio->uid_override = -1;
150         cpio->gid_override = -1;
151         cpio->argv = argv;
152         cpio->argc = argc;
153         cpio->line_separator = '\n';
154         cpio->mode = '\0';
155         cpio->verbose = 0;
156         cpio->compress = '\0';
157         cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR;
158         cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
159         cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS;
160         cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;
161         cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
162         cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
163         cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
164 #if defined(_WIN32) || defined(__CYGWIN__)
165         if (bsdcpio_is_privileged())
166 #else
167         if (geteuid() == 0)
168 #endif
169                 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
170         cpio->bytes_per_block = 512;
171         cpio->filename = NULL;
172
173         while ((opt = cpio_getopt(cpio)) != -1) {
174                 switch (opt) {
175                 case '0': /* GNU convention: --null, -0 */
176                         cpio->line_separator = '\0';
177                         break;
178                 case 'A': /* NetBSD/OpenBSD */
179                         cpio->option_append = 1;
180                         break;
181                 case 'a': /* POSIX 1997 */
182                         cpio->option_atime_restore = 1;
183                         break;
184                 case 'B': /* POSIX 1997 */
185                         cpio->bytes_per_block = 5120;
186                         break;
187                 case 'C': /* NetBSD/OpenBSD */
188                         cpio->bytes_per_block = atoi(cpio->optarg);
189                         if (cpio->bytes_per_block <= 0)
190                                 cpio_errc(1, 0, "Invalid blocksize %s", cpio->optarg);
191                         break;
192                 case 'c': /* POSIX 1997 */
193                         cpio->format = "odc";
194                         break;
195                 case 'd': /* POSIX 1997 */
196                         cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
197                         break;
198                 case 'E': /* NetBSD/OpenBSD */
199                         include_from_file(cpio, cpio->optarg);
200                         break;
201                 case 'F': /* NetBSD/OpenBSD/GNU cpio */
202                         cpio->filename = cpio->optarg;
203                         break;
204                 case 'f': /* POSIX 1997 */
205                         exclude(cpio, cpio->optarg);
206                         break;
207                 case 'H': /* GNU cpio (also --format) */
208                         cpio->format = cpio->optarg;
209                         break;
210                 case 'h':
211                         long_help();
212                         break;
213                 case 'I': /* NetBSD/OpenBSD */
214                         cpio->filename = cpio->optarg;
215                         break;
216                 case 'i': /* POSIX 1997 */
217                         if (cpio->mode != '\0')
218                                 cpio_errc(1, 0,
219                                     "Cannot use both -i and -%c", cpio->mode);
220                         cpio->mode = opt;
221                         break;
222                 case OPTION_INSECURE:
223                         cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
224                         cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
225                         break;
226                 case 'L': /* GNU cpio */
227                         cpio->option_follow_links = 1;
228                         break;
229                 case 'l': /* POSIX 1997 */
230                         cpio->option_link = 1;
231                         break;
232                 case 'm': /* POSIX 1997 */
233                         cpio->extract_flags |= ARCHIVE_EXTRACT_TIME;
234                         break;
235                 case 'n': /* GNU cpio */
236                         cpio->option_numeric_uid_gid = 1;
237                         break;
238                 case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */
239                         cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
240                         break;
241                 case 'O': /* GNU cpio */
242                         cpio->filename = cpio->optarg;
243                         break;
244                 case 'o': /* POSIX 1997 */
245                         if (cpio->mode != '\0')
246                                 cpio_errc(1, 0,
247                                     "Cannot use both -o and -%c", cpio->mode);
248                         cpio->mode = opt;
249                         break;
250                 case 'p': /* POSIX 1997 */
251                         if (cpio->mode != '\0')
252                                 cpio_errc(1, 0,
253                                     "Cannot use both -p and -%c", cpio->mode);
254                         cpio->mode = opt;
255                         cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
256                         break;
257                 case OPTION_QUIET: /* GNU cpio */
258                         cpio->quiet = 1;
259                         break;
260                 case 'R': /* GNU cpio, also --owner */
261                         if (owner_parse(cpio->optarg, &uid, &gid))
262                                 usage();
263                         if (uid != -1)
264                                 cpio->uid_override = uid;
265                         if (gid != -1)
266                                 cpio->gid_override = gid;
267                         break;
268                 case 'r': /* POSIX 1997 */
269                         cpio->option_rename = 1;
270                         break;
271                 case 't': /* POSIX 1997 */
272                         cpio->option_list = 1;
273                         break;
274                 case 'u': /* POSIX 1997 */
275                         cpio->extract_flags
276                             &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
277                         break;
278                 case 'v': /* POSIX 1997 */
279                         cpio->verbose++;
280                         break;
281                 case OPTION_VERSION: /* GNU convention */
282                         version();
283                         break;
284 #if 0
285                 /*
286                  * cpio_getopt() handles -W specially, so it's not
287                  * available here.
288                  */
289                 case 'W': /* Obscure, but useful GNU convention. */
290                         break;
291 #endif
292                 case 'y': /* tar convention */
293 #if HAVE_LIBBZ2
294                         cpio->compress = opt;
295 #else
296                         cpio_warnc(0, "bzip2 compression not supported by "
297                             "this version of bsdcpio");
298 #endif
299                         break;
300                 case 'Z': /* tar convention */
301                         cpio->compress = opt;
302                         break;
303                 case 'z': /* tar convention */
304 #if HAVE_LIBZ
305                         cpio->compress = opt;
306 #else
307                         cpio_warnc(0, "gzip compression not supported by "
308                             "this version of bsdcpio");
309 #endif
310                         break;
311                 default:
312                         usage();
313                 }
314         }
315
316         /*
317          * Sanity-check args, error out on nonsensical combinations.
318          */
319         /* -t implies -i if no mode was specified. */
320         if (cpio->option_list && cpio->mode == '\0')
321                 cpio->mode = 'i';
322         /* -t requires -i */
323         if (cpio->option_list && cpio->mode != 'i')
324                 cpio_errc(1, 0, "Option -t requires -i", cpio->mode);
325         /* -n requires -it */
326         if (cpio->option_numeric_uid_gid && !cpio->option_list)
327                 cpio_errc(1, 0, "Option -n requires -it");
328         /* Can only specify format when writing */
329         if (cpio->format != NULL && cpio->mode != 'o')
330                 cpio_errc(1, 0, "Option --format requires -o");
331         /* -l requires -p */
332         if (cpio->option_link && cpio->mode != 'p')
333                 cpio_errc(1, 0, "Option -l requires -p");
334         /* TODO: Flag other nonsensical combinations. */
335
336         switch (cpio->mode) {
337         case 'o':
338                 /* TODO: Implement old binary format in libarchive,
339                    use that here. */
340                 if (cpio->format == NULL)
341                         cpio->format = "odc"; /* Default format */
342
343                 mode_out(cpio);
344                 break;
345         case 'i':
346                 while (*cpio->argv != NULL) {
347                         include(cpio, *cpio->argv);
348                         --cpio->argc;
349                         ++cpio->argv;
350                 }
351                 if (cpio->option_list)
352                         mode_list(cpio);
353                 else
354                         mode_in(cpio);
355                 break;
356         case 'p':
357                 if (*cpio->argv == NULL || **cpio->argv == '\0')
358                         cpio_errc(1, 0,
359                             "-p mode requires a target directory");
360                 mode_pass(cpio, *cpio->argv);
361                 break;
362         default:
363                 cpio_errc(1, 0,
364                     "Must specify at least one of -i, -o, or -p");
365         }
366
367         free_cache(cpio->gname_cache);
368         free_cache(cpio->uname_cache);
369         return (0);
370 }
371
372 void
373 usage(void)
374 {
375         const char      *p;
376
377         p = cpio_progname;
378
379         fprintf(stderr, "Brief Usage:\n");
380         fprintf(stderr, "  List:    %s -it < archive\n", p);
381         fprintf(stderr, "  Extract: %s -i < archive\n", p);
382         fprintf(stderr, "  Create:  %s -o < filenames > archive\n", p);
383         fprintf(stderr, "  Help:    %s --help\n", p);
384         exit(1);
385 }
386
387 static const char *long_help_msg =
388         "First option must be a mode specifier:\n"
389         "  -i Input  -o Output  -p Pass\n"
390         "Common Options:\n"
391         "  -v    Verbose\n"
392         "Create: %p -o [options]  < [list of files] > [archive]\n"
393 #ifdef HAVE_BZLIB_H
394         "  -y  Compress archive with bzip2\n"
395 #endif
396 #ifdef HAVE_ZLIB_H
397         "  -z  Compress archive with gzip\n"
398 #endif
399         "  --format {odc|newc|ustar}  Select archive format\n"
400         "List: %p -it < [archive]\n"
401         "Extract: %p -i [options] < [archive]\n";
402
403
404 /*
405  * Note that the word 'bsdcpio' will always appear in the first line
406  * of output.
407  *
408  * In particular, /bin/sh scripts that need to test for the presence
409  * of bsdcpio can use the following template:
410  *
411  * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
412  *          echo bsdcpio; else echo not bsdcpio; fi
413  */
414 static void
415 long_help(void)
416 {
417         const char      *prog;
418         const char      *p;
419
420         prog = cpio_progname;
421
422         fflush(stderr);
423
424         p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : "";
425         printf("%s%s: manipulate archive files\n", prog, p);
426
427         for (p = long_help_msg; *p != '\0'; p++) {
428                 if (*p == '%') {
429                         if (p[1] == 'p') {
430                                 fputs(prog, stdout);
431                                 p++;
432                         } else
433                                 putchar('%');
434                 } else
435                         putchar(*p);
436         }
437         version();
438 }
439
440 static void
441 version(void)
442 {
443         fprintf(stdout,"bsdcpio %s -- %s\n",
444             BSDCPIO_VERSION_STRING,
445             archive_version());
446         exit(0);
447 }
448
449 static void
450 mode_out(struct cpio *cpio)
451 {
452         unsigned long blocks;
453         struct archive_entry *entry, *spare;
454         struct line_reader *lr;
455         const char *p;
456         int r;
457
458         if (cpio->option_append)
459                 cpio_errc(1, 0, "Append mode not yet supported.");
460         cpio->archive = archive_write_new();
461         if (cpio->archive == NULL)
462                 cpio_errc(1, 0, "Failed to allocate archive object");
463         switch (cpio->compress) {
464 #ifdef HAVE_BZLIB_H
465         case 'j': case 'y':
466                 archive_write_set_compression_bzip2(cpio->archive);
467                 break;
468 #endif
469 #ifdef HAVE_ZLIB_H
470         case 'z':
471                 archive_write_set_compression_gzip(cpio->archive);
472                 break;
473 #endif
474         case 'Z':
475                 archive_write_set_compression_compress(cpio->archive);
476                 break;
477         default:
478                 archive_write_set_compression_none(cpio->archive);
479                 break;
480         }
481         r = archive_write_set_format_by_name(cpio->archive, cpio->format);
482         if (r != ARCHIVE_OK)
483                 cpio_errc(1, 0, archive_error_string(cpio->archive));
484         archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block);
485         cpio->linkresolver = archive_entry_linkresolver_new();
486         archive_entry_linkresolver_set_strategy(cpio->linkresolver,
487             archive_format(cpio->archive));
488
489         r = archive_write_open_file(cpio->archive, cpio->filename);
490         if (r != ARCHIVE_OK)
491                 cpio_errc(1, 0, archive_error_string(cpio->archive));
492         lr = process_lines_init("-", cpio->line_separator);
493         while ((p = process_lines_next(lr)) != NULL)
494                 file_to_archive(cpio, p);
495         process_lines_free(lr);
496
497         /*
498          * The hardlink detection may have queued up a couple of entries
499          * that can now be flushed.
500          */
501         entry = NULL;
502         archive_entry_linkify(cpio->linkresolver, &entry, &spare);
503         while (entry != NULL) {
504                 entry_to_archive(cpio, entry);
505                 archive_entry_free(entry);
506                 entry = NULL;
507                 archive_entry_linkify(cpio->linkresolver, &entry, &spare);
508         }
509
510         r = archive_write_close(cpio->archive);
511         if (r != ARCHIVE_OK)
512                 cpio_errc(1, 0, archive_error_string(cpio->archive));
513
514         if (!cpio->quiet) {
515                 blocks = (archive_position_uncompressed(cpio->archive) + 511)
516                               / 512;
517                 fprintf(stderr, "%lu %s\n", blocks,
518                     blocks == 1 ? "block" : "blocks");
519         }
520         archive_write_finish(cpio->archive);
521 }
522
523 /*
524  * This is used by both out mode (to copy objects from disk into
525  * an archive) and pass mode (to copy objects from disk to
526  * an archive_write_disk "archive").
527  */
528 static int
529 file_to_archive(struct cpio *cpio, const char *srcpath)
530 {
531         struct stat st;
532         const char *destpath;
533         struct archive_entry *entry, *spare;
534         size_t len;
535         const char *p;
536 #if !defined(_WIN32) || defined(__CYGWIN__)
537         int lnklen;
538 #endif
539         int r;
540
541         /*
542          * Create an archive_entry describing the source file.
543          *
544          * XXX TODO: rework to use archive_read_disk_entry_from_file()
545          */
546         entry = archive_entry_new();
547         if (entry == NULL)
548                 cpio_errc(1, 0, "Couldn't allocate entry");
549         archive_entry_copy_sourcepath(entry, srcpath);
550
551         /* Get stat information. */
552         if (cpio->option_follow_links)
553                 r = stat(srcpath, &st);
554         else
555                 r = lstat(srcpath, &st);
556         if (r != 0) {
557                 cpio_warnc(errno, "Couldn't stat \"%s\"", srcpath);
558                 archive_entry_free(entry);
559                 return (0);
560         }
561
562         if (cpio->uid_override >= 0)
563                 st.st_uid = cpio->uid_override;
564         if (cpio->gid_override >= 0)
565                 st.st_gid = cpio->uid_override;
566         archive_entry_copy_stat(entry, &st);
567
568 #if !defined(_WIN32) || defined(__CYGWIN__)
569         /* If its a symlink, pull the target. */
570         if (S_ISLNK(st.st_mode)) {
571                 lnklen = readlink(srcpath, cpio->buff, cpio->buff_size);
572                 if (lnklen < 0) {
573                         cpio_warnc(errno,
574                             "%s: Couldn't read symbolic link", srcpath);
575                         archive_entry_free(entry);
576                         return (0);
577                 }
578                 cpio->buff[lnklen] = 0;
579                 archive_entry_set_symlink(entry, cpio->buff);
580         }
581 #endif
582
583         /*
584          * Generate a destination path for this entry.
585          * "destination path" is the name to which it will be copied in
586          * pass mode or the name that will go into the archive in
587          * output mode.
588          */
589         destpath = srcpath;
590         if (cpio->destdir) {
591                 len = strlen(cpio->destdir) + strlen(srcpath) + 8;
592                 if (len >= cpio->pass_destpath_alloc) {
593                         while (len >= cpio->pass_destpath_alloc) {
594                                 cpio->pass_destpath_alloc += 512;
595                                 cpio->pass_destpath_alloc *= 2;
596                         }
597                         free(cpio->pass_destpath);
598                         cpio->pass_destpath = malloc(cpio->pass_destpath_alloc);
599                         if (cpio->pass_destpath == NULL)
600                                 cpio_errc(1, ENOMEM,
601                                     "Can't allocate path buffer");
602                 }
603                 strcpy(cpio->pass_destpath, cpio->destdir);
604                 p = srcpath;
605                 while (p[0] == '/')
606                         ++p;
607                 strcat(cpio->pass_destpath, p);
608                 destpath = cpio->pass_destpath;
609         }
610         if (cpio->option_rename)
611                 destpath = cpio_rename(destpath);
612         if (destpath == NULL)
613                 return (0);
614         archive_entry_copy_pathname(entry, destpath);
615
616         /*
617          * If we're trying to preserve hardlinks, match them here.
618          */
619         spare = NULL;
620         if (cpio->linkresolver != NULL
621             && !S_ISDIR(st.st_mode)) {
622                 archive_entry_linkify(cpio->linkresolver, &entry, &spare);
623         }
624
625         if (entry != NULL) {
626                 r = entry_to_archive(cpio, entry);
627                 archive_entry_free(entry);
628         }
629         if (spare != NULL) {
630                 if (r == 0)
631                         r = entry_to_archive(cpio, spare);
632                 archive_entry_free(spare);
633         }
634         return (r);
635 }
636
637 static int
638 entry_to_archive(struct cpio *cpio, struct archive_entry *entry)
639 {
640         const char *destpath = archive_entry_pathname(entry);
641         const char *srcpath = archive_entry_sourcepath(entry);
642         int fd = -1;
643         ssize_t bytes_read;
644         int r;
645
646         /* Print out the destination name to the user. */
647         if (cpio->verbose)
648                 fprintf(stderr,"%s", destpath);
649
650         /*
651          * Option_link only makes sense in pass mode and for
652          * regular files.  Also note: if a link operation fails
653          * because of cross-device restrictions, we'll fall back
654          * to copy mode for that entry.
655          *
656          * TODO: Test other cpio implementations to see if they
657          * hard-link anything other than regular files here.
658          */
659         if (cpio->option_link
660             && archive_entry_filetype(entry) == AE_IFREG)
661         {
662                 struct archive_entry *t;
663                 /* Save the original entry in case we need it later. */
664                 t = archive_entry_clone(entry);
665                 if (t == NULL)
666                         cpio_errc(1, ENOMEM, "Can't create link");
667                 /* Note: link(2) doesn't create parent directories,
668                  * so we use archive_write_header() instead as a
669                  * convenience. */
670                 archive_entry_set_hardlink(t, srcpath);
671                 /* This is a straight link that carries no data. */
672                 archive_entry_set_size(t, 0);
673                 r = archive_write_header(cpio->archive, t);
674                 archive_entry_free(t);
675                 if (r != ARCHIVE_OK)
676                         cpio_warnc(archive_errno(cpio->archive),
677                             archive_error_string(cpio->archive));
678                 if (r == ARCHIVE_FATAL)
679                         exit(1);
680 #ifdef EXDEV
681                 if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) {
682                         /* Cross-device link:  Just fall through and use
683                          * the original entry to copy the file over. */
684                         cpio_warnc(0, "Copying file instead");
685                 } else
686 #endif
687                 return (0);
688         }
689
690         /*
691          * Make sure we can open the file (if necessary) before
692          * trying to write the header.
693          */
694         if (archive_entry_filetype(entry) == AE_IFREG) {
695                 if (archive_entry_size(entry) > 0) {
696                         fd = open(srcpath, O_RDONLY);
697                         if (fd < 0) {
698                                 cpio_warnc(errno,
699                                     "%s: could not open file", srcpath);
700                                 goto cleanup;
701                         }
702                 }
703         } else {
704                 archive_entry_set_size(entry, 0);
705         }
706
707         r = archive_write_header(cpio->archive, entry);
708
709         if (r != ARCHIVE_OK)
710                 cpio_warnc(archive_errno(cpio->archive),
711                     "%s: %s",
712                     srcpath,
713                     archive_error_string(cpio->archive));
714
715         if (r == ARCHIVE_FATAL)
716                 exit(1);
717
718         if (r >= ARCHIVE_WARN && fd >= 0) {
719                 bytes_read = read(fd, cpio->buff, cpio->buff_size);
720                 while (bytes_read > 0) {
721                         r = archive_write_data(cpio->archive,
722                             cpio->buff, bytes_read);
723                         if (r < 0)
724                                 cpio_errc(1, archive_errno(cpio->archive),
725                                     archive_error_string(cpio->archive));
726                         if (r < bytes_read) {
727                                 cpio_warnc(0,
728                                     "Truncated write; file may have grown while being archived.");
729                         }
730                         bytes_read = read(fd, cpio->buff, cpio->buff_size);
731                 }
732         }
733
734         fd = restore_time(cpio, entry, srcpath, fd);
735
736 cleanup:
737         if (cpio->verbose)
738                 fprintf(stderr,"\n");
739         if (fd >= 0)
740                 close(fd);
741         return (0);
742 }
743
744 static int
745 restore_time(struct cpio *cpio, struct archive_entry *entry,
746     const char *name, int fd)
747 {
748 #ifndef HAVE_UTIMES
749         static int warned = 0;
750
751         (void)cpio; /* UNUSED */
752         (void)entry; /* UNUSED */
753         (void)name; /* UNUSED */
754
755         if (!warned)
756                 cpio_warnc(0, "Can't restore access times on this platform");
757         warned = 1;
758         return (fd);
759 #else
760 #if defined(_WIN32) && !defined(__CYGWIN__)
761         struct __timeval times[2];
762 #else
763         struct timeval times[2];
764 #endif
765
766         if (!cpio->option_atime_restore)
767                 return (fd);
768
769         times[1].tv_sec = archive_entry_mtime(entry);
770         times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
771
772         times[0].tv_sec = archive_entry_atime(entry);
773         times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000;
774
775 #ifdef HAVE_FUTIMES
776         if (fd >= 0 && futimes(fd, times) == 0)
777                 return (fd);
778 #endif
779         /*
780          * Some platform cannot restore access times if the file descriptor
781          * is still opened.
782          */
783         if (fd >= 0) {
784                 close(fd);
785                 fd = -1;
786         }
787
788 #ifdef HAVE_LUTIMES
789         if (lutimes(name, times) != 0)
790 #else
791         if (!S_ISLNK(archive_entry_mode(entry)) && utimes(name, times) != 0)
792 #endif
793                 cpio_warnc(errno, "Can't update time for %s", name);
794 #endif
795         return (fd);
796 }
797
798
799 static void
800 mode_in(struct cpio *cpio)
801 {
802         struct archive *a;
803         struct archive_entry *entry;
804         struct archive *ext;
805         const char *destpath;
806         unsigned long blocks;
807         int r;
808
809         ext = archive_write_disk_new();
810         if (ext == NULL)
811                 cpio_errc(1, 0, "Couldn't allocate restore object");
812         r = archive_write_disk_set_options(ext, cpio->extract_flags);
813         if (r != ARCHIVE_OK)
814                 cpio_errc(1, 0, archive_error_string(ext));
815         a = archive_read_new();
816         if (a == NULL)
817                 cpio_errc(1, 0, "Couldn't allocate archive object");
818         archive_read_support_compression_all(a);
819         archive_read_support_format_all(a);
820
821         if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
822                 cpio_errc(1, archive_errno(a),
823                     archive_error_string(a));
824         for (;;) {
825                 r = archive_read_next_header(a, &entry);
826                 if (r == ARCHIVE_EOF)
827                         break;
828                 if (r != ARCHIVE_OK) {
829                         cpio_errc(1, archive_errno(a),
830                             archive_error_string(a));
831                 }
832                 if (excluded(cpio, archive_entry_pathname(entry)))
833                         continue;
834                 if (cpio->option_rename) {
835                         destpath = cpio_rename(archive_entry_pathname(entry));
836                         archive_entry_set_pathname(entry, destpath);
837                 } else
838                         destpath = archive_entry_pathname(entry);
839                 if (destpath == NULL)
840                         continue;
841                 if (cpio->verbose)
842                         fprintf(stdout, "%s\n", destpath);
843                 if (cpio->uid_override >= 0)
844                         archive_entry_set_uid(entry, cpio->uid_override);
845                 if (cpio->gid_override >= 0)
846                         archive_entry_set_gid(entry, cpio->gid_override);
847                 r = archive_write_header(ext, entry);
848                 if (r != ARCHIVE_OK) {
849                         fprintf(stderr, "%s: %s\n",
850                             archive_entry_pathname(entry),
851                             archive_error_string(ext));
852                 } else if (archive_entry_size(entry) > 0) {
853                         r = copy_data(a, ext);
854                 }
855         }
856         r = archive_read_close(a);
857         if (r != ARCHIVE_OK)
858                 cpio_errc(1, 0, archive_error_string(a));
859         r = archive_write_close(ext);
860         if (r != ARCHIVE_OK)
861                 cpio_errc(1, 0, archive_error_string(ext));
862         if (!cpio->quiet) {
863                 blocks = (archive_position_uncompressed(a) + 511)
864                               / 512;
865                 fprintf(stderr, "%lu %s\n", blocks,
866                     blocks == 1 ? "block" : "blocks");
867         }
868         archive_read_finish(a);
869         archive_write_finish(ext);
870         exit(0);
871 }
872
873 static int
874 copy_data(struct archive *ar, struct archive *aw)
875 {
876         int r;
877         size_t size;
878         const void *block;
879         off_t offset;
880
881         for (;;) {
882                 r = archive_read_data_block(ar, &block, &size, &offset);
883                 if (r == ARCHIVE_EOF)
884                         return (ARCHIVE_OK);
885                 if (r != ARCHIVE_OK) {
886                         cpio_warnc(archive_errno(ar),
887                             "%s", archive_error_string(ar));
888                         return (r);
889                 }
890                 r = archive_write_data_block(aw, block, size, offset);
891                 if (r != ARCHIVE_OK) {
892                         cpio_warnc(archive_errno(aw),
893                             archive_error_string(aw));
894                         return (r);
895                 }
896         }
897 }
898
899 static void
900 mode_list(struct cpio *cpio)
901 {
902         struct archive *a;
903         struct archive_entry *entry;
904         unsigned long blocks;
905         int r;
906
907         a = archive_read_new();
908         if (a == NULL)
909                 cpio_errc(1, 0, "Couldn't allocate archive object");
910         archive_read_support_compression_all(a);
911         archive_read_support_format_all(a);
912
913         if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block))
914                 cpio_errc(1, archive_errno(a),
915                     archive_error_string(a));
916         for (;;) {
917                 r = archive_read_next_header(a, &entry);
918                 if (r == ARCHIVE_EOF)
919                         break;
920                 if (r != ARCHIVE_OK) {
921                         cpio_errc(1, archive_errno(a),
922                             archive_error_string(a));
923                 }
924                 if (excluded(cpio, archive_entry_pathname(entry)))
925                         continue;
926                 if (cpio->verbose)
927                         list_item_verbose(cpio, entry);
928                 else
929                         fprintf(stdout, "%s\n", archive_entry_pathname(entry));
930         }
931         r = archive_read_close(a);
932         if (r != ARCHIVE_OK)
933                 cpio_errc(1, 0, archive_error_string(a));
934         if (!cpio->quiet) {
935                 blocks = (archive_position_uncompressed(a) + 511)
936                               / 512;
937                 fprintf(stderr, "%lu %s\n", blocks,
938                     blocks == 1 ? "block" : "blocks");
939         }
940         archive_read_finish(a);
941         exit(0);
942 }
943
944 /*
945  * Display information about the current file.
946  *
947  * The format here roughly duplicates the output of 'ls -l'.
948  * This is based on SUSv2, where 'tar tv' is documented as
949  * listing additional information in an "unspecified format,"
950  * and 'pax -l' is documented as using the same format as 'ls -l'.
951  */
952 static void
953 list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
954 {
955         char                     size[32];
956         char                     date[32];
957         char                     uids[16], gids[16];
958         const char              *uname, *gname;
959         FILE                    *out = stdout;
960         const struct stat       *st;
961         const char              *fmt;
962         time_t                   tim;
963         static time_t            now;
964
965         st = archive_entry_stat(entry);
966
967         if (!now)
968                 time(&now);
969
970         if (cpio->option_numeric_uid_gid) {
971                 /* Format numeric uid/gid for display. */
972                 snprintf(uids, sizeof(uids), "%jd",
973                     (intmax_t)archive_entry_uid(entry));
974                 uname = uids;
975                 snprintf(gids, sizeof(gids), "%jd",
976                     (intmax_t)archive_entry_gid(entry));
977                 gname = gids;
978         } else {
979                 /* Use uname if it's present, else lookup name from uid. */
980                 uname = archive_entry_uname(entry);
981                 if (uname == NULL)
982                         uname = lookup_uname(cpio, archive_entry_uid(entry));
983                 /* Use gname if it's present, else lookup name from gid. */
984                 gname = archive_entry_gname(entry);
985                 if (gname == NULL)
986                         gname = lookup_gname(cpio, archive_entry_gid(entry));
987         }
988
989         /* Print device number or file size. */
990         if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
991                 snprintf(size, sizeof(size), "%lu,%lu",
992                     (unsigned long)major(st->st_rdev),
993                     (unsigned long)minor(st->st_rdev)); /* ls(1) also casts here. */
994         } else {
995                 snprintf(size, sizeof(size), CPIO_FILESIZE_PRINTF,
996                     (CPIO_FILESIZE_TYPE)st->st_size);
997         }
998
999         /* Format the time using 'ls -l' conventions. */
1000         tim = (time_t)st->st_mtime;
1001 #if defined(_WIN32) && !defined(__CYGWIN__)
1002         /* Windows' strftime function does not support %e format. */
1003         if (abs(tim - now) > (365/2)*86400)
1004                 fmt = cpio->day_first ? "%d %b  %Y" : "%b %d  %Y";
1005         else
1006                 fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
1007 #else
1008         if (abs(tim - now) > (365/2)*86400)
1009                 fmt = cpio->day_first ? "%e %b  %Y" : "%b %e  %Y";
1010         else
1011                 fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
1012 #endif
1013         strftime(date, sizeof(date), fmt, localtime(&tim));
1014
1015         fprintf(out, "%s%3d %-8s %-8s %8s %12s %s",
1016             archive_entry_strmode(entry),
1017             archive_entry_nlink(entry),
1018             uname, gname, size, date,
1019             archive_entry_pathname(entry));
1020
1021         /* Extra information for links. */
1022         if (archive_entry_hardlink(entry)) /* Hard link */
1023                 fprintf(out, " link to %s", archive_entry_hardlink(entry));
1024         else if (archive_entry_symlink(entry)) /* Symbolic link */
1025                 fprintf(out, " -> %s", archive_entry_symlink(entry));
1026         fprintf(out, "\n");
1027 }
1028
1029 static void
1030 mode_pass(struct cpio *cpio, const char *destdir)
1031 {
1032         unsigned long blocks;
1033         struct line_reader *lr;
1034         const char *p;
1035         int r;
1036
1037         /* Ensure target dir has a trailing '/' to simplify path surgery. */
1038         cpio->destdir = malloc(strlen(destdir) + 8);
1039         strcpy(cpio->destdir, destdir);
1040         if (destdir[strlen(destdir) - 1] != '/')
1041                 strcat(cpio->destdir, "/");
1042
1043         cpio->archive = archive_write_disk_new();
1044         if (cpio->archive == NULL)
1045                 cpio_errc(1, 0, "Failed to allocate archive object");
1046         r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags);
1047         if (r != ARCHIVE_OK)
1048                 cpio_errc(1, 0, archive_error_string(cpio->archive));
1049         cpio->linkresolver = archive_entry_linkresolver_new();
1050         archive_write_disk_set_standard_lookup(cpio->archive);
1051         lr = process_lines_init("-", cpio->line_separator);
1052         while ((p = process_lines_next(lr)) != NULL)
1053                 file_to_archive(cpio, p);
1054         process_lines_free(lr);
1055
1056         archive_entry_linkresolver_free(cpio->linkresolver);
1057         r = archive_write_close(cpio->archive);
1058         if (r != ARCHIVE_OK)
1059                 cpio_errc(1, 0, archive_error_string(cpio->archive));
1060
1061         if (!cpio->quiet) {
1062                 blocks = (archive_position_uncompressed(cpio->archive) + 511)
1063                               / 512;
1064                 fprintf(stderr, "%lu %s\n", blocks,
1065                     blocks == 1 ? "block" : "blocks");
1066         }
1067
1068         archive_write_finish(cpio->archive);
1069 }
1070
1071 /*
1072  * Prompt for a new name for this entry.  Returns a pointer to the
1073  * new name or NULL if the entry should not be copied.  This
1074  * implements the semantics defined in POSIX.1-1996, which specifies
1075  * that an input of '.' means the name should be unchanged.  GNU cpio
1076  * treats '.' as a literal new name.
1077  */
1078 static const char *
1079 cpio_rename(const char *name)
1080 {
1081         static char buff[1024];
1082         FILE *t;
1083         char *p, *ret;
1084
1085         t = fopen("/dev/tty", "r+");
1086         if (t == NULL)
1087                 return (name);
1088         fprintf(t, "%s (Enter/./(new name))? ", name);
1089         fflush(t);
1090
1091         p = fgets(buff, sizeof(buff), t);
1092         fclose(t);
1093         if (p == NULL)
1094                 /* End-of-file is a blank line. */
1095                 return (NULL);
1096
1097         while (*p == ' ' || *p == '\t')
1098                 ++p;
1099         if (*p == '\n' || *p == '\0')
1100                 /* Empty line. */
1101                 return (NULL);
1102         if (*p == '.' && p[1] == '\n')
1103                 /* Single period preserves original name. */
1104                 return (name);
1105         ret = p;
1106         /* Trim the final newline. */
1107         while (*p != '\0' && *p != '\n')
1108                 ++p;
1109         /* Overwrite the final \n with a null character. */
1110         *p = '\0';
1111         return (ret);
1112 }
1113
1114
1115 /*
1116  * Read lines from file and do something with each one.  If option_null
1117  * is set, lines are terminated with zero bytes; otherwise, they're
1118  * terminated with newlines.
1119  *
1120  * This uses a self-sizing buffer to handle arbitrarily-long lines.
1121  */
1122 struct line_reader {
1123         FILE *f;
1124         char *buff, *buff_end, *line_start, *line_end, *p;
1125         char *pathname;
1126         size_t buff_length;
1127         int separator;
1128         int ret;
1129 };
1130
1131 struct line_reader *
1132 process_lines_init(const char *pathname, char separator)
1133 {
1134         struct line_reader *lr;
1135
1136         lr = calloc(1, sizeof(*lr));
1137         if (lr == NULL)
1138                 cpio_errc(1, ENOMEM, "Can't open %s", pathname);
1139
1140         lr->separator = separator;
1141         lr->pathname = strdup(pathname);
1142
1143         if (strcmp(pathname, "-") == 0)
1144                 lr->f = stdin;
1145         else
1146                 lr->f = fopen(pathname, "r");
1147         if (lr->f == NULL)
1148                 cpio_errc(1, errno, "Couldn't open %s", pathname);
1149         lr->buff_length = 8192;
1150         lr->buff = malloc(lr->buff_length);
1151         if (lr->buff == NULL)
1152                 cpio_errc(1, ENOMEM, "Can't read %s", pathname);
1153         lr->line_start = lr->line_end = lr->buff_end = lr->buff;
1154
1155         return (lr);
1156 }
1157
1158 const char *
1159 process_lines_next(struct line_reader *lr)
1160 {
1161         size_t bytes_wanted, bytes_read, new_buff_size;
1162         char *line_start, *p;
1163
1164         for (;;) {
1165                 /* If there's a line in the buffer, return it immediately. */
1166                 while (lr->line_end < lr->buff_end) {
1167                         if (*lr->line_end == lr->separator) {
1168                                 *lr->line_end = '\0';
1169                                 line_start = lr->line_start;
1170                                 lr->line_start = lr->line_end + 1;
1171                                 lr->line_end = lr->line_start;
1172                                 return (line_start);
1173                         } else
1174                                 lr->line_end++;
1175                 }
1176
1177                 /* If we're at end-of-file, process the final data. */
1178                 if (lr->f == NULL) {
1179                         /* If there's more text, return one last line. */
1180                         if (lr->line_end > lr->line_start) {
1181                                 *lr->line_end = '\0';
1182                                 line_start = lr->line_start;
1183                                 lr->line_start = lr->line_end + 1;
1184                                 lr->line_end = lr->line_start;
1185                                 return (line_start);
1186                         }
1187                         /* Otherwise, we're done. */
1188                         return (NULL);
1189                 }
1190
1191                 /* Buffer only has part of a line. */
1192                 if (lr->line_start > lr->buff) {
1193                         /* Move a leftover fractional line to the beginning. */
1194                         memmove(lr->buff, lr->line_start,
1195                             lr->buff_end - lr->line_start);
1196                         lr->buff_end -= lr->line_start - lr->buff;
1197                         lr->line_end -= lr->line_start - lr->buff;
1198                         lr->line_start = lr->buff;
1199                 } else {
1200                         /* Line is too big; enlarge the buffer. */
1201                         new_buff_size = lr->buff_length * 2;
1202                         if (new_buff_size <= lr->buff_length)
1203                                 cpio_errc(1, ENOMEM,
1204                                     "Line too long in %s", lr->pathname);
1205                         lr->buff_length = new_buff_size;
1206                         p = realloc(lr->buff, new_buff_size);
1207                         if (p == NULL)
1208                                 cpio_errc(1, ENOMEM,
1209                                     "Line too long in %s", lr->pathname);
1210                         lr->buff_end = p + (lr->buff_end - lr->buff);
1211                         lr->line_end = p + (lr->line_end - lr->buff);
1212                         lr->line_start = lr->buff = p;
1213                 }
1214
1215                 /* Get some more data into the buffer. */
1216                 bytes_wanted = lr->buff + lr->buff_length - lr->buff_end;
1217                 bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f);
1218                 lr->buff_end += bytes_read;
1219
1220                 if (ferror(lr->f))
1221                         cpio_errc(1, errno, "Can't read %s", lr->pathname);
1222                 if (feof(lr->f)) {
1223                         if (lr->f != stdin)
1224                                 fclose(lr->f);
1225                         lr->f = NULL;
1226                 }
1227         }
1228 }
1229
1230 void
1231 process_lines_free(struct line_reader *lr)
1232 {
1233         free(lr->buff);
1234         free(lr->pathname);
1235         free(lr);
1236 }
1237
1238 static void
1239 free_cache(struct name_cache *cache)
1240 {
1241         size_t i;
1242
1243         if (cache != NULL) {
1244                 for (i = 0; i < cache->size; i++)
1245                         free(cache->cache[i].name);
1246                 free(cache);
1247         }
1248 }
1249
1250 /*
1251  * Lookup uname/gname from uid/gid, return NULL if no match.
1252  */
1253 static const char *
1254 lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable,
1255     int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id)
1256 {
1257         char asnum[16];
1258         struct name_cache       *cache;
1259         const char *name;
1260         int slot;
1261
1262
1263         if (*name_cache_variable == NULL) {
1264                 *name_cache_variable = malloc(sizeof(struct name_cache));
1265                 if (*name_cache_variable == NULL)
1266                         cpio_errc(1, ENOMEM, "No more memory");
1267                 memset(*name_cache_variable, 0, sizeof(struct name_cache));
1268                 (*name_cache_variable)->size = name_cache_size;
1269         }
1270
1271         cache = *name_cache_variable;
1272         cache->probes++;
1273
1274         slot = id % cache->size;
1275         if (cache->cache[slot].name != NULL) {
1276                 if (cache->cache[slot].id == id) {
1277                         cache->hits++;
1278                         return (cache->cache[slot].name);
1279                 }
1280                 free(cache->cache[slot].name);
1281                 cache->cache[slot].name = NULL;
1282         }
1283
1284         if (lookup_fn(cpio, &name, id) == 0) {
1285                 if (name == NULL || name[0] == '\0') {
1286                         /* If lookup failed, format it as a number. */
1287                         snprintf(asnum, sizeof(asnum), "%u", (unsigned)id);
1288                         name = asnum;
1289                 }
1290                 cache->cache[slot].name = strdup(name);
1291                 if (cache->cache[slot].name != NULL) {
1292                         cache->cache[slot].id = id;
1293                         return (cache->cache[slot].name);
1294                 }
1295                 /*
1296                  * Conveniently, NULL marks an empty slot, so
1297                  * if the strdup() fails, we've just failed to
1298                  * cache it.  No recovery necessary.
1299                  */
1300         }
1301         return (NULL);
1302 }
1303
1304 static const char *
1305 lookup_uname(struct cpio *cpio, uid_t uid)
1306 {
1307         return (lookup_name(cpio, &cpio->uname_cache,
1308                     &lookup_uname_helper, (id_t)uid));
1309 }
1310
1311 static int
1312 lookup_uname_helper(struct cpio *cpio, const char **name, id_t id)
1313 {
1314         struct passwd   *pwent;
1315
1316         (void)cpio; /* UNUSED */
1317
1318         errno = 0;
1319         pwent = getpwuid((uid_t)id);
1320         if (pwent == NULL) {
1321                 *name = NULL;
1322                 if (errno != 0)
1323                         cpio_warnc(errno, "getpwuid(%d) failed", id);
1324                 return (errno);
1325         }
1326
1327         *name = pwent->pw_name;
1328         return (0);
1329 }
1330
1331 static const char *
1332 lookup_gname(struct cpio *cpio, gid_t gid)
1333 {
1334         return (lookup_name(cpio, &cpio->gname_cache,
1335                     &lookup_gname_helper, (id_t)gid));
1336 }
1337
1338 static int
1339 lookup_gname_helper(struct cpio *cpio, const char **name, id_t id)
1340 {
1341         struct group    *grent;
1342
1343         (void)cpio; /* UNUSED */
1344
1345         errno = 0;
1346         grent = getgrgid((gid_t)id);
1347         if (grent == NULL) {
1348                 *name = NULL;
1349                 if (errno != 0)
1350                         cpio_warnc(errno, "getgrgid(%d) failed", id);
1351                 return (errno);
1352         }
1353
1354         *name = grent->gr_name;
1355         return (0);
1356 }