/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2007 Kai Wang4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#define BSDAR_VERSION "1.1.0"2930/*31* ar(1) options.32*/33#define AR_A 0x0001 /* position-after */34#define AR_B 0x0002 /* position-before */35#define AR_C 0x0004 /* creating new archive */36#define AR_CC 0x0008 /* do not overwrite when extracting */37#define AR_J 0x0010 /* bzip2 compression */38#define AR_O 0x0020 /* preserve original mtime when extracting */39#define AR_S 0x0040 /* write archive symbol table */40#define AR_SS 0x0080 /* do not write archive symbol table */41#define AR_TR 0x0100 /* only keep first 15 chars for member name */42#define AR_U 0x0200 /* only extract or update newer members.*/43#define AR_V 0x0400 /* verbose mode */44#define AR_Z 0x0800 /* gzip compression */45#define AR_D 0x1000 /* insert dummy mode, mtime, uid and gid */4647#define DEF_BLKSZ 10240 /* default block size */4849/*50* Convenient wrapper for general libarchive error handling.51*/52#define AC(CALL) do { \53if ((CALL)) \54bsdar_errc(bsdar, archive_errno(a), "%s", \55archive_error_string(a)); \56} while (0)5758/*59* In-memory representation of archive member(object).60*/61struct ar_obj {62char *name; /* member name */63void *maddr; /* mmap start address */64uid_t uid; /* user id */65gid_t gid; /* group id */66mode_t md; /* octal file permissions */67size_t size; /* member size */68time_t mtime; /* modification time */69int fd; /* file descriptor */70dev_t dev; /* inode's device */71ino_t ino; /* inode's number */7273TAILQ_ENTRY(ar_obj) objs;74};7576/*77* Structure encapsulates the "global" data for "ar" program.78*/79struct bsdar {80const char *filename; /* archive name. */81const char *addlib; /* target of ADDLIB. */82const char *posarg; /* position arg for modifiers -a, -b. */83char mode; /* program mode */84int options; /* command line options */8586const char *progname; /* program name */87int argc;88char **argv;8990/*91* Fields for the archive string table.92*/93char *as; /* buffer for archive string table. */94size_t as_sz; /* current size of as table. */95size_t as_cap; /* capacity of as table buffer. */9697/*98* Fields for the archive symbol table.99*/100uint64_t s_cnt; /* current number of symbols. */101uint64_t *s_so; /* symbol offset table. */102uint64_t s_so_max; /* maximum symbol offset. */103size_t s_so_cap; /* capacity of so table buffer. */104105char *s_sn; /* symbol name table */106size_t s_sn_cap; /* capacity of sn table buffer. */107size_t s_sn_sz; /* current size of sn table. */108/* Current member's offset (relative to the end of pseudo members.) */109off_t rela_off;110111TAILQ_HEAD(, ar_obj) v_obj; /* object(member) list */112};113114void ar_mode_script(struct bsdar *ar);115int ar_read_archive(struct bsdar *ar, int mode, FILE *out);116int ar_write_archive(struct bsdar *ar, int mode);117void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2;118void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...);119120121