xref: /onnv-gate/usr/src/cmd/sgs/ar/common/cmd.c (revision 372:5bfa4fc81658)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*372Smike_s  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  *	Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
290Sstevel@tonic-gate  *	  All Rights Reserved
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Incompatible Archive Header
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * The archive file member header used in SunOS 4.1 archive files and
390Sstevel@tonic-gate  * Solaris archive files are incompatible. The header file is:
400Sstevel@tonic-gate  * 	/usr/include/ar.h, struct ar_hdr.
410Sstevel@tonic-gate  * The member ar_name[] in Solaris comforms with Standard and the
420Sstevel@tonic-gate  * member name terminates with '/'. The SunOS's member does not terminate
430Sstevel@tonic-gate  * with '/' character. A bug 4046054 was filed:
440Sstevel@tonic-gate  * 	The ar command in Solaris 2.5.1 is incompatible with archives
450Sstevel@tonic-gate  * 	created on 4.x.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * To handle archive files created in SunOS 4.1 system on Solaris, the
480Sstevel@tonic-gate  * following changes were made:
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * 	1. file.c/writefile()
510Sstevel@tonic-gate  * 		Before writing each member files into the output
520Sstevel@tonic-gate  * 		archive file, ar_name[] is checked. If it is NULL,
530Sstevel@tonic-gate  * 		it means that the original archive header for this
540Sstevel@tonic-gate  * 		member was incompatible with Solaris format.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * 		The original Solaris ar command ended up having
570Sstevel@tonic-gate  * 		NULL name for the header. The change here uses the
580Sstevel@tonic-gate  * 		ar_rawname, which is much closer to the original
590Sstevel@tonic-gate  * 		name.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * 	2. cmd.c
620Sstevel@tonic-gate  * 		For the p command, the code used to use only ar_longname
630Sstevel@tonic-gate  * 		to seach the matching name. The member is set to NULL
640Sstevel@tonic-gate  * 		if the archive member header was incompatible.
650Sstevel@tonic-gate  * 		The ar_rawname is also used to find the matching member name.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * 		For commands to update the archive file, we do not
680Sstevel@tonic-gate  * 		use ar_rawname, and just use the ar_longname. The commands are
690Sstevel@tonic-gate  * 		r (replace), m (modify the position) and d (delete).
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include "inc.h"
730Sstevel@tonic-gate #include "extern.h"
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Function prototypes
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate static char *match(char *, Cmd_info *);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static void cleanup(Cmd_info *);
810Sstevel@tonic-gate static void movefil(ARFILE *, struct stat *);
820Sstevel@tonic-gate static void mesg(int, char *, Cmd_info *);
830Sstevel@tonic-gate static void ar_select(int *, unsigned long);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static FILE *stats(char *, struct stat *);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int create_extract(ARFILE *, int, int, Cmd_info *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Commands
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate int
930Sstevel@tonic-gate rcmd(Cmd_info *cmd_info)
940Sstevel@tonic-gate {
95*372Smike_s 	FILE *f;
96*372Smike_s 	ARFILE *fileptr;
97*372Smike_s 	ARFILE	*abifile = NULL;
98*372Smike_s 	ARFILE	*backptr = NULL;
990Sstevel@tonic-gate 	ARFILE	*endptr;
1000Sstevel@tonic-gate 	ARFILE	*moved_files;
1010Sstevel@tonic-gate 	ARFILE  *prev_entry, *new_listhead, *new_listend;
1020Sstevel@tonic-gate 	int	deleted;
1030Sstevel@tonic-gate 	struct stat stbuf;
1040Sstevel@tonic-gate 	char *gfile;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	new_listhead  = NULL;
1070Sstevel@tonic-gate 	new_listend   = NULL;
1080Sstevel@tonic-gate 	prev_entry    = NULL;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	for (fileptr = getfile(cmd_info);
1110Sstevel@tonic-gate 	    fileptr; fileptr = getfile(cmd_info)) {
1120Sstevel@tonic-gate 		deleted = 0;
1130Sstevel@tonic-gate 		if (!abifile && cmd_info-> ponam &&
1140Sstevel@tonic-gate 		    strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
1150Sstevel@tonic-gate 			abifile = fileptr;
1160Sstevel@tonic-gate 		else if (!abifile)
1170Sstevel@tonic-gate 			backptr = fileptr;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
1200Sstevel@tonic-gate 		    (gfile = match(fileptr->ar_longname, cmd_info)) != NULL) {
1210Sstevel@tonic-gate 			/*
1220Sstevel@tonic-gate 			 * NOTE:
1230Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
1240Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
1250Sstevel@tonic-gate 			 */
1260Sstevel@tonic-gate 			f = stats(gfile, &stbuf); /* gfile is set by match */
1270Sstevel@tonic-gate 			if (f == NULL) {
1280Sstevel@tonic-gate 				if (cmd_info->namc)
1290Sstevel@tonic-gate 					error_message(SYS_OPEN_ERROR,
1300Sstevel@tonic-gate 					    SYSTEM_ERROR, strerror(errno),
1310Sstevel@tonic-gate 					    gfile);
1320Sstevel@tonic-gate 				/*
1330Sstevel@tonic-gate 				 * Created
1340Sstevel@tonic-gate 				 */
1350Sstevel@tonic-gate 				mesg('c', gfile, cmd_info);
1360Sstevel@tonic-gate 			} else {
1370Sstevel@tonic-gate 				if (opt_FLAG(cmd_info, u_FLAG) &&
1380Sstevel@tonic-gate 				    stbuf.st_mtime <= fileptr->ar_date) {
1390Sstevel@tonic-gate 					(void) fclose(f);
1400Sstevel@tonic-gate 					continue;
1410Sstevel@tonic-gate 				}
1420Sstevel@tonic-gate 				/*
1430Sstevel@tonic-gate 				 * Replaced
1440Sstevel@tonic-gate 				 */
1450Sstevel@tonic-gate 				mesg('r', fileptr->ar_longname, cmd_info);
1460Sstevel@tonic-gate 				movefil(fileptr, &stbuf);
1470Sstevel@tonic-gate 				/*
1480Sstevel@tonic-gate 				 * Clear the previous contents.
1490Sstevel@tonic-gate 				 */
1500Sstevel@tonic-gate 				if (fileptr->ar_flag & F_MALLOCED)
1510Sstevel@tonic-gate 					free(fileptr->ar_contents);
1520Sstevel@tonic-gate 				else if (fileptr->ar_flag & F_ELFRAW) {
1530Sstevel@tonic-gate 					/*
1540Sstevel@tonic-gate 					 * clear ar_elf
1550Sstevel@tonic-gate 					 */
1560Sstevel@tonic-gate 					(void) elf_end(fileptr->ar_elf);
1570Sstevel@tonic-gate 					fileptr->ar_elf = 0;
1580Sstevel@tonic-gate 				}
1590Sstevel@tonic-gate 				/* clear 'ar_flag' */
1600Sstevel@tonic-gate 				fileptr->ar_flag &=
1610Sstevel@tonic-gate 				    ~(F_ELFRAW | F_MMAPED | F_MALLOCED);
1620Sstevel@tonic-gate 				if ((cmd_info->OPT_flgs & M_FLAG) == 0) {
1630Sstevel@tonic-gate 					if ((cmd_info->bytes_in_mem +
1640Sstevel@tonic-gate 					    stbuf.st_size)
1650Sstevel@tonic-gate 					    < AR_MAX_BYTES_IN_MEM) {
1660Sstevel@tonic-gate 						if ((fileptr->ar_contents =
1670Sstevel@tonic-gate 						    malloc(ROUNDUP(
1680Sstevel@tonic-gate 						    stbuf.st_size))) == NULL) {
1690Sstevel@tonic-gate 							error_message(
1700Sstevel@tonic-gate 							    MALLOC_ERROR,
1710Sstevel@tonic-gate 							    PLAIN_ERROR,
1720Sstevel@tonic-gate 							    (char *)0);
1730Sstevel@tonic-gate 							exit(1);
1740Sstevel@tonic-gate 						}
1750Sstevel@tonic-gate 						fileptr->ar_flag &=
1760Sstevel@tonic-gate 						    ~(F_ELFRAW | F_MMAPED);
1770Sstevel@tonic-gate 						fileptr->ar_flag |= F_MALLOCED;
1780Sstevel@tonic-gate 						if (fread(fileptr->ar_contents,
1790Sstevel@tonic-gate 						    sizeof (char),
1800Sstevel@tonic-gate 						    stbuf.st_size, f) !=
1810Sstevel@tonic-gate 						    stbuf.st_size) {
1820Sstevel@tonic-gate 							error_message(
1830Sstevel@tonic-gate 							    SYS_READ_ERROR,
1840Sstevel@tonic-gate 							    SYSTEM_ERROR,
1850Sstevel@tonic-gate 							    strerror(errno),
1860Sstevel@tonic-gate 							    fileptr->
1870Sstevel@tonic-gate 							    ar_longname);
1880Sstevel@tonic-gate 							exit(1);
1890Sstevel@tonic-gate 						}
1900Sstevel@tonic-gate 						cmd_info->bytes_in_mem +=
1910Sstevel@tonic-gate 						    stbuf.st_size;
1920Sstevel@tonic-gate 					}
1930Sstevel@tonic-gate 				} else {
1940Sstevel@tonic-gate 					if ((fileptr->ar_contents = (char *)
1950Sstevel@tonic-gate 					    mmap(0, stbuf.st_size,
1960Sstevel@tonic-gate 					    PROT_READ,
1970Sstevel@tonic-gate 					    MAP_SHARED,
1980Sstevel@tonic-gate 					    fileno(f), 0)) == (char *)-1) {
1990Sstevel@tonic-gate 						error_message(MALLOC_ERROR,
2000Sstevel@tonic-gate 						    PLAIN_ERROR, (char *)0);
2010Sstevel@tonic-gate 						exit(1);
2020Sstevel@tonic-gate 					}
2030Sstevel@tonic-gate 					fileptr->ar_flag &=
2040Sstevel@tonic-gate 					    ~(F_ELFRAW | F_MALLOCED);
2050Sstevel@tonic-gate 					fileptr->ar_flag |= F_MMAPED;
2060Sstevel@tonic-gate 				}
2070Sstevel@tonic-gate 				if (fileptr->ar_pathname != NULL)
2080Sstevel@tonic-gate 					free(fileptr->ar_pathname);
2090Sstevel@tonic-gate 				if ((fileptr->ar_pathname =
2100Sstevel@tonic-gate 				    malloc(strlen(gfile) + 1)) == NULL) {
2110Sstevel@tonic-gate 					error_message(MALLOC_ERROR,
2120Sstevel@tonic-gate 					    PLAIN_ERROR, (char *)0);
2130Sstevel@tonic-gate 					exit(1);
2140Sstevel@tonic-gate 				}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 				(void) strcpy(fileptr->ar_pathname, gfile);
2170Sstevel@tonic-gate 				(void) fclose(f);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 				if (cmd_info->ponam && (abifile != fileptr)) {
2200Sstevel@tonic-gate 					deleted = 1;
2210Sstevel@tonic-gate 					/* remove from archive list */
2220Sstevel@tonic-gate 					if (prev_entry != NULL)
2230Sstevel@tonic-gate 						prev_entry->ar_next = NULL;
2240Sstevel@tonic-gate 					else
2250Sstevel@tonic-gate 						listhead = NULL;
2260Sstevel@tonic-gate 					listend = prev_entry;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 					/* add to moved list */
2290Sstevel@tonic-gate 					if (new_listhead == NULL)
2300Sstevel@tonic-gate 						new_listhead = fileptr;
2310Sstevel@tonic-gate 					else
2320Sstevel@tonic-gate 						new_listend->ar_next = fileptr;
2330Sstevel@tonic-gate 					new_listend = fileptr;
2340Sstevel@tonic-gate 				}
2350Sstevel@tonic-gate 				cmd_info->modified++;
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 		else
2390Sstevel@tonic-gate 			/*
2400Sstevel@tonic-gate 			 * Unchaged
2410Sstevel@tonic-gate 			 */
2420Sstevel@tonic-gate 			mesg('u', fileptr->ar_longname, cmd_info);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if (deleted)
2450Sstevel@tonic-gate 			deleted = 0;
2460Sstevel@tonic-gate 		else
2470Sstevel@tonic-gate 			prev_entry = fileptr;
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	endptr = listend;
2510Sstevel@tonic-gate 	cleanup(cmd_info);
2520Sstevel@tonic-gate 	if (cmd_info->ponam && endptr &&
2530Sstevel@tonic-gate 	    (((moved_files = endptr->ar_next) != NULL) || new_listhead)) {
2540Sstevel@tonic-gate 		if (!abifile) {
2550Sstevel@tonic-gate 			error_message(NOT_FOUND_02_ERROR,
2560Sstevel@tonic-gate 			    PLAIN_ERROR, (char *)0, cmd_info->ponam);
2570Sstevel@tonic-gate 			exit(2);
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 		endptr->ar_next = NULL;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * link new/moved files into archive entry list...
2630Sstevel@tonic-gate 		 * 1: prepend newlist to moved/appended list
2640Sstevel@tonic-gate 		 */
2650Sstevel@tonic-gate 		if (new_listhead) {
2660Sstevel@tonic-gate 			if (!moved_files)
2670Sstevel@tonic-gate 				listend = new_listend;
2680Sstevel@tonic-gate 			new_listend->ar_next = moved_files;
2690Sstevel@tonic-gate 			moved_files = new_listhead;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		/* 2: insert at appropriate position... */
2720Sstevel@tonic-gate 		if (opt_FLAG(cmd_info, b_FLAG))
2730Sstevel@tonic-gate 			abifile = backptr;
2740Sstevel@tonic-gate 		if (abifile) {
2750Sstevel@tonic-gate 			listend->ar_next = abifile->ar_next;
2760Sstevel@tonic-gate 			abifile->ar_next = moved_files;
2770Sstevel@tonic-gate 		} else {
2780Sstevel@tonic-gate 			listend->ar_next = listhead;
2790Sstevel@tonic-gate 			listhead = moved_files;
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 		listend = endptr;
2820Sstevel@tonic-gate 	} else if (cmd_info->ponam && !abifile)
2830Sstevel@tonic-gate 		error_message(NOT_FOUND_02_ERROR,
2840Sstevel@tonic-gate 		    PLAIN_ERROR, (char *)0, cmd_info->ponam);
2850Sstevel@tonic-gate 	return (0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate int
2890Sstevel@tonic-gate dcmd(Cmd_info *cmd_info)
2900Sstevel@tonic-gate {
291*372Smike_s 	ARFILE	*fptr;
292*372Smike_s 	ARFILE *backptr = NULL;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info)) {
2950Sstevel@tonic-gate 		if (match(fptr->ar_longname, cmd_info) != NULL) {
2960Sstevel@tonic-gate 			/*
2970Sstevel@tonic-gate 			 * NOTE:
2980Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
2990Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
3000Sstevel@tonic-gate 			 */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 			/*
3030Sstevel@tonic-gate 			 * Deleted
3040Sstevel@tonic-gate 			 */
3050Sstevel@tonic-gate 			mesg('d', fptr->ar_longname, cmd_info);
3060Sstevel@tonic-gate 			if (backptr == NULL) {
3070Sstevel@tonic-gate 				listhead = NULL;
3080Sstevel@tonic-gate 				listend = NULL;
3090Sstevel@tonic-gate 			} else {
3100Sstevel@tonic-gate 				backptr->ar_next = NULL;
3110Sstevel@tonic-gate 				listend = backptr;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 			cmd_info->modified = 1;
3140Sstevel@tonic-gate 		} else {
3150Sstevel@tonic-gate 			/*
3160Sstevel@tonic-gate 			 * Unchaged
3170Sstevel@tonic-gate 			 */
3180Sstevel@tonic-gate 			mesg('u', fptr->ar_longname, cmd_info);
3190Sstevel@tonic-gate 			backptr = fptr;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	return (0);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate int
3260Sstevel@tonic-gate xcmd(Cmd_info *cmd_info)
3270Sstevel@tonic-gate {
328*372Smike_s 	int f;
329*372Smike_s 	ARFILE *next;
3300Sstevel@tonic-gate 	int rawname = 0;
3310Sstevel@tonic-gate 	int f_len = 0;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * If -T is specified, get the maximum file name length.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (cmd_info->OPT_flgs & T_FLAG) {
3370Sstevel@tonic-gate 		f_len = pathconf(".", _PC_NAME_MAX);
3380Sstevel@tonic-gate 		if (f_len == -1) {
3390Sstevel@tonic-gate 			error_message(PATHCONF_ERROR,
3400Sstevel@tonic-gate 			    SYSTEM_ERROR, strerror(errno));
3410Sstevel@tonic-gate 			exit(1);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
3450Sstevel@tonic-gate 		if ((next->ar_longname[0] == 0) && (next->ar_rawname[0] != 0))
3460Sstevel@tonic-gate 			rawname = 1;
3470Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
3480Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
3490Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
3500Sstevel@tonic-gate 			/*
3510Sstevel@tonic-gate 			 * NOTE:
3520Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
3530Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
3540Sstevel@tonic-gate 			 */
3550Sstevel@tonic-gate 			f = create_extract(next, rawname, f_len, cmd_info);
3560Sstevel@tonic-gate 			if (f >= 0) {
3570Sstevel@tonic-gate 				if (rawname) {
3580Sstevel@tonic-gate 					/*
3590Sstevel@tonic-gate 					 * eXtracted
3600Sstevel@tonic-gate 					 */
3610Sstevel@tonic-gate 					mesg('x', next->ar_rawname, cmd_info);
3620Sstevel@tonic-gate 					if (write(f, next->ar_contents,
3630Sstevel@tonic-gate 					    (unsigned)next->ar_size) !=
3640Sstevel@tonic-gate 					    next->ar_size) {
3650Sstevel@tonic-gate 						error_message(SYS_WRITE_ERROR,
3660Sstevel@tonic-gate 						    SYSTEM_ERROR,
3670Sstevel@tonic-gate 						    strerror(errno),
3680Sstevel@tonic-gate 						    next->ar_rawname);
3690Sstevel@tonic-gate 						exit(1);
3700Sstevel@tonic-gate 					}
3710Sstevel@tonic-gate 				} else {
3720Sstevel@tonic-gate 					/*
3730Sstevel@tonic-gate 					 * eXtracted
3740Sstevel@tonic-gate 					 */
3750Sstevel@tonic-gate 					mesg('x', next->ar_longname, cmd_info);
3760Sstevel@tonic-gate 					if (write(f, next->ar_contents,
3770Sstevel@tonic-gate 					    (unsigned)next->ar_size) !=
3780Sstevel@tonic-gate 					    next->ar_size) {
3790Sstevel@tonic-gate 						error_message(SYS_WRITE_ERROR,
3800Sstevel@tonic-gate 						    SYSTEM_ERROR,
3810Sstevel@tonic-gate 						    strerror(errno),
3820Sstevel@tonic-gate 						    next->ar_longname);
3830Sstevel@tonic-gate 						exit(1);
3840Sstevel@tonic-gate 					}
3850Sstevel@tonic-gate 				}
3860Sstevel@tonic-gate 				(void) close(f);
3870Sstevel@tonic-gate 			} else
3880Sstevel@tonic-gate 				exit(1);
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 		rawname = 0;
3910Sstevel@tonic-gate 	} /* for */
3920Sstevel@tonic-gate 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate int
3960Sstevel@tonic-gate pcmd(Cmd_info *cmd_info)
3970Sstevel@tonic-gate {
398*372Smike_s 	ARFILE	*next;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
4010Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
4020Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
4030Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
4040Sstevel@tonic-gate 			/*
4050Sstevel@tonic-gate 			 * NOTE:
4060Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
4070Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
4080Sstevel@tonic-gate 			 */
4090Sstevel@tonic-gate 			if (opt_FLAG(cmd_info, v_FLAG)) {
4100Sstevel@tonic-gate 				(void) fprintf(stdout,
4110Sstevel@tonic-gate 				    "\n<%s>\n\n", next->ar_longname);
4120Sstevel@tonic-gate 				(void) fflush(stdout);
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			(void) fwrite(next->ar_contents, sizeof (char),
4150Sstevel@tonic-gate 			    next->ar_size, stdout);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	return (0);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate int
4220Sstevel@tonic-gate mcmd(Cmd_info *cmd_info)
4230Sstevel@tonic-gate {
424*372Smike_s 	ARFILE	*fileptr;
425*372Smike_s 	ARFILE	*abifile = NULL;
426*372Smike_s 	ARFILE	*tmphead = NULL;
427*372Smike_s 	ARFILE	*tmpend = NULL;
4280Sstevel@tonic-gate 	ARFILE	*backptr1 = NULL;
4290Sstevel@tonic-gate 	ARFILE	*backptr2 = NULL;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	for (fileptr = getfile(cmd_info);
4320Sstevel@tonic-gate 	    fileptr; fileptr = getfile(cmd_info)) {
4330Sstevel@tonic-gate 		if (match(fileptr->ar_longname, cmd_info) != NULL) {
4340Sstevel@tonic-gate 			/*
4350Sstevel@tonic-gate 			 * position Modified
4360Sstevel@tonic-gate 			 */
4370Sstevel@tonic-gate 			mesg('m', fileptr->ar_longname, cmd_info);
4380Sstevel@tonic-gate 			if (tmphead)
4390Sstevel@tonic-gate 				tmpend->ar_next = fileptr;
4400Sstevel@tonic-gate 			else
4410Sstevel@tonic-gate 				tmphead = fileptr;
4420Sstevel@tonic-gate 			tmpend = fileptr;
4430Sstevel@tonic-gate 			if (backptr1) {
4440Sstevel@tonic-gate 				listend = backptr1;
4450Sstevel@tonic-gate 				listend->ar_next = NULL;
4460Sstevel@tonic-gate 			}
4470Sstevel@tonic-gate 			else
4480Sstevel@tonic-gate 				listhead = NULL;
4490Sstevel@tonic-gate 			continue;
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		/*
4520Sstevel@tonic-gate 		 * position Unchaged
4530Sstevel@tonic-gate 		 */
4540Sstevel@tonic-gate 		mesg('u', fileptr->ar_longname, cmd_info);
4550Sstevel@tonic-gate 		backptr1 = fileptr;
4560Sstevel@tonic-gate 		if (cmd_info->ponam && !abifile) {
4570Sstevel@tonic-gate 			if (strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
4580Sstevel@tonic-gate 				abifile = fileptr;
4590Sstevel@tonic-gate 			else
4600Sstevel@tonic-gate 				backptr2 = fileptr;
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (!tmphead)
4650Sstevel@tonic-gate 		return (1);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (!cmd_info->ponam)
4680Sstevel@tonic-gate 		listend->ar_next = tmphead;
4690Sstevel@tonic-gate 	else {
4700Sstevel@tonic-gate 		if (!abifile) {
4710Sstevel@tonic-gate 			error_message(NOT_FOUND_02_ERROR,
4720Sstevel@tonic-gate 			    PLAIN_ERROR, (char *)0, cmd_info->ponam);
4730Sstevel@tonic-gate 			exit(2);
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 		if (opt_FLAG(cmd_info, b_FLAG))
4760Sstevel@tonic-gate 			abifile = backptr2;
4770Sstevel@tonic-gate 		if (abifile) {
4780Sstevel@tonic-gate 			tmpend->ar_next = abifile->ar_next;
4790Sstevel@tonic-gate 			abifile->ar_next = tmphead;
4800Sstevel@tonic-gate 		} else {
4810Sstevel@tonic-gate 			tmphead->ar_next = listhead;
4820Sstevel@tonic-gate 			listhead = tmphead;
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 	(cmd_info->modified)++;
4860Sstevel@tonic-gate 	return (0);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate int
4900Sstevel@tonic-gate tcmd(Cmd_info *cmd_info)
4910Sstevel@tonic-gate {
492*372Smike_s 	ARFILE	*next;
493*372Smike_s 	int	**mp;
4940Sstevel@tonic-gate 	char   buf[DATESIZE];
4950Sstevel@tonic-gate 	int m1[] = {1, ROWN, 'r', '-'};
4960Sstevel@tonic-gate 	int m2[] = {1, WOWN, 'w', '-'};
4970Sstevel@tonic-gate 	int m3[] = {2, SUID, 's', XOWN, 'x', '-'};
4980Sstevel@tonic-gate 	int m4[] = {1, RGRP, 'r', '-'};
4990Sstevel@tonic-gate 	int m5[] = {1, WGRP, 'w', '-'};
5000Sstevel@tonic-gate 	int m6[] = {2, SGID, 's', XGRP, 'x', '-'};
5010Sstevel@tonic-gate 	int m7[] = {1, ROTH, 'r', '-'};
5020Sstevel@tonic-gate 	int m8[] = {1, WOTH, 'w', '-'};
5030Sstevel@tonic-gate 	int m9[] = {2, STXT, 't', XOTH, 'x', '-'};
5040Sstevel@tonic-gate 	int *m[10];
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	m[0] = m1;
5070Sstevel@tonic-gate 	m[1] = m2;
5080Sstevel@tonic-gate 	m[2] = m3;
5090Sstevel@tonic-gate 	m[3] = m4;
5100Sstevel@tonic-gate 	m[4] = m5;
5110Sstevel@tonic-gate 	m[5] = m6;
5120Sstevel@tonic-gate 	m[6] = m7;
5130Sstevel@tonic-gate 	m[7] = m8;
5140Sstevel@tonic-gate 	m[8] = m9;
5150Sstevel@tonic-gate 	m[9] = 0;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
5180Sstevel@tonic-gate 		if (cmd_info->namc == 0 ||
5190Sstevel@tonic-gate 		    match(next->ar_longname, cmd_info) != NULL ||
5200Sstevel@tonic-gate 		    match(next->ar_rawname, cmd_info) != NULL) {
5210Sstevel@tonic-gate 			/*
5220Sstevel@tonic-gate 			 * NOTE:
5230Sstevel@tonic-gate 			 *	Refer to "Incompatible Archive Header"
5240Sstevel@tonic-gate 			 *	blocked comment at the beginning of this file.
5250Sstevel@tonic-gate 			 */
5260Sstevel@tonic-gate 			if (opt_FLAG(cmd_info, v_FLAG)) {
5270Sstevel@tonic-gate 				for (mp = &m[0]; mp < &m[9]; )
5280Sstevel@tonic-gate 					ar_select(*mp++, next->ar_mode);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 				(void) fprintf(stdout, "%6d/%6d", next->ar_uid,
5310Sstevel@tonic-gate 				    next->ar_gid);
5320Sstevel@tonic-gate 				(void) fprintf(stdout, "%7ld", next->ar_size);
5330Sstevel@tonic-gate 				if ((strftime(buf,
5340Sstevel@tonic-gate 				    DATESIZE,
5350Sstevel@tonic-gate 				    "%b %e %H:%M %Y",
5360Sstevel@tonic-gate 				    localtime(&(next->ar_date)))) == 0) {
5370Sstevel@tonic-gate 					error_message(LOCALTIME_ERROR,
5380Sstevel@tonic-gate 					    PLAIN_ERROR, (char *)0);
5390Sstevel@tonic-gate 					exit(1);
5400Sstevel@tonic-gate 				}
5410Sstevel@tonic-gate 				(void) fprintf(stdout, " %s ", buf);
5420Sstevel@tonic-gate 			}
5430Sstevel@tonic-gate 			if ((next->ar_longname[0] == 0) &&
5440Sstevel@tonic-gate 			    (next->ar_rawname[0] != 0))
5450Sstevel@tonic-gate 				(void) fprintf(stdout,
5460Sstevel@tonic-gate 				    "%s\n", trim(next->ar_rawname));
5470Sstevel@tonic-gate 			else
5480Sstevel@tonic-gate 				(void) fprintf(stdout,
5490Sstevel@tonic-gate 				    "%s\n", trim(next->ar_longname));
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 	} /* for */
5520Sstevel@tonic-gate 	return (0);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate int
5560Sstevel@tonic-gate qcmd(Cmd_info *cmd_info)
5570Sstevel@tonic-gate {
558*372Smike_s 	ARFILE *fptr;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (opt_FLAG(cmd_info, a_FLAG) || opt_FLAG(cmd_info, b_FLAG)) {
5610Sstevel@tonic-gate 		error_message(USAGE_05_ERROR,
5620Sstevel@tonic-gate 		    PLAIN_ERROR, (char *)0);
5630Sstevel@tonic-gate 		exit(1);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info))
5660Sstevel@tonic-gate 		;
5670Sstevel@tonic-gate 	cleanup(cmd_info);
5680Sstevel@tonic-gate 	return (0);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * Supplementary functions
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate static char *
5750Sstevel@tonic-gate match(char *file, Cmd_info *cmd_info)
5760Sstevel@tonic-gate {
577*372Smike_s 	int i;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	for (i = 0; i < cmd_info->namc; i++) {
5800Sstevel@tonic-gate 		if (cmd_info->namv[i] == 0)
5810Sstevel@tonic-gate 			continue;
5820Sstevel@tonic-gate 		if (strcmp(trim(cmd_info->namv[i]), file) == 0) {
5830Sstevel@tonic-gate 			file = cmd_info->namv[i];
5840Sstevel@tonic-gate 			cmd_info->namv[i] = 0;
5850Sstevel@tonic-gate 			return (file);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	return (NULL);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate  * puts the file which was in the list in the linked list
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate static void
5950Sstevel@tonic-gate cleanup(Cmd_info *cmd_info)
5960Sstevel@tonic-gate {
597*372Smike_s 	int i;
598*372Smike_s 	FILE	*f;
599*372Smike_s 	ARFILE	*fileptr;
6000Sstevel@tonic-gate 	struct stat stbuf;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	for (i = 0; i < cmd_info->namc; i++) {
6030Sstevel@tonic-gate 		if (cmd_info->namv[i] == 0)
6040Sstevel@tonic-gate 			continue;
6050Sstevel@tonic-gate 		/*
6060Sstevel@tonic-gate 		 * Appended
6070Sstevel@tonic-gate 		 */
6080Sstevel@tonic-gate 		mesg('a', cmd_info->namv[i], cmd_info);
6090Sstevel@tonic-gate 		f = stats(cmd_info->namv[i], &stbuf);
6100Sstevel@tonic-gate 		if (f == NULL)
6110Sstevel@tonic-gate 			error_message(SYS_OPEN_ERROR,
6120Sstevel@tonic-gate 			    SYSTEM_ERROR, strerror(errno), cmd_info->namv[i]);
6130Sstevel@tonic-gate 		else {
6140Sstevel@tonic-gate 			fileptr = newfile();
6150Sstevel@tonic-gate 			/* if short name */
6160Sstevel@tonic-gate 			(void) strncpy(fileptr->ar_name,
6170Sstevel@tonic-gate 			    trim(cmd_info->namv[i]), SNAME);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 			if ((fileptr->ar_longname =
6200Sstevel@tonic-gate 			    malloc(strlen(trim(cmd_info->namv[i])) + 1)) ==
6210Sstevel@tonic-gate 			    NULL) {
6220Sstevel@tonic-gate 				error_message(MALLOC_ERROR,
6230Sstevel@tonic-gate 				    PLAIN_ERROR, (char *)0);
6240Sstevel@tonic-gate 				exit(1);
6250Sstevel@tonic-gate 			}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 			(void) strcpy(fileptr->ar_longname,
6280Sstevel@tonic-gate 			    trim(cmd_info->namv[i]));
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 			if ((fileptr->ar_pathname =
6310Sstevel@tonic-gate 			    malloc(strlen(cmd_info->namv[i]) + 1)) == NULL) {
6320Sstevel@tonic-gate 				error_message(MALLOC_ERROR,
6330Sstevel@tonic-gate 				    PLAIN_ERROR, (char *)0);
6340Sstevel@tonic-gate 				exit(1);
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 			(void) strcpy(fileptr->ar_pathname, cmd_info->namv[i]);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 			movefil(fileptr, &stbuf);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 			/* clear 'ar_flag' */
6420Sstevel@tonic-gate 			fileptr->ar_flag &= ~(F_ELFRAW | F_MMAPED | F_MALLOCED);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 			if ((cmd_info->OPT_flgs & M_FLAG) == 0) {
6450Sstevel@tonic-gate 				if ((cmd_info->bytes_in_mem + stbuf.st_size) <
6460Sstevel@tonic-gate 				    AR_MAX_BYTES_IN_MEM) {
6470Sstevel@tonic-gate 					fileptr->ar_flag &=
6480Sstevel@tonic-gate 					    ~(F_ELFRAW | F_MMAPED);
6490Sstevel@tonic-gate 					fileptr->ar_flag |= F_MALLOCED;
6500Sstevel@tonic-gate 					if ((fileptr->ar_contents =
6510Sstevel@tonic-gate 					    malloc(ROUNDUP(stbuf.st_size))) ==
6520Sstevel@tonic-gate 					    NULL) {
6530Sstevel@tonic-gate 						error_message(MALLOC_ERROR,
6540Sstevel@tonic-gate 						    PLAIN_ERROR, (char *)0);
6550Sstevel@tonic-gate 						exit(1);
6560Sstevel@tonic-gate 					}
6570Sstevel@tonic-gate 					if (fread(fileptr->ar_contents,
6580Sstevel@tonic-gate 					    sizeof (char), stbuf.st_size,
6590Sstevel@tonic-gate 					    f) != stbuf.st_size) {
6600Sstevel@tonic-gate 						error_message(SYS_READ_ERROR,
6610Sstevel@tonic-gate 						    SYSTEM_ERROR,
6620Sstevel@tonic-gate 						    strerror(errno),
6630Sstevel@tonic-gate 						    fileptr->ar_longname);
6640Sstevel@tonic-gate 						exit(1);
6650Sstevel@tonic-gate 					}
6660Sstevel@tonic-gate 					cmd_info->bytes_in_mem += stbuf.st_size;
6670Sstevel@tonic-gate 				}
6680Sstevel@tonic-gate 			} else {
6690Sstevel@tonic-gate 				fileptr->ar_flag &= ~(F_ELFRAW | F_MALLOCED);
6700Sstevel@tonic-gate 				fileptr->ar_flag |= F_MMAPED;
6710Sstevel@tonic-gate 				if ((fileptr->ar_contents =
6720Sstevel@tonic-gate 				    (char *)mmap(0, stbuf.st_size, PROT_READ,
6730Sstevel@tonic-gate 				    MAP_SHARED, fileno(f), 0)) == (char *)-1) {
6740Sstevel@tonic-gate 					error_message(MALLOC_ERROR,
6750Sstevel@tonic-gate 					    PLAIN_ERROR, (char *)0);
6760Sstevel@tonic-gate 					exit(1);
6770Sstevel@tonic-gate 				}
6780Sstevel@tonic-gate 			}
6790Sstevel@tonic-gate 			(void) fclose(f);
6800Sstevel@tonic-gate 			(cmd_info->modified)++;
6810Sstevel@tonic-gate 			cmd_info->namv[i] = 0;
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate  * insert the file 'file' into the temporary file
6880Sstevel@tonic-gate  */
6890Sstevel@tonic-gate static void
6900Sstevel@tonic-gate movefil(ARFILE *fileptr, struct stat *stbuf)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	fileptr->ar_size = stbuf->st_size;
6930Sstevel@tonic-gate 	fileptr->ar_date = stbuf->st_mtime;
6940Sstevel@tonic-gate 	fileptr->ar_mode = stbuf->st_mode;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * The format of an 'ar' file includes a 6 character
6980Sstevel@tonic-gate 	 * decimal string to contain the uid.
6990Sstevel@tonic-gate 	 *
7000Sstevel@tonic-gate 	 * If the uid or gid is too big to fit, then set it to
7010Sstevel@tonic-gate 	 * nobody (for want of a better value).  Clear the
7020Sstevel@tonic-gate 	 * setuid/setgid bits in the mode to avoid setuid nobody
7030Sstevel@tonic-gate 	 * or setgid nobody files unexpectedly coming into existence.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	if ((fileptr->ar_uid = stbuf->st_uid) > 999999) {
7060Sstevel@tonic-gate 		fileptr->ar_uid = UID_NOBODY;
7070Sstevel@tonic-gate 		if (S_ISREG(fileptr->ar_mode))
7080Sstevel@tonic-gate 			fileptr->ar_mode &= ~S_ISUID;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	if ((fileptr->ar_gid = stbuf->st_gid) > 999999) {
7110Sstevel@tonic-gate 		fileptr->ar_gid = GID_NOBODY;
7120Sstevel@tonic-gate 		if (S_ISREG(fileptr->ar_mode))
7130Sstevel@tonic-gate 			fileptr->ar_mode &= ~S_ISGID;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate static FILE *
7180Sstevel@tonic-gate stats(char *file, struct stat *stbuf)
7190Sstevel@tonic-gate {
720*372Smike_s 	FILE *f;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	f = fopen(file, "r");
7230Sstevel@tonic-gate 	if (f == NULL)
7240Sstevel@tonic-gate 		return (f);
7250Sstevel@tonic-gate 	if (stat(file, stbuf) < 0) {
7260Sstevel@tonic-gate 		(void) fclose(f);
7270Sstevel@tonic-gate 		return (NULL);
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	return (f);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * Used by xcmd()
7340Sstevel@tonic-gate  */
7350Sstevel@tonic-gate int
7360Sstevel@tonic-gate create_extract(ARFILE *a, int rawname, int f_len, Cmd_info *cmd_info)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	int f;
7400Sstevel@tonic-gate 	char *f_name;
7410Sstevel@tonic-gate 	char *dup = NULL;
7420Sstevel@tonic-gate 	if (rawname)
7430Sstevel@tonic-gate 		f_name = a->ar_rawname;
7440Sstevel@tonic-gate 	else
7450Sstevel@tonic-gate 		f_name = a->ar_longname;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * If -T is specified, check the file length.
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	if (cmd_info->OPT_flgs & T_FLAG) {
7510Sstevel@tonic-gate 		int len;
7520Sstevel@tonic-gate 		len = strlen(f_name);
7530Sstevel@tonic-gate 		if (f_len <= len) {
7540Sstevel@tonic-gate 			dup = malloc(f_len+1);
7550Sstevel@tonic-gate 			if (dup == NULL) {
7560Sstevel@tonic-gate 				error_message(MALLOC_ERROR,
7570Sstevel@tonic-gate 				    PLAIN_ERROR, (char *)0);
7580Sstevel@tonic-gate 				exit(1);
7590Sstevel@tonic-gate 			}
7600Sstevel@tonic-gate 			(void) strncpy(dup, f_name, f_len);
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 		f_name = dup;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Bug 4052067 - If a file to be extracted has the same
7670Sstevel@tonic-gate 	 * filename as the archive, the archive gets overwritten
7680Sstevel@tonic-gate 	 * which can lead to a corrupted archive or worse, a ufs
7690Sstevel@tonic-gate 	 * deadlock because libelf has mmap'ed the archive!  We
7700Sstevel@tonic-gate 	 * can't rely on strcmp() to test for this case because
7710Sstevel@tonic-gate 	 * the archive could be prefixed with a partial or full
7720Sstevel@tonic-gate 	 * path (and we could be using the rawname from the archive)
7730Sstevel@tonic-gate 	 * This means we have to do the same thing we did for mv,
7740Sstevel@tonic-gate 	 * which is to explicitly check if the file we would extract
7750Sstevel@tonic-gate 	 * to is identical to the archive.  Because part of this
7760Sstevel@tonic-gate 	 * test is essentially what the -C flag does, I've merged
7770Sstevel@tonic-gate 	 * the code together.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	if (access(f_name, F_OK) != -1) {
7800Sstevel@tonic-gate 		struct stat s1, s2;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		/*
7830Sstevel@tonic-gate 		 * If -C is specified, this is an error anyway
7840Sstevel@tonic-gate 		 */
7850Sstevel@tonic-gate 		if (cmd_info->OPT_flgs & C_FLAG) {
7860Sstevel@tonic-gate 			if (dup != NULL)
7870Sstevel@tonic-gate 				free(dup);
7880Sstevel@tonic-gate 			error_message(OVERRIDE_WARN_ERROR,
7890Sstevel@tonic-gate 			    PLAIN_ERROR, (char *)0, f_name);
7900Sstevel@tonic-gate 			return (-1);
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		/*
7940Sstevel@tonic-gate 		 * Okay, -C wasn't specified.  However, now we do
7950Sstevel@tonic-gate 		 * the check to see if the archive would be overwritten
7960Sstevel@tonic-gate 		 * by extracting this file.  stat() both objects and
7970Sstevel@tonic-gate 		 * test to see if their identical.
7980Sstevel@tonic-gate 		 */
7990Sstevel@tonic-gate 		if ((stat(f_name, &s1) == 0) &&
8000Sstevel@tonic-gate 		    (stat(cmd_info->arnam, &s2) == 0)) {
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 			if ((s1.st_dev == s2.st_dev) &&
8030Sstevel@tonic-gate 			    (s1.st_ino == s2.st_ino)) {
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 				if (dup != NULL)
8060Sstevel@tonic-gate 					free(dup);
8070Sstevel@tonic-gate 				error_message(OVERRIDE_WARN_ERROR,
8080Sstevel@tonic-gate 				    PLAIN_ERROR, (char *)0, f_name);
8090Sstevel@tonic-gate 				return (-1);
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/*
8150Sstevel@tonic-gate 	 * Okay to create extraction file...
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 	f = creat(f_name, (mode_t)a->ar_mode & 0777);
8180Sstevel@tonic-gate 	if (f < 0) {
8190Sstevel@tonic-gate 		error_message(SYS_CREATE_01_ERROR,
8200Sstevel@tonic-gate 		    SYSTEM_ERROR, strerror(errno), f_name);
8210Sstevel@tonic-gate 		/*
8220Sstevel@tonic-gate 		 * Created
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		mesg('c', f_name, cmd_info);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	if (dup)
8270Sstevel@tonic-gate 		free(dup);
8280Sstevel@tonic-gate 	return (f);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate static void
8320Sstevel@tonic-gate mesg(int c, char *file, Cmd_info *cmd_info)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate #ifdef XPG4
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * XPG4 does not have any message defined for
8370Sstevel@tonic-gate 	 * 'c' operation.
8380Sstevel@tonic-gate 	 * In fact, XPG only defines messages for
8390Sstevel@tonic-gate 	 *	d, r, a and x at the present. (03/05/'96)
8400Sstevel@tonic-gate 	 */
8410Sstevel@tonic-gate 	if (c == 'c' || c == 'u' || c == 'm')
8420Sstevel@tonic-gate 		return;
8430Sstevel@tonic-gate #endif
8440Sstevel@tonic-gate 	/*
8450Sstevel@tonic-gate 	 * If 'u' is passed, convert it to 'c'.
8460Sstevel@tonic-gate 	 * 'u' makes more sense since the operation did not
8470Sstevel@tonic-gate 	 * do anything, Unchanged, but 'c' has been used so
8480Sstevel@tonic-gate 	 * I do no want to break the compatibility at this moment.
8490Sstevel@tonic-gate 	 * (03/05/'96).
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	if (c == 'u')
8520Sstevel@tonic-gate 		c = 'c';
8530Sstevel@tonic-gate 	if (opt_FLAG(cmd_info, v_FLAG))
8540Sstevel@tonic-gate 		if (c != 'c')
8550Sstevel@tonic-gate 			(void) fprintf(stdout, "%c - %s\n", c, file);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate static void
8590Sstevel@tonic-gate ar_select(int *pairp, unsigned long mode)
8600Sstevel@tonic-gate {
861*372Smike_s 	int n, *ap;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	ap = pairp;
8640Sstevel@tonic-gate 	n = *ap++;
8650Sstevel@tonic-gate 	while (--n >= 0 && (mode & *ap++) == 0)
8660Sstevel@tonic-gate 		ap++;
8670Sstevel@tonic-gate 	(void) putchar(*ap);
8680Sstevel@tonic-gate }
869