diff --git a/usr.bin/ar/acpyacc.y b/usr.bin/ar/acpyacc.y index 34156e1d2d58..1ce7197ef222 100644 --- a/usr.bin/ar/acpyacc.y +++ b/usr.bin/ar/acpyacc.y @@ -1,659 +1,657 @@ %{ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ar.h" #define TEMPLATE "arscp.XXXXXXXX" struct list { char *str; struct list *next; }; extern int yylex(void); static void yyerror(const char *); static void arscp_addlib(char *archive, struct list *list); static void arscp_addmod(struct list *list); static void arscp_clear(void); static int arscp_copy(int ifd, int ofd); static void arscp_create(char *in, char *out); static void arscp_delete(struct list *list); static void arscp_dir(char *archive, struct list *list, char *rlt); static void arscp_end(int eval); static void arscp_extract(struct list *list); static void arscp_free_argv(void); static void arscp_free_mlist(struct list *list); static void arscp_list(void); static struct list *arscp_mlist(struct list *list, char *str); static void arscp_mlist2argv(struct list *list); static int arscp_mlist_len(struct list *list); static void arscp_open(char *fname); static void arscp_prompt(void); static void arscp_replace(struct list *list); static void arscp_save(void); static int arscp_target_exist(void); extern int lineno; static struct bsdar *bsdar; static char *target; static char *tmpac; static int interactive; static int verbose; %} %token ADDLIB %token ADDMOD %token CLEAR %token CREATE %token DELETE %token DIRECTORY %token END %token EXTRACT %token LIST %token OPEN %token REPLACE %token VERBOSE %token SAVE %token LP %token RP %token COMMA %token EOL %token FNAME %type mod_list %union { char *str; struct list *list; } %% begin : { arscp_prompt(); } ar_script ; ar_script : cmd_list | ; mod_list : FNAME { $$ = arscp_mlist(NULL, $1); } | mod_list separator FNAME { $$ = arscp_mlist($1, $3); } ; separator : COMMA | ; cmd_list : rawcmd | cmd_list rawcmd ; rawcmd : cmd EOL { arscp_prompt(); } ; cmd : addlib_cmd | addmod_cmd | clear_cmd | create_cmd | delete_cmd | directory_cmd | end_cmd | extract_cmd | list_cmd | open_cmd | replace_cmd | verbose_cmd | save_cmd | invalid_cmd | empty_cmd | error ; addlib_cmd : ADDLIB FNAME LP mod_list RP { arscp_addlib($2, $4); } | ADDLIB FNAME { arscp_addlib($2, NULL); } ; addmod_cmd : ADDMOD mod_list { arscp_addmod($2); } ; clear_cmd : CLEAR { arscp_clear(); } ; create_cmd : CREATE FNAME { arscp_create(NULL, $2); } ; delete_cmd : DELETE mod_list { arscp_delete($2); } ; directory_cmd : DIRECTORY FNAME { arscp_dir($2, NULL, NULL); } | DIRECTORY FNAME LP mod_list RP { arscp_dir($2, $4, NULL); } | DIRECTORY FNAME LP mod_list RP FNAME { arscp_dir($2, $4, $6); } ; end_cmd : END { arscp_end(EXIT_SUCCESS); } ; extract_cmd : EXTRACT mod_list { arscp_extract($2); } ; list_cmd : LIST { arscp_list(); } ; open_cmd : OPEN FNAME { arscp_open($2); } ; replace_cmd : REPLACE mod_list { arscp_replace($2); } ; save_cmd : SAVE { arscp_save(); } ; verbose_cmd : VERBOSE { verbose = !verbose; } ; empty_cmd : ; invalid_cmd : FNAME { yyerror(NULL); } ; %% /* ARGSUSED */ static void yyerror(const char *s) { (void) s; printf("Syntax error in archive script, line %d\n", lineno); } /* * arscp_open first open an archive and check its validity. If the archive * format is valid, it calls arscp_create to create a temporary copy of * the archive. */ static void arscp_open(char *fname) { struct archive *a; struct archive_entry *entry; int r; if ((a = archive_read_new()) == NULL) bsdar_errc(bsdar, 0, "archive_read_new failed"); archive_read_support_format_ar(a); AC(archive_read_open_filename(a, fname, DEF_BLKSZ)); if ((r = archive_read_next_header(a, &entry))) bsdar_warnc(bsdar, archive_errno(a), "%s", archive_error_string(a)); AC(archive_read_close(a)); AC(archive_read_free(a)); if (r != ARCHIVE_OK) return; arscp_create(fname, fname); } /* * Create archive. in != NULL indicate it's a OPEN cmd, and resulting * archive is based on modification of an existing one. If in == NULL, * we are in CREATE cmd and a new empty archive will be created. */ static void arscp_create(char *in, char *out) { struct archive *a; int ifd, ofd; /* Delete previously created temporary archive, if any. */ if (tmpac) { if (unlink(tmpac) < 0) bsdar_errc(bsdar, errno, "unlink failed"); free(tmpac); } tmpac = strdup(TEMPLATE); if (tmpac == NULL) bsdar_errc(bsdar, errno, "strdup failed"); if ((ofd = mkstemp(tmpac)) < 0) bsdar_errc(bsdar, errno, "mkstemp failed"); if (in) { /* * Command OPEN creates a temporary copy of the * input archive. */ if ((ifd = open(in, O_RDONLY)) < 0) { bsdar_warnc(bsdar, errno, "open failed"); return; } if (arscp_copy(ifd, ofd)) { bsdar_warnc(bsdar, 0, "arscp_copy failed"); return; } close(ifd); close(ofd); } else { /* * Command CREATE creates an "empty" archive. * (archive with only global header) */ if ((a = archive_write_new()) == NULL) bsdar_errc(bsdar, 0, "archive_write_new failed"); archive_write_set_format_ar_svr4(a); AC(archive_write_open_fd(a, ofd)); AC(archive_write_close(a)); AC(archive_write_free(a)); } /* Override previous target, if any. */ if (target) free(target); target = out; bsdar->filename = tmpac; } /* A file copying implementation using mmap. */ static int arscp_copy(int ifd, int ofd) { struct stat sb; char *buf, *p; ssize_t w; size_t bytes; if (fstat(ifd, &sb) < 0) { bsdar_warnc(bsdar, errno, "fstate failed"); return (1); } if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, (off_t)0)) == MAP_FAILED) { bsdar_warnc(bsdar, errno, "mmap failed"); return (1); } for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) { w = write(ofd, buf, bytes); if (w <= 0) { bsdar_warnc(bsdar, errno, "write failed"); break; } } if (munmap(p, sb.st_size) < 0) bsdar_errc(bsdar, errno, "munmap failed"); if (bytes > 0) return (1); return (0); } /* * Add all modules of archive to current archive, if list != NULL, * only those modules specified in 'list' will be added. */ static void arscp_addlib(char *archive, struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); bsdar->addlib = archive; ar_write_archive(bsdar, 'A'); arscp_free_argv(); arscp_free_mlist(list); } /* Add modules into current archive. */ static void arscp_addmod(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_write_archive(bsdar, 'q'); arscp_free_argv(); arscp_free_mlist(list); } /* Delete modules from current archive. */ static void arscp_delete(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_write_archive(bsdar, 'd'); arscp_free_argv(); arscp_free_mlist(list); } /* Extract modules from current archive. */ static void arscp_extract(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_read_archive(bsdar, 'x'); + ar_read_archive(bsdar, 'x', stdout); arscp_free_argv(); arscp_free_mlist(list); } /* List modules of archive. (Simple Mode) */ static void arscp_list(void) { if (!arscp_target_exist()) return; bsdar->argc = 0; bsdar->argv = NULL; /* Always verbose. */ bsdar->options |= AR_V; - ar_read_archive(bsdar, 't'); + ar_read_archive(bsdar, 't', stdout); bsdar->options &= ~AR_V; } /* List modules of archive. (Advance Mode) */ static void arscp_dir(char *archive, struct list *list, char *rlt) { FILE *out; /* If rlt != NULL, redirect output to it */ - out = NULL; + out = stdout; if (rlt) { - out = stdout; - if ((stdout = fopen(rlt, "w")) == NULL) + if ((out = fopen(rlt, "w")) == NULL) bsdar_errc(bsdar, errno, "fopen %s failed", rlt); } bsdar->filename = archive; if (list) arscp_mlist2argv(list); else { bsdar->argc = 0; bsdar->argv = NULL; } if (verbose) bsdar->options |= AR_V; - ar_read_archive(bsdar, 't'); + ar_read_archive(bsdar, 't', out); bsdar->options &= ~AR_V; if (rlt) { - if (fclose(stdout) == EOF) + if (fclose(out) == EOF) bsdar_errc(bsdar, errno, "fclose %s failed", rlt); - stdout = out; free(rlt); } free(archive); bsdar->filename = tmpac; arscp_free_argv(); arscp_free_mlist(list); } /* Replace modules of current archive. */ static void arscp_replace(struct list *list) { if (!arscp_target_exist()) return; arscp_mlist2argv(list); ar_write_archive(bsdar, 'r'); arscp_free_argv(); arscp_free_mlist(list); } /* Rename the temporary archive to the target archive. */ static void arscp_save(void) { mode_t mask; if (target) { if (rename(tmpac, target) < 0) bsdar_errc(bsdar, errno, "rename failed"); /* * mkstemp creates temp files with mode 0600, here we * set target archive mode per process umask. */ mask = umask(0); umask(mask); if (chmod(target, 0666 & ~mask) < 0) bsdar_errc(bsdar, errno, "chmod failed"); free(tmpac); free(target); tmpac = NULL; target= NULL; bsdar->filename = NULL; } else bsdar_warnc(bsdar, 0, "no open output archive"); } /* * Discard all the contents of current archive. This is achieved by * invoking CREATE cmd on current archive. */ static void arscp_clear(void) { char *new_target; if (target) { new_target = strdup(target); if (new_target == NULL) bsdar_errc(bsdar, errno, "strdup failed"); arscp_create(NULL, new_target); } } /* * Quit ar(1). Note that END cmd will not SAVE current archive * before exit. */ static void arscp_end(int eval) { if (target) free(target); if (tmpac) { if (unlink(tmpac) == -1) bsdar_errc(bsdar, errno, "unlink %s failed", tmpac); free(tmpac); } exit(eval); } /* * Check if target specified, i.e, whether OPEN or CREATE has been * issued by user. */ static int arscp_target_exist(void) { if (target) return (1); bsdar_warnc(bsdar, 0, "no open output archive"); return (0); } /* Construct module list. */ static struct list * arscp_mlist(struct list *list, char *str) { struct list *l; l = malloc(sizeof(*l)); if (l == NULL) bsdar_errc(bsdar, errno, "malloc failed"); l->str = str; l->next = list; return (l); } /* Calculate the length of a mlist. */ static int arscp_mlist_len(struct list *list) { int len; for(len = 0; list; list = list->next) len++; return (len); } /* Free the space allocated for mod_list. */ static void arscp_free_mlist(struct list *list) { struct list *l; /* Note that list->str was freed in arscp_free_argv. */ for(; list; list = l) { l = list->next; free(list); } } /* Convert mlist to argv array. */ static void arscp_mlist2argv(struct list *list) { char **argv; int i, n; n = arscp_mlist_len(list); argv = malloc(n * sizeof(*argv)); if (argv == NULL) bsdar_errc(bsdar, errno, "malloc failed"); /* Note that module names are stored in reverse order in mlist. */ for(i = n - 1; i >= 0; i--, list = list->next) { if (list == NULL) bsdar_errc(bsdar, errno, "invalid mlist"); argv[i] = list->str; } bsdar->argc = n; bsdar->argv = argv; } /* Free space allocated for argv array and its elements. */ static void arscp_free_argv(void) { int i; for(i = 0; i < bsdar->argc; i++) free(bsdar->argv[i]); free(bsdar->argv); } /* Show a prompt if we are in interactive mode */ static void arscp_prompt(void) { if (interactive) { printf("AR >"); fflush(stdout); } } /* Main function for ar script mode. */ void ar_mode_script(struct bsdar *ar) { bsdar = ar; interactive = isatty(fileno(stdin)); while(yyparse()) { if (!interactive) arscp_end(EXIT_FAILURE); } /* Script ends without END */ arscp_end(EXIT_SUCCESS); } diff --git a/usr.bin/ar/ar.c b/usr.bin/ar/ar.c index fb57fbe21e7f..17dd2a88d25b 100644 --- a/usr.bin/ar/ar.c +++ b/usr.bin/ar/ar.c @@ -1,409 +1,409 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 2007 Kai Wang * Copyright (c) 2007 Tim Kientzle * Copyright (c) 2007 Joseph Koshy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /*- * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Hugh Smith at The University of Guelph. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include "ar.h" enum options { OPTION_HELP }; static struct option longopts[] = { {"help", no_argument, NULL, OPTION_HELP}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; static void bsdar_usage(void); static void ranlib_usage(void); static void set_mode(struct bsdar *bsdar, char opt); static void only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes); static void bsdar_version(void); static void ranlib_version(void); int main(int argc, char **argv) { struct bsdar *bsdar, bsdar_storage; char *p; size_t len; int exitcode, i, opt, Dflag, Uflag; bsdar = &bsdar_storage; memset(bsdar, 0, sizeof(*bsdar)); exitcode = EXIT_SUCCESS; Dflag = 0; Uflag = 0; if ((bsdar->progname = getprogname()) == NULL) bsdar->progname = "ar"; /* Act like ranlib if our name ends in "ranlib"; this * accommodates arm-freebsd7.1-ranlib, bsdranlib, etc. */ len = strlen(bsdar->progname); if (len >= strlen("ranlib") && strcmp(bsdar->progname + len - strlen("ranlib"), "ranlib") == 0) { while ((opt = getopt_long(argc, argv, "tDUV", longopts, NULL)) != -1) { switch(opt) { case 't': /* Ignored. */ break; case 'D': Dflag = 1; Uflag = 0; break; case 'U': Uflag = 1; Dflag = 0; break; case 'V': ranlib_version(); break; case OPTION_HELP: ranlib_usage(); default: ranlib_usage(); } } argv += optind; argc -= optind; if (*argv == NULL) ranlib_usage(); /* Enable determinstic mode unless -U is set. */ if (Uflag == 0) bsdar->options |= AR_D; bsdar->options |= AR_S; while ((bsdar->filename = *argv++) != NULL) if (ar_write_archive(bsdar, 's')) exitcode = EXIT_FAILURE; exit(exitcode); } else { if (argc < 2) bsdar_usage(); if (*argv[1] != '-') { len = strlen(argv[1]) + 2; if ((p = malloc(len)) == NULL) bsdar_errc(bsdar, errno, "malloc failed"); *p = '-'; (void)strlcpy(p + 1, argv[1], len - 1); argv[1] = p; } } while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtUuVvxz", longopts, NULL)) != -1) { switch(opt) { case 'a': bsdar->options |= AR_A; break; case 'b': case 'i': bsdar->options |= AR_B; break; case 'C': bsdar->options |= AR_CC; break; case 'c': bsdar->options |= AR_C; break; case 'd': set_mode(bsdar, opt); break; case 'D': Dflag = 1; Uflag = 0; break; case 'f': bsdar->options |= AR_TR; break; case 'j': /* ignored */ break; case 'l': /* ignored, for GNU ar comptibility */ break; case 'M': set_mode(bsdar, opt); break; case 'm': set_mode(bsdar, opt); break; case 'o': bsdar->options |= AR_O; break; case 'p': set_mode(bsdar, opt); break; case 'q': set_mode(bsdar, opt); break; case 'r': set_mode(bsdar, opt); break; case 'S': bsdar->options |= AR_SS; break; case 's': bsdar->options |= AR_S; break; case 'T': /* ignored */ break; case 't': set_mode(bsdar, opt); break; case 'U': Uflag = 1; Dflag = 0; break; case 'u': bsdar->options |= AR_U; break; case 'V': bsdar_version(); break; case 'v': bsdar->options |= AR_V; break; case 'x': set_mode(bsdar, opt); break; case 'z': /* ignored */ break; case OPTION_HELP: bsdar_usage(); default: bsdar_usage(); } } argv += optind; argc -= optind; if (*argv == NULL && bsdar->mode != 'M') bsdar_usage(); if (bsdar->options & AR_A && bsdar->options & AR_B) bsdar_errc(bsdar, 0, "only one of -a and -[bi] options allowed"); if (bsdar->options & AR_J && bsdar->options & AR_Z) bsdar_errc(bsdar, 0, "only one of -j and -z options allowed"); if (bsdar->options & AR_S && bsdar->options & AR_SS) bsdar_errc(bsdar, 0, "only one of -s and -S options allowed"); if (bsdar->options & (AR_A | AR_B)) { if (*argv == NULL) bsdar_errc(bsdar, 0, "no position operand specified"); if ((bsdar->posarg = basename(*argv)) == NULL) bsdar_errc(bsdar, errno, "basename failed"); argc--; argv++; } /* Set determinstic mode for -D, and by default without -U. */ if (Dflag || (Uflag == 0 && (bsdar->mode == 'q' || bsdar->mode == 'r' || (bsdar->mode == '\0' && bsdar->options & AR_S)))) bsdar->options |= AR_D; if (bsdar->options & AR_A) only_mode(bsdar, "-a", "mqr"); if (bsdar->options & AR_B) only_mode(bsdar, "-b", "mqr"); if (bsdar->options & AR_C) only_mode(bsdar, "-c", "qr"); if (bsdar->options & AR_CC) only_mode(bsdar, "-C", "x"); if (Dflag) only_mode(bsdar, "-D", "qr"); if (Uflag) only_mode(bsdar, "-U", "qr"); if (bsdar->options & AR_O) only_mode(bsdar, "-o", "x"); if (bsdar->options & AR_SS) only_mode(bsdar, "-S", "mqr"); if (bsdar->options & AR_U) only_mode(bsdar, "-u", "qrx"); if (bsdar->mode == 'M') { ar_mode_script(bsdar); exit(EXIT_SUCCESS); } if ((bsdar->filename = *argv) == NULL) bsdar_usage(); bsdar->argc = --argc; bsdar->argv = ++argv; if ((!bsdar->mode || strchr("ptx", bsdar->mode)) && bsdar->options & AR_S) { exitcode = ar_write_archive(bsdar, 's'); if (!bsdar->mode) exit(exitcode); } switch(bsdar->mode) { case 'd': case 'm': case 'q': case 'r': exitcode = ar_write_archive(bsdar, bsdar->mode); break; case 'p': case 't': case 'x': - exitcode = ar_read_archive(bsdar, bsdar->mode); + exitcode = ar_read_archive(bsdar, bsdar->mode, stdout); break; default: bsdar_usage(); /* NOTREACHED */ } for (i = 0; i < bsdar->argc; i++) { if (bsdar->argv[i] != NULL) { bsdar_warnc(bsdar, 0, "%s: not found in archive", bsdar->argv[i]); exitcode = EXIT_FAILURE; } } exit(exitcode); } static void set_mode(struct bsdar *bsdar, char opt) { if (bsdar->mode != '\0' && bsdar->mode != opt) bsdar_errc(bsdar, 0, "Can't specify both -%c and -%c", opt, bsdar->mode); bsdar->mode = opt; } static void only_mode(struct bsdar *bsdar, const char *opt, const char *valid_modes) { if (strchr(valid_modes, bsdar->mode) == NULL) bsdar_errc(bsdar, 0, "Option %s is not permitted in mode -%c", opt, bsdar->mode); } static void bsdar_usage(void) { (void)fprintf(stderr, "usage: ar -d [-Tjsvz] archive file ...\n"); (void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n"); (void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n"); (void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n"); (void)fprintf(stderr, "\tar -q [-TcDjsUvz] archive file ...\n"); (void)fprintf(stderr, "\tar -r [-TcDjsUuvz] archive file ...\n"); (void)fprintf(stderr, "\tar -r [-TabcDijsUuvz] position archive file ...\n"); (void)fprintf(stderr, "\tar -s [-jz] archive\n"); (void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n"); (void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n"); (void)fprintf(stderr, "\tar -V\n"); exit(EXIT_FAILURE); } static void ranlib_usage(void) { (void)fprintf(stderr, "usage: ranlib [-DtU] archive ...\n"); (void)fprintf(stderr, "\tranlib -V\n"); exit(EXIT_FAILURE); } static void bsdar_version(void) { (void)printf("BSD ar %s - %s\n", BSDAR_VERSION, archive_version_string()); exit(EXIT_SUCCESS); } static void ranlib_version(void) { (void)printf("ranlib %s - %s\n", BSDAR_VERSION, archive_version_string()); exit(EXIT_SUCCESS); } diff --git a/usr.bin/ar/ar.h b/usr.bin/ar/ar.h index bcccf93a6016..66a7888c3e1c 100644 --- a/usr.bin/ar/ar.h +++ b/usr.bin/ar/ar.h @@ -1,121 +1,121 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2007 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #define BSDAR_VERSION "1.1.0" /* * ar(1) options. */ #define AR_A 0x0001 /* position-after */ #define AR_B 0x0002 /* position-before */ #define AR_C 0x0004 /* creating new archive */ #define AR_CC 0x0008 /* do not overwrite when extracting */ #define AR_J 0x0010 /* bzip2 compression */ #define AR_O 0x0020 /* preserve original mtime when extracting */ #define AR_S 0x0040 /* write archive symbol table */ #define AR_SS 0x0080 /* do not write archive symbol table */ #define AR_TR 0x0100 /* only keep first 15 chars for member name */ #define AR_U 0x0200 /* only extract or update newer members.*/ #define AR_V 0x0400 /* verbose mode */ #define AR_Z 0x0800 /* gzip compression */ #define AR_D 0x1000 /* insert dummy mode, mtime, uid and gid */ #define DEF_BLKSZ 10240 /* default block size */ /* * Convenient wrapper for general libarchive error handling. */ #define AC(CALL) do { \ if ((CALL)) \ bsdar_errc(bsdar, archive_errno(a), "%s", \ archive_error_string(a)); \ } while (0) /* * In-memory representation of archive member(object). */ struct ar_obj { char *name; /* member name */ void *maddr; /* mmap start address */ uid_t uid; /* user id */ gid_t gid; /* group id */ mode_t md; /* octal file permissions */ size_t size; /* member size */ time_t mtime; /* modification time */ int fd; /* file descriptor */ dev_t dev; /* inode's device */ ino_t ino; /* inode's number */ TAILQ_ENTRY(ar_obj) objs; }; /* * Structure encapsulates the "global" data for "ar" program. */ struct bsdar { const char *filename; /* archive name. */ const char *addlib; /* target of ADDLIB. */ const char *posarg; /* position arg for modifiers -a, -b. */ char mode; /* program mode */ int options; /* command line options */ const char *progname; /* program name */ int argc; char **argv; /* * Fields for the archive string table. */ char *as; /* buffer for archive string table. */ size_t as_sz; /* current size of as table. */ size_t as_cap; /* capacity of as table buffer. */ /* * Fields for the archive symbol table. */ uint64_t s_cnt; /* current number of symbols. */ uint64_t *s_so; /* symbol offset table. */ uint64_t s_so_max; /* maximum symbol offset. */ size_t s_so_cap; /* capacity of so table buffer. */ char *s_sn; /* symbol name table */ size_t s_sn_cap; /* capacity of sn table buffer. */ size_t s_sn_sz; /* current size of sn table. */ /* Current member's offset (relative to the end of pseudo members.) */ off_t rela_off; TAILQ_HEAD(, ar_obj) v_obj; /* object(member) list */ }; void ar_mode_script(struct bsdar *ar); -int ar_read_archive(struct bsdar *ar, int mode); +int ar_read_archive(struct bsdar *ar, int mode, FILE *out); int ar_write_archive(struct bsdar *ar, int mode); void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2; void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); diff --git a/usr.bin/ar/read.c b/usr.bin/ar/read.c index 81e0bfce1b7e..84b94a1f4d77 100644 --- a/usr.bin/ar/read.c +++ b/usr.bin/ar/read.c @@ -1,206 +1,206 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2007 Kai Wang * Copyright (c) 2007 Tim Kientzle * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include "ar.h" /* * Handle read modes: 'x', 't' and 'p'. */ int -ar_read_archive(struct bsdar *bsdar, int mode) +ar_read_archive(struct bsdar *bsdar, int mode, FILE *out) { struct archive *a; struct archive_entry *entry; struct stat sb; struct tm *tp; const char *bname; const char *name; mode_t md; size_t size; time_t mtime; uid_t uid; gid_t gid; char **av; char buf[25]; char find; int exitcode, flags, r, i; assert(mode == 'p' || mode == 't' || mode == 'x'); if ((a = archive_read_new()) == NULL) bsdar_errc(bsdar, 0, "archive_read_new failed"); archive_read_support_format_ar(a); AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ)); exitcode = EXIT_SUCCESS; for (;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || r == ARCHIVE_FATAL) bsdar_warnc(bsdar, archive_errno(a), "%s", archive_error_string(a)); if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL) break; if (r == ARCHIVE_RETRY) { bsdar_warnc(bsdar, 0, "Retrying..."); continue; } if ((name = archive_entry_pathname(entry)) == NULL) break; /* Skip pseudo members. */ if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 || strcmp(name, "/SYM64/") == 0) continue; if (bsdar->argc > 0) { find = 0; for(i = 0; i < bsdar->argc; i++) { av = &bsdar->argv[i]; if (*av == NULL) continue; if ((bname = basename(*av)) == NULL) bsdar_errc(bsdar, errno, "basename failed"); if (strcmp(bname, name) != 0) continue; *av = NULL; find = 1; break; } if (!find) continue; } if (mode == 't') { if (bsdar->options & AR_V) { md = archive_entry_mode(entry); uid = archive_entry_uid(entry); gid = archive_entry_gid(entry); size = archive_entry_size(entry); mtime = archive_entry_mtime(entry); (void)strmode(md, buf); - (void)fprintf(stdout, "%s %6d/%-6d %8ju ", + (void)fprintf(out, "%s %6d/%-6d %8ju ", buf + 1, uid, gid, (uintmax_t)size); tp = localtime(&mtime); (void)strftime(buf, sizeof(buf), "%b %e %H:%M %Y", tp); - (void)fprintf(stdout, "%s %s", buf, name); + (void)fprintf(out, "%s %s", buf, name); } else - (void)fprintf(stdout, "%s", name); + (void)fprintf(out, "%s", name); r = archive_read_data_skip(a); if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || r == ARCHIVE_FATAL) { - (void)fprintf(stdout, "\n"); + (void)fprintf(out, "\n"); bsdar_warnc(bsdar, archive_errno(a), "%s", archive_error_string(a)); } if (r == ARCHIVE_FATAL) break; - (void)fprintf(stdout, "\n"); + (void)fprintf(out, "\n"); } else { /* mode == 'x' || mode = 'p' */ if (mode == 'p') { if (bsdar->options & AR_V) { - (void)fprintf(stdout, "\n<%s>\n\n", + (void)fprintf(out, "\n<%s>\n\n", name); - fflush(stdout); + fflush(out); } r = archive_read_data_into_fd(a, 1); } else { /* mode == 'x' */ if (stat(name, &sb) != 0) { if (errno != ENOENT) { bsdar_warnc(bsdar, 0, "stat %s failed", bsdar->filename); continue; } } else { /* stat success, file exist */ if (bsdar->options & AR_CC) continue; if (bsdar->options & AR_U && archive_entry_mtime(entry) <= sb.st_mtime) continue; } if (bsdar->options & AR_V) - (void)fprintf(stdout, "x - %s\n", name); + (void)fprintf(out, "x - %s\n", name); /* Disallow absolute paths. */ if (name[0] == '/') { bsdar_warnc(bsdar, 0, "Absolute path '%s'", name); continue; } /* Basic path security flags. */ flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_SECURE_NODOTDOT; if (bsdar->options & AR_O) flags |= ARCHIVE_EXTRACT_TIME; r = archive_read_extract(a, entry, flags); } if (r) { bsdar_warnc(bsdar, archive_errno(a), "%s", archive_error_string(a)); exitcode = EXIT_FAILURE; } } } if (r == ARCHIVE_FATAL) exitcode = EXIT_FAILURE; AC(archive_read_close(a)); AC(archive_read_free(a)); return (exitcode); }