xref: /plan9/sys/src/ape/cmd/pax/pax.h (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 /* $Source: /u/mark/src/pax/RCS/pax.h,v $
2  *
3  * $Revision: 1.2 $
4  *
5  * pax.h - defnitions for entire program
6  *
7  * DESCRIPTION
8  *
9  *	This file contains most all of the definitions required by the PAX
10  *	software.  This header is included in every source file.
11  *
12  * AUTHOR
13  *
14  *     Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
15  *
16  * Sponsored by The USENIX Association for public distribution.
17  *
18  * Copyright (c) 1989 Mark H. Colburn.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that the above copyright notice and this paragraph are
23  * duplicated in all such forms and that any documentation,
24  * advertising materials, and other materials related to such
25  * distribution and use acknowledge that the software was developed
26  * by Mark H. Colburn and sponsored by The USENIX Association.
27  *
28  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31  */
32 
33 #ifndef _PAX_H
34 #define _PAX_H
35 
36 /* Headers */
37 
38 #include "config.h"
39 #include "limits.h"
40 #include <stdio.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <ctype.h>
44 #include <sys/types.h>
45 #ifndef _POSIX_SOURCE
46 #include <sys/ioctl.h>
47 #endif
48 #include <sys/stat.h>
49 #include "regexp.h"
50 
51 #if defined(DIRENT) || defined(_POSIX_SOURCE)
52 # ifdef PAXDIR
53 #  include "paxdir.h"
54 # else
55 #  include <dirent.h>
56 # endif
57 #else
58 # ifdef hpux
59 #  include <ndir.h>
60 # else
61 #  ifdef XENIX_286
62 #   include <sys/ndir.h>
63 #  else XENIX_286
64 #   include <sys/dir.h>
65 #  endif XENIX_286
66 # endif /* hpux */
67 # define dirent direct
68 #endif
69 
70 #ifdef _POSIX_SOURCE
71 #define major(x)	0
72 #define minor(x)	0
73 #else
74 #ifndef	major
75 #   include <sys/sysmacros.h>
76 #endif				/* major */
77 #endif
78 
79 #ifdef	SYSTIME
80 #   include <sys/time.h>
81 #else				/* SYSTIME */
82 #   include <time.h>
83 #endif				/* SYSTIME */
84 
85 #ifndef V7
86 #   include <fcntl.h>
87 #endif
88 
89 #ifdef XENIX
90 #   include <sys/inode.h>
91 #endif
92 #ifdef XENIX_286
93 #include <sys/param.h>
94 #endif XENIX_286
95 
96 #include <pwd.h>
97 #include <grp.h>
98 #ifndef XENIX_286
99 #ifndef _POSIX_SOURCE
100 #include <sys/file.h>
101 #endif
102 #endif /* XENIX_286 */
103 
104 #ifdef _POSIX_SOURCE
105 #include <unistd.h>
106 #include <stdlib.h>
107 #endif
108 
109 /* Defines */
110 
111 #define	STDIN	0		/* Standard input  file descriptor */
112 #define	STDOUT	1		/* Standard output file descriptor */
113 
114 /*
115  * Open modes; there is no <fcntl.h> with v7 UNIX and other versions of
116  * UNIX may not have all of these defined...
117  */
118 
119 #ifndef O_RDONLY
120 #   define	O_RDONLY	0
121 #endif
122 
123 #ifndef O_WRONLY
124 #   define	O_WRONLY	1
125 #endif
126 
127 #ifndef O_RDWR
128 #   define	O_WRONLY	2
129 #endif
130 
131 #ifndef	O_BINARY
132 #   define	O_BINARY	0
133 #endif
134 
135 #ifndef NULL
136 #   define 	NULL 		0
137 #endif
138 
139 #define TMAGIC		"ustar"		/* ustar and a null */
140 #define TMAGLEN		6
141 #define TVERSION	"00"		/* 00 and no null */
142 #define TVERSLEN	2
143 
144 /* Values used in typeflag field */
145 #define REGTYPE		'0'		/* Regular File */
146 #define AREGTYPE	'\0'		/* Regular File */
147 #define LNKTYPE		'1'		/* Link */
148 #define SYMTYPE		'2'		/* Reserved */
149 #define CHRTYPE		'3'		/* Character Special File */
150 #define BLKTYPE		'4'		/* Block Special File */
151 #define DIRTYPE		'5'		/* Directory */
152 #define FIFOTYPE	'6'		/* FIFO */
153 #define CONTTYPE	'7'		/* Reserved */
154 
155 #define BLOCKSIZE	512	/* all output is padded to 512 bytes */
156 #define	uint	unsigned int	/* Not always in types.h */
157 #define	ushort	unsigned short	/* Not always in types.h */
158 #define	BLOCK	5120		/* Default archive block size */
159 #define	H_COUNT	10		/* Number of items in ASCII header */
160 #define	H_PRINT	"%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo"
161 #define	H_SCAN	"%6ho%6ho%6ho%6ho%6ho%6ho%6ho%11lo%6o%11lo"
162 #define	H_STRLEN 70		/* ASCII header string length */
163 #define	M_ASCII "070707"	/* ASCII magic number */
164 #define	M_BINARY 070707		/* Binary magic number */
165 #define	M_STRLEN 6		/* ASCII magic number length */
166 #define	PATHELEM 256		/* Pathname element count limit */
167 #define	S_IFSHF	12		/* File type shift (shb in stat.h) */
168 #define	S_IPERM	07777		/* File permission bits (shb in stat.h) */
169 #define	S_IPEXE	07000		/* Special execution bits (shb in stat.h) */
170 #define	S_IPOPN	0777		/* Open access bits (shb in stat.h) */
171 
172 #ifdef _POSIX_SOURCE
173 /* hack: depend on knowing bits out of stat.h for S_ISREG, etc. */
174 #define S_IFMT	0170000
175 #define S_IFREG	0100000
176 #define S_IFDIR	0040000
177 #define S_IFCHR	0020000
178 #define S_IFBLK 0060000
179 #define S_IFIFO 0010000
180 #endif
181 
182 /*
183  * Trailer pathnames. All must be of the same length.
184  */
185 #define	TRAILER	"TRAILER!!!"	/* Archive trailer (cpio compatible) */
186 #define	TRAILZ	11		/* Trailer pathname length (including null) */
187 
188 #include "port.h"
189 
190 
191 #define	TAR		1
192 #define	CPIO		2
193 #define	PAX		3
194 
195 #define AR_READ 	0
196 #define AR_WRITE 	1
197 #define AR_EXTRACT	2
198 #define AR_APPEND 	4
199 
200 /*
201  * Header block on tape.
202  */
203 #define	NAMSIZ		100
204 #define	PFIXSIZ		155
205 #define	TUNMLEN		32
206 #define	TGNMLEN		32
207 
208 /* The checksum field is filled with this while the checksum is computed. */
209 #define	CHKBLANKS	"        "	/* 8 blanks, no null */
210 
211 /*
212  * Exit codes from the "tar" program
213  */
214 #define	EX_SUCCESS	0	/* success! */
215 #define	EX_ARGSBAD	1	/* invalid args */
216 #define	EX_BADFILE	2	/* invalid filename */
217 #define	EX_BADARCH	3	/* bad archive */
218 #define	EX_SYSTEM	4	/* system gave unexpected error */
219 
220 #define	ROUNDUP(a,b) 	(((a) % (b)) == 0 ? (a) : ((a) + ((b) - ((a) % (b)))))
221 
222 /*
223  * Mininum value.
224  */
225 #define	MIN(a, b)	(((a) < (b)) ? (a) : (b))
226 
227 /*
228  * Remove a file or directory.
229  */
230 #ifdef _POSIX_SOURCE
231 #define	REMOVE(name, asb) \
232 	(S_ISDIR((asb)->sb_mode)? rmdir(name) : unlink(name))
233 #else
234 #define	REMOVE(name, asb) \
235 	(((asb)->sb_mode & S_IFMT) == S_IFDIR ? rmdir(name) : unlink(name))
236 #endif
237 
238 /*
239  * Cast and reduce to unsigned short.
240  */
241 #define	USH(n)		(((ushort) (n)) & 0177777)
242 
243 
244 /* Type Definitions */
245 
246 /*
247  * Binary archive header (obsolete).
248  */
249 typedef struct {
250     short           b_dev;	/* Device code */
251     ushort          b_ino;	/* Inode number */
252     ushort          b_mode;	/* Type and permissions */
253     ushort          b_uid;	/* Owner */
254     ushort          b_gid;	/* Group */
255     short           b_nlink;	/* Number of links */
256     short           b_rdev;	/* Real device */
257     ushort          b_mtime[2];	/* Modification time (hi/lo) */
258     ushort          b_name;	/* Length of pathname (with null) */
259     ushort          b_size[2];	/* Length of data */
260 } Binary;
261 
262 /*
263  * File status with symbolic links. Kludged to hold symbolic link pathname
264  * within structure.
265  */
266 typedef struct {
267     struct stat     sb_stat;
268     char            sb_link[PATH_MAX + 1];
269 } Stat;
270 
271 #define	STAT(name, asb)		stat(name, &(asb)->sb_stat)
272 #define	FSTAT(fd, asb)		fstat(fd, &(asb)->sb_stat)
273 
274 #define	sb_dev		sb_stat.st_dev
275 #define	sb_ino		sb_stat.st_ino
276 #define	sb_mode		sb_stat.st_mode
277 #define	sb_nlink	sb_stat.st_nlink
278 #define	sb_uid		sb_stat.st_uid
279 #define	sb_gid		sb_stat.st_gid
280 #define	sb_rdev		sb_stat.st_rdev
281 #define	sb_size		sb_stat.st_size
282 #define	sb_atime	sb_stat.st_atime
283 #define	sb_mtime	sb_stat.st_mtime
284 #define	sb_ctime	sb_stat.st_ctime
285 
286 #ifdef	S_IFLNK
287 #	define	LSTAT(name, asb)	lstat(name, &(asb)->sb_stat)
288 #	define	sb_blksize	sb_stat.st_blksize
289 #	define	sb_blocks	sb_stat.st_blocks
290 #else				/* S_IFLNK */
291 /*
292  * File status without symbolic links.
293  */
294 #	define	LSTAT(name, asb)	stat(name, &(asb)->sb_stat)
295 #endif				/* S_IFLNK */
296 
297 /*
298  * Hard link sources. One or more are chained from each link structure.
299  */
300 typedef struct name {
301     struct name    *p_forw;	/* Forward chain (terminated) */
302     struct name    *p_back;	/* Backward chain (circular) */
303     char           *p_name;	/* Pathname to link from */
304 } Path;
305 
306 /*
307  * File linking information. One entry exists for each unique file with with
308  * outstanding hard links.
309  */
310 typedef struct link {
311     struct link    *l_forw;	/* Forward chain (terminated) */
312     struct link    *l_back;	/* Backward chain (terminated) */
313     dev_t           l_dev;	/* Device */
314     ino_t           l_ino;	/* Inode */
315     ushort          l_nlink;	/* Unresolved link count */
316     OFFSET          l_size;	/* Length */
317     char	   *l_name;	/* pathname to link from */
318     Path           *l_path;	/* Pathname which link to l_name */
319 } Link;
320 
321 /*
322  * Structure for ed-style replacement strings (-s option).
323 */
324 typedef struct replstr {
325     regexp	   *comp;	/* compiled regular expression */
326     char	   *replace;	/* replacement string */
327     char	    print;	/* >0 if we are to print replacement */
328     char	    global;	/* >0 if we are to replace globally */
329     struct replstr *next;	/* pointer to next record */
330 } Replstr;
331 
332 
333 /*
334  * This has to be included here to insure that all of the type
335  * delcarations are declared for the prototypes.
336  */
337 #ifndef STRERROR
338 /* boofheads have a different interface than standard, so rename */
339 #define strerror xstrerror
340 #endif
341 #include "func.h"
342 
343 
344 #ifndef NO_EXTERN
345 /* Globally Available Identifiers */
346 
347 extern char    *ar_file;
348 extern char    *bufend;
349 extern char    *bufstart;
350 extern char    *bufidx;
351 extern char    *myname;
352 extern int      archivefd;
353 extern int      blocking;
354 extern uint     blocksize;
355 extern int      gid;
356 extern int      head_standard;
357 extern int      ar_interface;
358 extern int      ar_format;
359 extern int      mask;
360 extern int      ttyf;
361 extern int      uid;
362 extern OFFSET    total;
363 extern short    areof;
364 extern short    f_append;
365 extern short    f_create;
366 extern short    f_extract;
367 extern short    f_follow_links;
368 extern short    f_interactive;
369 extern short    f_linksleft;
370 extern short    f_list;
371 extern short    f_modified;
372 extern short    f_verbose;
373 extern short	f_link;
374 extern short	f_owner;
375 extern short	f_access_time;
376 extern short	f_pass;
377 extern short	f_pass;
378 extern short	f_disposition;
379 extern short    f_reverse_match;
380 extern short    f_mtime;
381 extern short    f_dir_create;
382 extern short    f_unconditional;
383 extern short    f_newer;
384 extern time_t   now;
385 extern uint     arvolume;
386 extern int	names_from_stdin;
387 extern Replstr *rplhead;
388 extern Replstr *rpltail;
389 extern char   **n_argv;
390 extern int      n_argc;
391 extern FILE    *msgfile;
392 #endif /* NO_EXTERN */
393 
394 extern char    *optarg;
395 extern int      optind;
396 #ifndef _POSIX_SOURCE
397 extern int      sys_nerr;
398 extern char    *sys_errlist[];
399 #endif
400 extern int      errno;
401 
402 #endif /* _PAX_H */
403