xref: /onnv-gate/usr/src/cmd/sgs/ar/common/cmd.c (revision 12792:1f56a791e275)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12792SAli.Bahrami@Oracle.COM  * Common Development and Distribution License (the "License").
6*12792SAli.Bahrami@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12792SAli.Bahrami@Oracle.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
270Sstevel@tonic-gate  *	  All Rights Reserved
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Incompatible Archive Header
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * The archive file member header used in SunOS 4.1 archive files and
350Sstevel@tonic-gate  * Solaris archive files are incompatible. The header file is:
360Sstevel@tonic-gate  * 	/usr/include/ar.h, struct ar_hdr.
370Sstevel@tonic-gate  * The member ar_name[] in Solaris comforms with Standard and the
380Sstevel@tonic-gate  * member name terminates with '/'. The SunOS's member does not terminate
390Sstevel@tonic-gate  * with '/' character. A bug 4046054 was filed:
400Sstevel@tonic-gate  * 	The ar command in Solaris 2.5.1 is incompatible with archives
410Sstevel@tonic-gate  * 	created on 4.x.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * To handle archive files created in SunOS 4.1 system on Solaris, the
440Sstevel@tonic-gate  * following changes were made:
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * 	1. file.c/writefile()
470Sstevel@tonic-gate  * 		Before writing each member files into the output
480Sstevel@tonic-gate  * 		archive file, ar_name[] is checked. If it is NULL,
490Sstevel@tonic-gate  * 		it means that the original archive header for this
500Sstevel@tonic-gate  * 		member was incompatible with Solaris format.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * 		The original Solaris ar command ended up having
530Sstevel@tonic-gate  * 		NULL name for the header. The change here uses the
540Sstevel@tonic-gate  * 		ar_rawname, which is much closer to the original
550Sstevel@tonic-gate  * 		name.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * 	2. cmd.c
580Sstevel@tonic-gate  * 		For the p command, the code used to use only ar_longname
590Sstevel@tonic-gate  * 		to seach the matching name. The member is set to NULL
600Sstevel@tonic-gate  * 		if the archive member header was incompatible.
610Sstevel@tonic-gate  * 		The ar_rawname is also used to find the matching member name.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * 		For commands to update the archive file, we do not
640Sstevel@tonic-gate  * 		use ar_rawname, and just use the ar_longname. The commands are
650Sstevel@tonic-gate  * 		r (replace), m (modify the position) and d (delete).
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include "inc.h"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
71*12792SAli.Bahrami@Oracle.COM  * Forward Declarations
720Sstevel@tonic-gate  */
73*12792SAli.Bahrami@Oracle.COM static void	ar_select(int *, unsigned long);
74*12792SAli.Bahrami@Oracle.COM static void	cleanup(Cmd_info *);
75*12792SAli.Bahrami@Oracle.COM static int	create_extract(ARFILE *, int, int, Cmd_info *);
76*12792SAli.Bahrami@Oracle.COM static char	*match(char *, Cmd_info *);
77*12792SAli.Bahrami@Oracle.COM static void	mesg(int, char *, Cmd_info *);
78*12792SAli.Bahrami@Oracle.COM static void	movefil(ARFILE *, struct stat *);
79*12792SAli.Bahrami@Oracle.COM static FILE	*stats(char *, struct stat *);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Commands
830Sstevel@tonic-gate  */
84*12792SAli.Bahrami@Oracle.COM void
rcmd(Cmd_info * cmd_info)850Sstevel@tonic-gate rcmd(Cmd_info *cmd_info)
860Sstevel@tonic-gate {
87*12792SAli.Bahrami@Oracle.COM 	FILE		*f;
88*12792SAli.Bahrami@Oracle.COM 	ARFILE		*fileptr;
89*12792SAli.Bahrami@Oracle.COM 	ARFILE		*abifile = NULL;
90*12792SAli.Bahrami@Oracle.COM 	ARFILE		*backptr = NULL;
91*12792SAli.Bahrami@Oracle.COM 	ARFILE		*endptr;
92*12792SAli.Bahrami@Oracle.COM 	ARFILE		*moved_files;
93*12792SAli.Bahrami@Oracle.COM 	ARFILE  	*prev_entry, *new_listhead, *new_listend;
94*12792SAli.Bahrami@Oracle.COM 	int		deleted;
95*12792SAli.Bahrami@Oracle.COM 	struct stat	stbuf;
96*12792SAli.Bahrami@Oracle.COM 	char		*gfile;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	new_listhead  = NULL;
990Sstevel@tonic-gate 	new_listend   = NULL;
1000Sstevel@tonic-gate 	prev_entry    = NULL;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	for (fileptr = getfile(cmd_info);
1030Sstevel@tonic-gate 	    fileptr; fileptr = getfile(cmd_info)) {
1040Sstevel@tonic-gate 		deleted = 0;
105*12792SAli.Bahrami@Oracle.COM 		if (!abifile && cmd_info->ponam &&
1060Sstevel@tonic-gate 		    strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
1070Sstevel@tonic-gate 			abifile = fileptr;
1080Sstevel@tonic-gate 		else if (!abifile)
1090Sstevel@tonic-gate 			backptr = fileptr;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
1120Sstevel@tonic-gate 		    (gfile = match(fileptr->ar_longname, cmd_info)) != NULL) {
1130Sstevel@tonic-gate 			/*
1140Sstevel@tonic-gate 			 * NOTE:
1150Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
1160Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
1170Sstevel@tonic-gate 			 */
1180Sstevel@tonic-gate 			f = stats(gfile, &stbuf); /* gfile is set by match */
1190Sstevel@tonic-gate 			if (f == NULL) {
120*12792SAli.Bahrami@Oracle.COM 				if (cmd_info->namc) {
121*12792SAli.Bahrami@Oracle.COM 					int err = errno;
122*12792SAli.Bahrami@Oracle.COM 					(void) fprintf(stderr,
123*12792SAli.Bahrami@Oracle.COM 					    MSG_INTL(MSG_SYS_OPEN),
124*12792SAli.Bahrami@Oracle.COM 					    gfile, strerror(err));
125*12792SAli.Bahrami@Oracle.COM 				}
1260Sstevel@tonic-gate 				/*
1270Sstevel@tonic-gate 				 * Created
1280Sstevel@tonic-gate 				 */
1290Sstevel@tonic-gate 				mesg('c', gfile, cmd_info);
1300Sstevel@tonic-gate 			} else {
131*12792SAli.Bahrami@Oracle.COM 				if ((cmd_info->opt_flgs & u_FLAG) &&
1320Sstevel@tonic-gate 				    stbuf.st_mtime <= fileptr->ar_date) {
1330Sstevel@tonic-gate 					(void) fclose(f);
1340Sstevel@tonic-gate 					continue;
1350Sstevel@tonic-gate 				}
1360Sstevel@tonic-gate 				/*
1370Sstevel@tonic-gate 				 * Replaced
1380Sstevel@tonic-gate 				 */
1390Sstevel@tonic-gate 				mesg('r', fileptr->ar_longname, cmd_info);
1400Sstevel@tonic-gate 				movefil(fileptr, &stbuf);
1410Sstevel@tonic-gate 				/*
1420Sstevel@tonic-gate 				 * Clear the previous contents.
1430Sstevel@tonic-gate 				 */
144*12792SAli.Bahrami@Oracle.COM 				if (fileptr->ar_flag & F_ELFRAW) {
1450Sstevel@tonic-gate 					/*
1460Sstevel@tonic-gate 					 * clear ar_elf
1470Sstevel@tonic-gate 					 */
1480Sstevel@tonic-gate 					(void) elf_end(fileptr->ar_elf);
1490Sstevel@tonic-gate 					fileptr->ar_elf = 0;
1500Sstevel@tonic-gate 				}
1510Sstevel@tonic-gate 				/* clear 'ar_flag' */
152*12792SAli.Bahrami@Oracle.COM 				fileptr->ar_flag &= ~F_ELFRAW;
153*12792SAli.Bahrami@Oracle.COM 
154*12792SAli.Bahrami@Oracle.COM 				/*
155*12792SAli.Bahrami@Oracle.COM 				 * Defer reading contents until needed, and
156*12792SAli.Bahrami@Oracle.COM 				 * then use an in-kernel file-to-file transfer
157*12792SAli.Bahrami@Oracle.COM 				 * to avoid excessive in-process memory use.
158*12792SAli.Bahrami@Oracle.COM 				 */
159*12792SAli.Bahrami@Oracle.COM 				fileptr->ar_contents = NULL;
160*12792SAli.Bahrami@Oracle.COM 
1610Sstevel@tonic-gate 				if (fileptr->ar_pathname != NULL)
1620Sstevel@tonic-gate 					free(fileptr->ar_pathname);
1630Sstevel@tonic-gate 				if ((fileptr->ar_pathname =
1640Sstevel@tonic-gate 				    malloc(strlen(gfile) + 1)) == NULL) {
165*12792SAli.Bahrami@Oracle.COM 					int err = errno;
166*12792SAli.Bahrami@Oracle.COM 					(void) fprintf(stderr,
167*12792SAli.Bahrami@Oracle.COM 					    MSG_INTL(MSG_MALLOC),
168*12792SAli.Bahrami@Oracle.COM 					    strerror(err));
1690Sstevel@tonic-gate 					exit(1);
1700Sstevel@tonic-gate 				}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 				(void) strcpy(fileptr->ar_pathname, gfile);
1730Sstevel@tonic-gate 				(void) fclose(f);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 				if (cmd_info->ponam && (abifile != fileptr)) {
1760Sstevel@tonic-gate 					deleted = 1;
1770Sstevel@tonic-gate 					/* remove from archive list */
1780Sstevel@tonic-gate 					if (prev_entry != NULL)
1790Sstevel@tonic-gate 						prev_entry->ar_next = NULL;
1800Sstevel@tonic-gate 					else
1810Sstevel@tonic-gate 						listhead = NULL;
1820Sstevel@tonic-gate 					listend = prev_entry;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 					/* add to moved list */
1850Sstevel@tonic-gate 					if (new_listhead == NULL)
1860Sstevel@tonic-gate 						new_listhead = fileptr;
1870Sstevel@tonic-gate 					else
1880Sstevel@tonic-gate 						new_listend->ar_next = fileptr;
1890Sstevel@tonic-gate 					new_listend = fileptr;
1900Sstevel@tonic-gate 				}
1910Sstevel@tonic-gate 				cmd_info->modified++;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		else
1950Sstevel@tonic-gate 			/*
1960Sstevel@tonic-gate 			 * Unchaged
1970Sstevel@tonic-gate 			 */
1980Sstevel@tonic-gate 			mesg('u', fileptr->ar_longname, cmd_info);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if (deleted)
2010Sstevel@tonic-gate 			deleted = 0;
2020Sstevel@tonic-gate 		else
2030Sstevel@tonic-gate 			prev_entry = fileptr;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	endptr = listend;
2070Sstevel@tonic-gate 	cleanup(cmd_info);
2080Sstevel@tonic-gate 	if (cmd_info->ponam && endptr &&
2090Sstevel@tonic-gate 	    (((moved_files = endptr->ar_next) != NULL) || new_listhead)) {
2100Sstevel@tonic-gate 		if (!abifile) {
211*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
212*12792SAli.Bahrami@Oracle.COM 			    cmd_info->ponam);
2130Sstevel@tonic-gate 			exit(2);
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		endptr->ar_next = NULL;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		/*
2180Sstevel@tonic-gate 		 * link new/moved files into archive entry list...
2190Sstevel@tonic-gate 		 * 1: prepend newlist to moved/appended list
2200Sstevel@tonic-gate 		 */
2210Sstevel@tonic-gate 		if (new_listhead) {
2220Sstevel@tonic-gate 			if (!moved_files)
2230Sstevel@tonic-gate 				listend = new_listend;
2240Sstevel@tonic-gate 			new_listend->ar_next = moved_files;
2250Sstevel@tonic-gate 			moved_files = new_listhead;
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 		/* 2: insert at appropriate position... */
228*12792SAli.Bahrami@Oracle.COM 		if (cmd_info->opt_flgs & b_FLAG)
2290Sstevel@tonic-gate 			abifile = backptr;
2300Sstevel@tonic-gate 		if (abifile) {
2310Sstevel@tonic-gate 			listend->ar_next = abifile->ar_next;
2320Sstevel@tonic-gate 			abifile->ar_next = moved_files;
2330Sstevel@tonic-gate 		} else {
2340Sstevel@tonic-gate 			listend->ar_next = listhead;
2350Sstevel@tonic-gate 			listhead = moved_files;
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 		listend = endptr;
2380Sstevel@tonic-gate 	} else if (cmd_info->ponam && !abifile)
239*12792SAli.Bahrami@Oracle.COM 		(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
240*12792SAli.Bahrami@Oracle.COM 		    cmd_info->ponam);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
243*12792SAli.Bahrami@Oracle.COM void
dcmd(Cmd_info * cmd_info)2440Sstevel@tonic-gate dcmd(Cmd_info *cmd_info)
2450Sstevel@tonic-gate {
246372Smike_s 	ARFILE	*fptr;
247372Smike_s 	ARFILE *backptr = NULL;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info)) {
2500Sstevel@tonic-gate 		if (match(fptr->ar_longname, cmd_info) != NULL) {
2510Sstevel@tonic-gate 			/*
2520Sstevel@tonic-gate 			 * NOTE:
2530Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
2540Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
2550Sstevel@tonic-gate 			 */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 			/*
2580Sstevel@tonic-gate 			 * Deleted
2590Sstevel@tonic-gate 			 */
2600Sstevel@tonic-gate 			mesg('d', fptr->ar_longname, cmd_info);
2610Sstevel@tonic-gate 			if (backptr == NULL) {
2620Sstevel@tonic-gate 				listhead = NULL;
2630Sstevel@tonic-gate 				listend = NULL;
2640Sstevel@tonic-gate 			} else {
2650Sstevel@tonic-gate 				backptr->ar_next = NULL;
2660Sstevel@tonic-gate 				listend = backptr;
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 			cmd_info->modified = 1;
2690Sstevel@tonic-gate 		} else {
2700Sstevel@tonic-gate 			/*
2710Sstevel@tonic-gate 			 * Unchaged
2720Sstevel@tonic-gate 			 */
2730Sstevel@tonic-gate 			mesg('u', fptr->ar_longname, cmd_info);
2740Sstevel@tonic-gate 			backptr = fptr;
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
279*12792SAli.Bahrami@Oracle.COM void
xcmd(Cmd_info * cmd_info)2800Sstevel@tonic-gate xcmd(Cmd_info *cmd_info)
2810Sstevel@tonic-gate {
282*12792SAli.Bahrami@Oracle.COM 	int	f;
283*12792SAli.Bahrami@Oracle.COM 	ARFILE	*next;
284*12792SAli.Bahrami@Oracle.COM 	int	rawname = 0;
285*12792SAli.Bahrami@Oracle.COM 	long	f_len = 0;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * If -T is specified, get the maximum file name length.
2890Sstevel@tonic-gate 	 */
290*12792SAli.Bahrami@Oracle.COM 	if (cmd_info->opt_flgs & T_FLAG) {
291*12792SAli.Bahrami@Oracle.COM 		f_len = pathconf(MSG_ORIG(MSG_STR_PERIOD), _PC_NAME_MAX);
2920Sstevel@tonic-gate 		if (f_len == -1) {
293*12792SAli.Bahrami@Oracle.COM 			int err = errno;
294*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stderr, MSG_INTL(MSG_PATHCONF),
295*12792SAli.Bahrami@Oracle.COM 			    strerror(err));
2960Sstevel@tonic-gate 			exit(1);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
3000Sstevel@tonic-gate 		if ((next->ar_longname[0] == 0) && (next->ar_rawname[0] != 0))
3010Sstevel@tonic-gate 			rawname = 1;
3020Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
3030Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
3040Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * NOTE:
3070Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
3080Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
3090Sstevel@tonic-gate 			 */
3100Sstevel@tonic-gate 			f = create_extract(next, rawname, f_len, cmd_info);
3110Sstevel@tonic-gate 			if (f >= 0) {
3120Sstevel@tonic-gate 				if (rawname) {
3130Sstevel@tonic-gate 					/*
3140Sstevel@tonic-gate 					 * eXtracted
3150Sstevel@tonic-gate 					 */
3160Sstevel@tonic-gate 					mesg('x', next->ar_rawname, cmd_info);
3170Sstevel@tonic-gate 					if (write(f, next->ar_contents,
3180Sstevel@tonic-gate 					    (unsigned)next->ar_size) !=
3190Sstevel@tonic-gate 					    next->ar_size) {
320*12792SAli.Bahrami@Oracle.COM 						int err = errno;
321*12792SAli.Bahrami@Oracle.COM 						(void) fprintf(stderr,
322*12792SAli.Bahrami@Oracle.COM 						    MSG_INTL(MSG_SYS_WRITE),
323*12792SAli.Bahrami@Oracle.COM 						    next->ar_rawname,
324*12792SAli.Bahrami@Oracle.COM 						    strerror(err));
3250Sstevel@tonic-gate 						exit(1);
3260Sstevel@tonic-gate 					}
3270Sstevel@tonic-gate 				} else {
3280Sstevel@tonic-gate 					/*
3290Sstevel@tonic-gate 					 * eXtracted
3300Sstevel@tonic-gate 					 */
3310Sstevel@tonic-gate 					mesg('x', next->ar_longname, cmd_info);
3320Sstevel@tonic-gate 					if (write(f, next->ar_contents,
3330Sstevel@tonic-gate 					    (unsigned)next->ar_size) !=
3340Sstevel@tonic-gate 					    next->ar_size) {
335*12792SAli.Bahrami@Oracle.COM 						int err = errno;
336*12792SAli.Bahrami@Oracle.COM 						(void) fprintf(stderr,
337*12792SAli.Bahrami@Oracle.COM 						    MSG_INTL(MSG_SYS_WRITE),
338*12792SAli.Bahrami@Oracle.COM 						    next->ar_longname,
339*12792SAli.Bahrami@Oracle.COM 						    strerror(err));
3400Sstevel@tonic-gate 						exit(1);
3410Sstevel@tonic-gate 					}
3420Sstevel@tonic-gate 				}
3430Sstevel@tonic-gate 				(void) close(f);
3440Sstevel@tonic-gate 			} else
3450Sstevel@tonic-gate 				exit(1);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 		rawname = 0;
3480Sstevel@tonic-gate 	} /* for */
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
351*12792SAli.Bahrami@Oracle.COM void
pcmd(Cmd_info * cmd_info)3520Sstevel@tonic-gate pcmd(Cmd_info *cmd_info)
3530Sstevel@tonic-gate {
354372Smike_s 	ARFILE	*next;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
3570Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
3580Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
3590Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
3600Sstevel@tonic-gate 			/*
3610Sstevel@tonic-gate 			 * NOTE:
3620Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
3630Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
3640Sstevel@tonic-gate 			 */
365*12792SAli.Bahrami@Oracle.COM 			if (cmd_info->opt_flgs & v_FLAG) {
3660Sstevel@tonic-gate 				(void) fprintf(stdout,
367*12792SAli.Bahrami@Oracle.COM 				    MSG_ORIG(MSG_FMT_P_TITLE),
368*12792SAli.Bahrami@Oracle.COM 				    next->ar_longname);
3690Sstevel@tonic-gate 				(void) fflush(stdout);
3700Sstevel@tonic-gate 			}
3710Sstevel@tonic-gate 			(void) fwrite(next->ar_contents, sizeof (char),
3720Sstevel@tonic-gate 			    next->ar_size, stdout);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
377*12792SAli.Bahrami@Oracle.COM void
mcmd(Cmd_info * cmd_info)3780Sstevel@tonic-gate mcmd(Cmd_info *cmd_info)
3790Sstevel@tonic-gate {
380372Smike_s 	ARFILE	*fileptr;
381372Smike_s 	ARFILE	*abifile = NULL;
382372Smike_s 	ARFILE	*tmphead = NULL;
383372Smike_s 	ARFILE	*tmpend = NULL;
3840Sstevel@tonic-gate 	ARFILE	*backptr1 = NULL;
3850Sstevel@tonic-gate 	ARFILE	*backptr2 = NULL;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	for (fileptr = getfile(cmd_info);
3880Sstevel@tonic-gate 	    fileptr; fileptr = getfile(cmd_info)) {
3890Sstevel@tonic-gate 		if (match(fileptr->ar_longname, cmd_info) != NULL) {
3900Sstevel@tonic-gate 			/*
3910Sstevel@tonic-gate 			 * position Modified
3920Sstevel@tonic-gate 			 */
3930Sstevel@tonic-gate 			mesg('m', fileptr->ar_longname, cmd_info);
3940Sstevel@tonic-gate 			if (tmphead)
3950Sstevel@tonic-gate 				tmpend->ar_next = fileptr;
3960Sstevel@tonic-gate 			else
3970Sstevel@tonic-gate 				tmphead = fileptr;
3980Sstevel@tonic-gate 			tmpend = fileptr;
3990Sstevel@tonic-gate 			if (backptr1) {
4000Sstevel@tonic-gate 				listend = backptr1;
4010Sstevel@tonic-gate 				listend->ar_next = NULL;
4020Sstevel@tonic-gate 			}
4030Sstevel@tonic-gate 			else
4040Sstevel@tonic-gate 				listhead = NULL;
4050Sstevel@tonic-gate 			continue;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 		/*
4080Sstevel@tonic-gate 		 * position Unchaged
4090Sstevel@tonic-gate 		 */
4100Sstevel@tonic-gate 		mesg('u', fileptr->ar_longname, cmd_info);
4110Sstevel@tonic-gate 		backptr1 = fileptr;
4120Sstevel@tonic-gate 		if (cmd_info->ponam && !abifile) {
4130Sstevel@tonic-gate 			if (strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
4140Sstevel@tonic-gate 				abifile = fileptr;
4150Sstevel@tonic-gate 			else
4160Sstevel@tonic-gate 				backptr2 = fileptr;
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (!tmphead)
421*12792SAli.Bahrami@Oracle.COM 		return;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (!cmd_info->ponam)
4240Sstevel@tonic-gate 		listend->ar_next = tmphead;
4250Sstevel@tonic-gate 	else {
4260Sstevel@tonic-gate 		if (!abifile) {
427*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
428*12792SAli.Bahrami@Oracle.COM 			    cmd_info->ponam);
4290Sstevel@tonic-gate 			exit(2);
4300Sstevel@tonic-gate 		}
431*12792SAli.Bahrami@Oracle.COM 		if (cmd_info->opt_flgs & b_FLAG)
4320Sstevel@tonic-gate 			abifile = backptr2;
4330Sstevel@tonic-gate 		if (abifile) {
4340Sstevel@tonic-gate 			tmpend->ar_next = abifile->ar_next;
4350Sstevel@tonic-gate 			abifile->ar_next = tmphead;
4360Sstevel@tonic-gate 		} else {
4370Sstevel@tonic-gate 			tmphead->ar_next = listhead;
4380Sstevel@tonic-gate 			listhead = tmphead;
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	(cmd_info->modified)++;
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
444*12792SAli.Bahrami@Oracle.COM void
tcmd(Cmd_info * cmd_info)4450Sstevel@tonic-gate tcmd(Cmd_info *cmd_info)
4460Sstevel@tonic-gate {
447372Smike_s 	ARFILE	*next;
448372Smike_s 	int	**mp;
4490Sstevel@tonic-gate 	char   buf[DATESIZE];
450*12792SAli.Bahrami@Oracle.COM 	int m1[] = {1, S_IRUSR, 'r', '-'};
451*12792SAli.Bahrami@Oracle.COM 	int m2[] = {1, S_IWUSR, 'w', '-'};
452*12792SAli.Bahrami@Oracle.COM 	int m3[] = {2, S_ISUID, 's', S_IXUSR, 'x', '-'};
453*12792SAli.Bahrami@Oracle.COM 	int m4[] = {1, S_IRGRP, 'r', '-'};
454*12792SAli.Bahrami@Oracle.COM 	int m5[] = {1, S_IWGRP, 'w', '-'};
455*12792SAli.Bahrami@Oracle.COM 	int m6[] = {2, S_ISGID, 's', S_IXGRP, 'x', '-'};
456*12792SAli.Bahrami@Oracle.COM 	int m7[] = {1, S_IROTH, 'r', '-'};
457*12792SAli.Bahrami@Oracle.COM 	int m8[] = {1, S_IWOTH, 'w', '-'};
458*12792SAli.Bahrami@Oracle.COM 	int m9[] = {2, S_ISVTX, 't', S_IXOTH, 'x', '-'};
4590Sstevel@tonic-gate 	int *m[10];
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	m[0] = m1;
4620Sstevel@tonic-gate 	m[1] = m2;
4630Sstevel@tonic-gate 	m[2] = m3;
4640Sstevel@tonic-gate 	m[3] = m4;
4650Sstevel@tonic-gate 	m[4] = m5;
4660Sstevel@tonic-gate 	m[5] = m6;
4670Sstevel@tonic-gate 	m[6] = m7;
4680Sstevel@tonic-gate 	m[7] = m8;
4690Sstevel@tonic-gate 	m[8] = m9;
4700Sstevel@tonic-gate 	m[9] = 0;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
4730Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
4740Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
4750Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
4760Sstevel@tonic-gate 			/*
4770Sstevel@tonic-gate 			 * NOTE:
4780Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
4790Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
4800Sstevel@tonic-gate 			 */
481*12792SAli.Bahrami@Oracle.COM 			if (cmd_info->opt_flgs & v_FLAG) {
4820Sstevel@tonic-gate 				for (mp = &m[0]; mp < &m[9]; )
4830Sstevel@tonic-gate 					ar_select(*mp++, next->ar_mode);
4840Sstevel@tonic-gate 
485*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stdout, MSG_ORIG(MSG_FMT_T_IDSZ),
486*12792SAli.Bahrami@Oracle.COM 				    next->ar_uid, next->ar_gid,
487*12792SAli.Bahrami@Oracle.COM 				    EC_XWORD(next->ar_size));
4880Sstevel@tonic-gate 				if ((strftime(buf,
489*12792SAli.Bahrami@Oracle.COM 				    DATESIZE, MSG_ORIG(MSG_FMT_T_DATE),
4900Sstevel@tonic-gate 				    localtime(&(next->ar_date)))) == 0) {
491*12792SAli.Bahrami@Oracle.COM 					(void) fprintf(stderr,
492*12792SAli.Bahrami@Oracle.COM 					    MSG_INTL(MSG_LOCALTIME));
4930Sstevel@tonic-gate 					exit(1);
4940Sstevel@tonic-gate 				}
495*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stdout,
496*12792SAli.Bahrami@Oracle.COM 				    MSG_ORIG(MSG_FMT_SPSTRSP), buf);
4970Sstevel@tonic-gate 			}
4980Sstevel@tonic-gate 			if ((next->ar_longname[0] == 0) &&
4990Sstevel@tonic-gate 			    (next->ar_rawname[0] != 0))
5000Sstevel@tonic-gate 				(void) fprintf(stdout,
501*12792SAli.Bahrami@Oracle.COM 				    MSG_ORIG(MSG_FMT_STRNL),
502*12792SAli.Bahrami@Oracle.COM 				    trim(next->ar_rawname));
5030Sstevel@tonic-gate 			else
5040Sstevel@tonic-gate 				(void) fprintf(stdout,
505*12792SAli.Bahrami@Oracle.COM 				    MSG_ORIG(MSG_FMT_STRNL),
506*12792SAli.Bahrami@Oracle.COM 				    trim(next->ar_longname));
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 	} /* for */
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
511*12792SAli.Bahrami@Oracle.COM void
qcmd(Cmd_info * cmd_info)5120Sstevel@tonic-gate qcmd(Cmd_info *cmd_info)
5130Sstevel@tonic-gate {
514372Smike_s 	ARFILE *fptr;
5150Sstevel@tonic-gate 
516*12792SAli.Bahrami@Oracle.COM 	if (cmd_info->opt_flgs & (a_FLAG | b_FLAG)) {
517*12792SAli.Bahrami@Oracle.COM 		(void) fprintf(stderr, MSG_INTL(MSG_USAGE_05));
5180Sstevel@tonic-gate 		exit(1);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info))
5210Sstevel@tonic-gate 		;
5220Sstevel@tonic-gate 	cleanup(cmd_info);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate  * Supplementary functions
5270Sstevel@tonic-gate  */
5280Sstevel@tonic-gate static char *
match(char * file,Cmd_info * cmd_info)5290Sstevel@tonic-gate match(char *file, Cmd_info *cmd_info)
5300Sstevel@tonic-gate {
531372Smike_s 	int i;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	for (i = 0; i < cmd_info->namc; i++) {
5340Sstevel@tonic-gate 		if (cmd_info->namv[i] == 0)
5350Sstevel@tonic-gate 			continue;
5360Sstevel@tonic-gate 		if (strcmp(trim(cmd_info->namv[i]), file) == 0) {
5370Sstevel@tonic-gate 			file = cmd_info->namv[i];
5380Sstevel@tonic-gate 			cmd_info->namv[i] = 0;
5390Sstevel@tonic-gate 			return (file);
5400Sstevel@tonic-gate 		}
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 	return (NULL);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * puts the file which was in the list in the linked list
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate static void
cleanup(Cmd_info * cmd_info)5490Sstevel@tonic-gate cleanup(Cmd_info *cmd_info)
5500Sstevel@tonic-gate {
551372Smike_s 	int i;
552372Smike_s 	FILE	*f;
553372Smike_s 	ARFILE	*fileptr;
5540Sstevel@tonic-gate 	struct stat stbuf;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	for (i = 0; i < cmd_info->namc; i++) {
5570Sstevel@tonic-gate 		if (cmd_info->namv[i] == 0)
5580Sstevel@tonic-gate 			continue;
5590Sstevel@tonic-gate 		/*
5600Sstevel@tonic-gate 		 * Appended
5610Sstevel@tonic-gate 		 */
5620Sstevel@tonic-gate 		mesg('a', cmd_info->namv[i], cmd_info);
5630Sstevel@tonic-gate 		f = stats(cmd_info->namv[i], &stbuf);
564*12792SAli.Bahrami@Oracle.COM 		if (f == NULL) {
565*12792SAli.Bahrami@Oracle.COM 			int err = errno;
566*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN),
567*12792SAli.Bahrami@Oracle.COM 			    cmd_info->namv[i], strerror(err));
568*12792SAli.Bahrami@Oracle.COM 		} else {
5690Sstevel@tonic-gate 			fileptr = newfile();
5700Sstevel@tonic-gate 			/* if short name */
5710Sstevel@tonic-gate 			(void) strncpy(fileptr->ar_name,
5720Sstevel@tonic-gate 			    trim(cmd_info->namv[i]), SNAME);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 			if ((fileptr->ar_longname =
5750Sstevel@tonic-gate 			    malloc(strlen(trim(cmd_info->namv[i])) + 1)) ==
5760Sstevel@tonic-gate 			    NULL) {
577*12792SAli.Bahrami@Oracle.COM 				int err = errno;
578*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
579*12792SAli.Bahrami@Oracle.COM 				    strerror(err));
5800Sstevel@tonic-gate 				exit(1);
5810Sstevel@tonic-gate 			}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			(void) strcpy(fileptr->ar_longname,
5840Sstevel@tonic-gate 			    trim(cmd_info->namv[i]));
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 			if ((fileptr->ar_pathname =
5870Sstevel@tonic-gate 			    malloc(strlen(cmd_info->namv[i]) + 1)) == NULL) {
588*12792SAli.Bahrami@Oracle.COM 				int err = errno;
589*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
590*12792SAli.Bahrami@Oracle.COM 				    strerror(err));
5910Sstevel@tonic-gate 				exit(1);
5920Sstevel@tonic-gate 			}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 			(void) strcpy(fileptr->ar_pathname, cmd_info->namv[i]);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 			movefil(fileptr, &stbuf);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 			/* clear 'ar_flag' */
599*12792SAli.Bahrami@Oracle.COM 			fileptr->ar_flag &= ~F_ELFRAW;
6000Sstevel@tonic-gate 
601*12792SAli.Bahrami@Oracle.COM 			/*
602*12792SAli.Bahrami@Oracle.COM 			 * Defer reading contents until needed, and then use
603*12792SAli.Bahrami@Oracle.COM 			 * an in-kernel file-to-file transfer to avoid
604*12792SAli.Bahrami@Oracle.COM 			 * excessive in-process memory use.
605*12792SAli.Bahrami@Oracle.COM 			 */
606*12792SAli.Bahrami@Oracle.COM 			fileptr->ar_contents = NULL;
607*12792SAli.Bahrami@Oracle.COM 
6080Sstevel@tonic-gate 			(void) fclose(f);
6090Sstevel@tonic-gate 			(cmd_info->modified)++;
6100Sstevel@tonic-gate 			cmd_info->namv[i] = 0;
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate  * insert the file 'file' into the temporary file
6170Sstevel@tonic-gate  */
6180Sstevel@tonic-gate static void
movefil(ARFILE * fileptr,struct stat * stbuf)6190Sstevel@tonic-gate movefil(ARFILE *fileptr, struct stat *stbuf)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	fileptr->ar_size = stbuf->st_size;
6220Sstevel@tonic-gate 	fileptr->ar_date = stbuf->st_mtime;
6230Sstevel@tonic-gate 	fileptr->ar_mode = stbuf->st_mode;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * The format of an 'ar' file includes a 6 character
6270Sstevel@tonic-gate 	 * decimal string to contain the uid.
6280Sstevel@tonic-gate 	 *
6290Sstevel@tonic-gate 	 * If the uid or gid is too big to fit, then set it to
6300Sstevel@tonic-gate 	 * nobody (for want of a better value).  Clear the
6310Sstevel@tonic-gate 	 * setuid/setgid bits in the mode to avoid setuid nobody
6320Sstevel@tonic-gate 	 * or setgid nobody files unexpectedly coming into existence.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	if ((fileptr->ar_uid = stbuf->st_uid) > 999999) {
6350Sstevel@tonic-gate 		fileptr->ar_uid = UID_NOBODY;
6360Sstevel@tonic-gate 		if (S_ISREG(fileptr->ar_mode))
6370Sstevel@tonic-gate 			fileptr->ar_mode &= ~S_ISUID;
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 	if ((fileptr->ar_gid = stbuf->st_gid) > 999999) {
6400Sstevel@tonic-gate 		fileptr->ar_gid = GID_NOBODY;
6410Sstevel@tonic-gate 		if (S_ISREG(fileptr->ar_mode))
6420Sstevel@tonic-gate 			fileptr->ar_mode &= ~S_ISGID;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static FILE *
stats(char * file,struct stat * stbuf)6470Sstevel@tonic-gate stats(char *file, struct stat *stbuf)
6480Sstevel@tonic-gate {
649372Smike_s 	FILE *f;
6500Sstevel@tonic-gate 
651*12792SAli.Bahrami@Oracle.COM 	f = fopen(file, MSG_ORIG(MSG_STR_LCR));
6520Sstevel@tonic-gate 	if (f == NULL)
6530Sstevel@tonic-gate 		return (f);
6540Sstevel@tonic-gate 	if (stat(file, stbuf) < 0) {
6550Sstevel@tonic-gate 		(void) fclose(f);
6560Sstevel@tonic-gate 		return (NULL);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 	return (f);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * Used by xcmd()
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate int
create_extract(ARFILE * a,int rawname,int f_len,Cmd_info * cmd_info)6650Sstevel@tonic-gate create_extract(ARFILE *a, int rawname, int f_len, Cmd_info *cmd_info)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	int f;
6690Sstevel@tonic-gate 	char *f_name;
6700Sstevel@tonic-gate 	char *dup = NULL;
6710Sstevel@tonic-gate 	if (rawname)
6720Sstevel@tonic-gate 		f_name = a->ar_rawname;
6730Sstevel@tonic-gate 	else
6740Sstevel@tonic-gate 		f_name = a->ar_longname;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * If -T is specified, check the file length.
6780Sstevel@tonic-gate 	 */
679*12792SAli.Bahrami@Oracle.COM 	if (cmd_info->opt_flgs & T_FLAG) {
6800Sstevel@tonic-gate 		int len;
6810Sstevel@tonic-gate 		len = strlen(f_name);
6820Sstevel@tonic-gate 		if (f_len <= len) {
6830Sstevel@tonic-gate 			dup = malloc(f_len+1);
6840Sstevel@tonic-gate 			if (dup == NULL) {
685*12792SAli.Bahrami@Oracle.COM 				int err = errno;
686*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
687*12792SAli.Bahrami@Oracle.COM 				    strerror(err));
6880Sstevel@tonic-gate 				exit(1);
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 			(void) strncpy(dup, f_name, f_len);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		f_name = dup;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Bug 4052067 - If a file to be extracted has the same
6970Sstevel@tonic-gate 	 * filename as the archive, the archive gets overwritten
6980Sstevel@tonic-gate 	 * which can lead to a corrupted archive or worse, a ufs
6990Sstevel@tonic-gate 	 * deadlock because libelf has mmap'ed the archive!  We
7000Sstevel@tonic-gate 	 * can't rely on strcmp() to test for this case because
7010Sstevel@tonic-gate 	 * the archive could be prefixed with a partial or full
7020Sstevel@tonic-gate 	 * path (and we could be using the rawname from the archive)
7030Sstevel@tonic-gate 	 * This means we have to do the same thing we did for mv,
7040Sstevel@tonic-gate 	 * which is to explicitly check if the file we would extract
7050Sstevel@tonic-gate 	 * to is identical to the archive.  Because part of this
7060Sstevel@tonic-gate 	 * test is essentially what the -C flag does, I've merged
7070Sstevel@tonic-gate 	 * the code together.
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	if (access(f_name, F_OK) != -1) {
7100Sstevel@tonic-gate 		struct stat s1, s2;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * If -C is specified, this is an error anyway
7140Sstevel@tonic-gate 		 */
715*12792SAli.Bahrami@Oracle.COM 		if (cmd_info->opt_flgs & C_FLAG) {
716*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stderr, MSG_INTL(MSG_OVERRIDE_WARN),
717*12792SAli.Bahrami@Oracle.COM 			    f_name);
7180Sstevel@tonic-gate 			if (dup != NULL)
7190Sstevel@tonic-gate 				free(dup);
7200Sstevel@tonic-gate 			return (-1);
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		/*
7240Sstevel@tonic-gate 		 * Okay, -C wasn't specified.  However, now we do
7250Sstevel@tonic-gate 		 * the check to see if the archive would be overwritten
7260Sstevel@tonic-gate 		 * by extracting this file.  stat() both objects and
7270Sstevel@tonic-gate 		 * test to see if their identical.
7280Sstevel@tonic-gate 		 */
7290Sstevel@tonic-gate 		if ((stat(f_name, &s1) == 0) &&
7300Sstevel@tonic-gate 		    (stat(cmd_info->arnam, &s2) == 0)) {
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 			if ((s1.st_dev == s2.st_dev) &&
7330Sstevel@tonic-gate 			    (s1.st_ino == s2.st_ino)) {
7340Sstevel@tonic-gate 
735*12792SAli.Bahrami@Oracle.COM 				(void) fprintf(stderr,
736*12792SAli.Bahrami@Oracle.COM 				    MSG_INTL(MSG_OVERRIDE_WARN), f_name);
7370Sstevel@tonic-gate 				if (dup != NULL)
7380Sstevel@tonic-gate 					free(dup);
7390Sstevel@tonic-gate 				return (-1);
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Okay to create extraction file...
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	f = creat(f_name, (mode_t)a->ar_mode & 0777);
7480Sstevel@tonic-gate 	if (f < 0) {
749*12792SAli.Bahrami@Oracle.COM 		int err = errno;
750*12792SAli.Bahrami@Oracle.COM 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), f_name,
751*12792SAli.Bahrami@Oracle.COM 		    strerror(err));
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Created
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate 		mesg('c', f_name, cmd_info);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	if (dup)
7580Sstevel@tonic-gate 		free(dup);
7590Sstevel@tonic-gate 	return (f);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate static void
mesg(int c,char * file,Cmd_info * cmd_info)7630Sstevel@tonic-gate mesg(int c, char *file, Cmd_info *cmd_info)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate #ifdef XPG4
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * XPG4 does not have any message defined for
7680Sstevel@tonic-gate 	 * 'c' operation.
7690Sstevel@tonic-gate 	 * In fact, XPG only defines messages for
7700Sstevel@tonic-gate 	 *	d, r, a and x at the present. (03/05/'96)
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	if (c == 'c' || c == 'u' || c == 'm')
7730Sstevel@tonic-gate 		return;
7740Sstevel@tonic-gate #endif
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * If 'u' is passed, convert it to 'c'.
7770Sstevel@tonic-gate 	 * 'u' makes more sense since the operation did not
7780Sstevel@tonic-gate 	 * do anything, Unchanged, but 'c' has been used so
7790Sstevel@tonic-gate 	 * I do no want to break the compatibility at this moment.
7800Sstevel@tonic-gate 	 * (03/05/'96).
7810Sstevel@tonic-gate 	 */
7820Sstevel@tonic-gate 	if (c == 'u')
7830Sstevel@tonic-gate 		c = 'c';
784*12792SAli.Bahrami@Oracle.COM 	if (cmd_info->opt_flgs & v_FLAG)
7850Sstevel@tonic-gate 		if (c != 'c')
786*12792SAli.Bahrami@Oracle.COM 			(void) fprintf(stdout, MSG_ORIG(MSG_FMT_FILE), c, file);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate static void
ar_select(int * pairp,unsigned long mode)7900Sstevel@tonic-gate ar_select(int *pairp, unsigned long mode)
7910Sstevel@tonic-gate {
792372Smike_s 	int n, *ap;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	ap = pairp;
7950Sstevel@tonic-gate 	n = *ap++;
7960Sstevel@tonic-gate 	while (--n >= 0 && (mode & *ap++) == 0)
7970Sstevel@tonic-gate 		ap++;
7980Sstevel@tonic-gate 	(void) putchar(*ap);
7990Sstevel@tonic-gate }
800