1176434Skaiw/*-
2176434Skaiw * Copyright (c) 2007 Kai Wang
3176434Skaiw * All rights reserved.
4176434Skaiw *
5176434Skaiw * Redistribution and use in source and binary forms, with or without
6176434Skaiw * modification, are permitted provided that the following conditions
7176434Skaiw * are met:
8176434Skaiw * 1. Redistributions of source code must retain the above copyright
9176434Skaiw *    notice, this list of conditions and the following disclaimer
10176434Skaiw *    in this position and unchanged.
11176434Skaiw * 2. Redistributions in binary form must reproduce the above copyright
12176434Skaiw *    notice, this list of conditions and the following disclaimer in the
13176434Skaiw *    documentation and/or other materials provided with the distribution.
14176434Skaiw *
15176434Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16176434Skaiw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17176434Skaiw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18176434Skaiw * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19176434Skaiw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20176434Skaiw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21176434Skaiw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22176434Skaiw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23176434Skaiw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24176434Skaiw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25176434Skaiw *
26176434Skaiw * $FreeBSD$
27176434Skaiw */
28176434Skaiw
29183218Skaiw#define	BSDAR_VERSION	"1.1.0"
30176453Sobrien
31176434Skaiw/*
32176434Skaiw * ar(1) options.
33176434Skaiw */
34176434Skaiw#define AR_A	0x0001		/* position-after */
35176434Skaiw#define AR_B	0x0002		/* position-before */
36176434Skaiw#define AR_C	0x0004		/* creating new archive */
37176434Skaiw#define AR_CC	0x0008		/* do not overwrite when extracting */
38176434Skaiw#define AR_J	0x0010		/* bzip2 compression */
39176434Skaiw#define AR_O	0x0020		/* preserve original mtime when extracting */
40176434Skaiw#define AR_S	0x0040		/* write archive symbol table */
41176434Skaiw#define AR_SS	0x0080		/* do not write archive symbol table */
42176434Skaiw#define AR_TR	0x0100		/* only keep first 15 chars for member name */
43176434Skaiw#define AR_U	0x0200		/* only extract or update newer members.*/
44176434Skaiw#define AR_V	0x0400		/* verbose mode */
45176434Skaiw#define AR_Z	0x0800		/* gzip compression */
46213643Skientzle#define AR_D	0x1000		/* insert dummy mode, mtime, uid and gid */
47176434Skaiw
48176434Skaiw#define DEF_BLKSZ 10240		/* default block size */
49176434Skaiw
50176434Skaiw/*
51176434Skaiw * Convenient wrapper for general libarchive error handling.
52176434Skaiw */
53176434Skaiw#define	AC(CALL) do {					\
54176434Skaiw	if ((CALL))					\
55176434Skaiw		bsdar_errc(bsdar, EX_SOFTWARE, 0, "%s",	\
56176434Skaiw		    archive_error_string(a));		\
57176434Skaiw} while (0)
58183218Skaiw
59176434Skaiw/*
60176434Skaiw * In-memory representation of archive member(object).
61176434Skaiw */
62176434Skaiwstruct ar_obj {
63176434Skaiw	char		 *name;		/* member name */
64176434Skaiw	void		 *maddr;	/* mmap start address */
65176434Skaiw	uid_t		  uid;		/* user id */
66176434Skaiw	gid_t		  gid;		/* group id */
67176434Skaiw	mode_t		  md;		/* octal file permissions */
68176434Skaiw	size_t		  size;		/* member size */
69176434Skaiw	time_t		  mtime;	/* modification time */
70176434Skaiw	int		  fd;		/* file descriptor */
71176434Skaiw	dev_t		  dev;		/* inode's device */
72176434Skaiw	ino_t		  ino;		/* inode's number */
73176434Skaiw
74176434Skaiw	TAILQ_ENTRY(ar_obj) objs;
75176434Skaiw};
76176434Skaiw
77176434Skaiw/*
78183218Skaiw * Structure encapsulates the "global" data for "ar" program.
79176434Skaiw */
80176434Skaiwstruct bsdar {
81176434Skaiw	const char	 *filename;	/* archive name. */
82183218Skaiw	const char	 *addlib;	/* target of ADDLIB. */
83176434Skaiw	const char	 *posarg;	/* position arg for modifiers -a, -b. */
84176434Skaiw	char		  mode;		/* program mode */
85176434Skaiw	int		  options;	/* command line options */
86176434Skaiw
87176434Skaiw	const char	 *progname;	/* program name */
88176434Skaiw	int		  argc;
89176434Skaiw	char		**argv;
90176434Skaiw
91176434Skaiw	/*
92176434Skaiw	 * Fields for the archive string table.
93176434Skaiw	 */
94176434Skaiw	char		 *as;		/* buffer for archive string table. */
95176434Skaiw	size_t		  as_sz;	/* current size of as table. */
96176434Skaiw	size_t		  as_cap;	/* capacity of as table buffer. */
97176434Skaiw
98176434Skaiw	/*
99176434Skaiw	 * Fields for the archive symbol table.
100176434Skaiw	 */
101176434Skaiw	uint32_t	  s_cnt;	/* current number of symbols. */
102176434Skaiw	uint32_t	 *s_so;		/* symbol offset table. */
103176434Skaiw	size_t		  s_so_cap;	/* capacity of so table buffer. */
104176434Skaiw	char		 *s_sn;		/* symbol name table */
105176434Skaiw	size_t		  s_sn_cap;	/* capacity of sn table buffer. */
106176434Skaiw	size_t		  s_sn_sz;	/* current size of sn table. */
107176434Skaiw	/* Current member's offset (relative to the end of pseudo members.) */
108176434Skaiw	off_t		  rela_off;
109176434Skaiw
110176434Skaiw	TAILQ_HEAD(, ar_obj) v_obj;	/* object(member) list */
111176434Skaiw};
112176434Skaiw
113176434Skaiwvoid	bsdar_errc(struct bsdar *, int _eval, int _code,
114241827Seadler	    const char *fmt, ...) __dead2;
115176434Skaiwvoid	bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...);
116176434Skaiwvoid	ar_mode_d(struct bsdar *bsdar);
117176434Skaiwvoid	ar_mode_m(struct bsdar *bsdar);
118176434Skaiwvoid	ar_mode_p(struct bsdar *bsdar);
119177064Skaiwvoid	ar_mode_q(struct bsdar *bsdar);
120176434Skaiwvoid	ar_mode_r(struct bsdar *bsdar);
121176434Skaiwvoid	ar_mode_s(struct bsdar *bsdar);
122176434Skaiwvoid	ar_mode_t(struct bsdar *bsdar);
123176434Skaiwvoid	ar_mode_x(struct bsdar *bsdar);
124183218Skaiwvoid	ar_mode_A(struct bsdar *bsdar);
125183218Skaiwvoid	ar_mode_script(struct bsdar *ar);
126