xref: /freebsd/usr.bin/ar/ar.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1d192f3d3SKai Wang /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4d192f3d3SKai Wang  * Copyright (c) 2007 Kai Wang
5d192f3d3SKai Wang  * All rights reserved.
6d192f3d3SKai Wang  *
7d192f3d3SKai Wang  * Redistribution and use in source and binary forms, with or without
8d192f3d3SKai Wang  * modification, are permitted provided that the following conditions
9d192f3d3SKai Wang  * are met:
10d192f3d3SKai Wang  * 1. Redistributions of source code must retain the above copyright
1125f82d56SEd Maste  *    notice, this list of conditions and the following disclaimer.
12d192f3d3SKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
13d192f3d3SKai Wang  *    notice, this list of conditions and the following disclaimer in the
14d192f3d3SKai Wang  *    documentation and/or other materials provided with the distribution.
15d192f3d3SKai Wang  *
1625f82d56SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1725f82d56SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1825f82d56SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1925f82d56SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2025f82d56SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2125f82d56SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2225f82d56SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2325f82d56SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2425f82d56SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2525f82d56SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2625f82d56SEd Maste  * SUCH DAMAGE.
27d192f3d3SKai Wang  */
28d192f3d3SKai Wang 
290c099281SKai Wang #define	BSDAR_VERSION	"1.1.0"
30a856b6c5SDavid E. O'Brien 
31d192f3d3SKai Wang /*
32d192f3d3SKai Wang  * ar(1) options.
33d192f3d3SKai Wang  */
34d192f3d3SKai Wang #define AR_A	0x0001		/* position-after */
35d192f3d3SKai Wang #define AR_B	0x0002		/* position-before */
36d192f3d3SKai Wang #define AR_C	0x0004		/* creating new archive */
37d192f3d3SKai Wang #define AR_CC	0x0008		/* do not overwrite when extracting */
38d192f3d3SKai Wang #define AR_J	0x0010		/* bzip2 compression */
39d192f3d3SKai Wang #define AR_O	0x0020		/* preserve original mtime when extracting */
40d192f3d3SKai Wang #define AR_S	0x0040		/* write archive symbol table */
41d192f3d3SKai Wang #define AR_SS	0x0080		/* do not write archive symbol table */
42d192f3d3SKai Wang #define AR_TR	0x0100		/* only keep first 15 chars for member name */
43d192f3d3SKai Wang #define AR_U	0x0200		/* only extract or update newer members.*/
44d192f3d3SKai Wang #define AR_V	0x0400		/* verbose mode */
45d192f3d3SKai Wang #define AR_Z	0x0800		/* gzip compression */
460e479ac8STim Kientzle #define AR_D	0x1000		/* insert dummy mode, mtime, uid and gid */
47d192f3d3SKai Wang 
48d192f3d3SKai Wang #define DEF_BLKSZ 10240		/* default block size */
49d192f3d3SKai Wang 
50d192f3d3SKai Wang /*
51d192f3d3SKai Wang  * Convenient wrapper for general libarchive error handling.
52d192f3d3SKai Wang  */
53d192f3d3SKai Wang #define	AC(CALL) do {							\
54d192f3d3SKai Wang 	if ((CALL))							\
55086f0908SEd Maste 		bsdar_errc(bsdar, archive_errno(a), "%s",		\
56d192f3d3SKai Wang 		    archive_error_string(a));				\
57d192f3d3SKai Wang } while (0)
58d192f3d3SKai Wang 
59d192f3d3SKai Wang /*
60d192f3d3SKai Wang  * In-memory representation of archive member(object).
61d192f3d3SKai Wang  */
62d192f3d3SKai Wang struct ar_obj {
63d192f3d3SKai Wang 	char		 *name;		/* member name */
64d192f3d3SKai Wang 	void		 *maddr;	/* mmap start address */
65d192f3d3SKai Wang 	uid_t		  uid;		/* user id */
66d192f3d3SKai Wang 	gid_t		  gid;		/* group id */
67d192f3d3SKai Wang 	mode_t		  md;		/* octal file permissions */
68d192f3d3SKai Wang 	size_t		  size;		/* member size */
69d192f3d3SKai Wang 	time_t		  mtime;	/* modification time */
70d192f3d3SKai Wang 	int		  fd;		/* file descriptor */
71d192f3d3SKai Wang 	dev_t		  dev;		/* inode's device */
72d192f3d3SKai Wang 	ino_t		  ino;		/* inode's number */
73d192f3d3SKai Wang 
74d192f3d3SKai Wang 	TAILQ_ENTRY(ar_obj) objs;
75d192f3d3SKai Wang };
76d192f3d3SKai Wang 
77d192f3d3SKai Wang /*
78d192f3d3SKai Wang  * Structure encapsulates the "global" data for "ar" program.
79d192f3d3SKai Wang  */
80d192f3d3SKai Wang struct bsdar {
81d192f3d3SKai Wang 	const char	 *filename;	/* archive name. */
820c099281SKai Wang 	const char	 *addlib;	/* target of ADDLIB. */
83d192f3d3SKai Wang 	const char	 *posarg;	/* position arg for modifiers -a, -b. */
84d192f3d3SKai Wang 	char		  mode;		/* program mode */
85d192f3d3SKai Wang 	int		  options;	/* command line options */
86d192f3d3SKai Wang 
87d192f3d3SKai Wang 	const char	 *progname;	/* program name */
88d192f3d3SKai Wang 	int		  argc;
89d192f3d3SKai Wang 	char		**argv;
90d192f3d3SKai Wang 
91d192f3d3SKai Wang 	/*
92d192f3d3SKai Wang 	 * Fields for the archive string table.
93d192f3d3SKai Wang 	 */
94d192f3d3SKai Wang 	char		 *as;		/* buffer for archive string table. */
95d192f3d3SKai Wang 	size_t		  as_sz;	/* current size of as table. */
96d192f3d3SKai Wang 	size_t		  as_cap;	/* capacity of as table buffer. */
97d192f3d3SKai Wang 
98d192f3d3SKai Wang 	/*
99d192f3d3SKai Wang 	 * Fields for the archive symbol table.
100d192f3d3SKai Wang 	 */
101fabed6b2SEd Maste 	uint64_t	  s_cnt;	/* current number of symbols. */
102fabed6b2SEd Maste 	uint64_t	 *s_so;		/* symbol offset table. */
103fabed6b2SEd Maste 	uint64_t	  s_so_max;     /* maximum symbol offset. */
104d192f3d3SKai Wang 	size_t		  s_so_cap;	/* capacity of so table buffer. */
105fabed6b2SEd Maste 
106d192f3d3SKai Wang 	char		 *s_sn;		/* symbol name table */
107d192f3d3SKai Wang 	size_t		  s_sn_cap;	/* capacity of sn table buffer. */
108d192f3d3SKai Wang 	size_t		  s_sn_sz;	/* current size of sn table. */
109d192f3d3SKai Wang 	/* Current member's offset (relative to the end of pseudo members.) */
110d192f3d3SKai Wang 	off_t		  rela_off;
111d192f3d3SKai Wang 
112d192f3d3SKai Wang 	TAILQ_HEAD(, ar_obj) v_obj;	/* object(member) list */
113d192f3d3SKai Wang };
114d192f3d3SKai Wang 
115c5bf58adSEd Maste void	ar_mode_script(struct bsdar *ar);
11641e6398fSMark Johnston int	ar_read_archive(struct bsdar *ar, int mode, FILE *out);
11757aaefdcSEd Maste int	ar_write_archive(struct bsdar *ar, int mode);
11857aaefdcSEd Maste void	bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2;
11957aaefdcSEd Maste void	bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...);
120