17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*ba7866cdSAli Bahrami * Common Development and Distribution License (the "License").
6*ba7866cdSAli Bahrami * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*ba7866cdSAli Bahrami * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate * Copyright (c) 1988 AT&T
277c478bd9Sstevel@tonic-gate * All Rights Reserved
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * Incompatible Archive Header
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * The archive file member header used in SunOS 4.1 archive files and
357c478bd9Sstevel@tonic-gate * Solaris archive files are incompatible. The header file is:
367c478bd9Sstevel@tonic-gate * /usr/include/ar.h, struct ar_hdr.
377c478bd9Sstevel@tonic-gate * The member ar_name[] in Solaris comforms with Standard and the
387c478bd9Sstevel@tonic-gate * member name terminates with '/'. The SunOS's member does not terminate
397c478bd9Sstevel@tonic-gate * with '/' character. A bug 4046054 was filed:
407c478bd9Sstevel@tonic-gate * The ar command in Solaris 2.5.1 is incompatible with archives
417c478bd9Sstevel@tonic-gate * created on 4.x.
427c478bd9Sstevel@tonic-gate *
437c478bd9Sstevel@tonic-gate * To handle archive files created in SunOS 4.1 system on Solaris, the
447c478bd9Sstevel@tonic-gate * following changes were made:
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * 1. file.c/writefile()
477c478bd9Sstevel@tonic-gate * Before writing each member files into the output
487c478bd9Sstevel@tonic-gate * archive file, ar_name[] is checked. If it is NULL,
497c478bd9Sstevel@tonic-gate * it means that the original archive header for this
507c478bd9Sstevel@tonic-gate * member was incompatible with Solaris format.
517c478bd9Sstevel@tonic-gate *
527c478bd9Sstevel@tonic-gate * The original Solaris ar command ended up having
537c478bd9Sstevel@tonic-gate * NULL name for the header. The change here uses the
547c478bd9Sstevel@tonic-gate * ar_rawname, which is much closer to the original
557c478bd9Sstevel@tonic-gate * name.
567c478bd9Sstevel@tonic-gate *
577c478bd9Sstevel@tonic-gate * 2. cmd.c
587c478bd9Sstevel@tonic-gate * For the p command, the code used to use only ar_longname
597c478bd9Sstevel@tonic-gate * to seach the matching name. The member is set to NULL
607c478bd9Sstevel@tonic-gate * if the archive member header was incompatible.
617c478bd9Sstevel@tonic-gate * The ar_rawname is also used to find the matching member name.
627c478bd9Sstevel@tonic-gate *
637c478bd9Sstevel@tonic-gate * For commands to update the archive file, we do not
647c478bd9Sstevel@tonic-gate * use ar_rawname, and just use the ar_longname. The commands are
657c478bd9Sstevel@tonic-gate * r (replace), m (modify the position) and d (delete).
667c478bd9Sstevel@tonic-gate */
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #include "inc.h"
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
71*ba7866cdSAli Bahrami * Forward Declarations
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate static void ar_select(int *, unsigned long);
74*ba7866cdSAli Bahrami static void cleanup(Cmd_info *);
757c478bd9Sstevel@tonic-gate static int create_extract(ARFILE *, int, int, Cmd_info *);
76*ba7866cdSAli Bahrami static char *match(char *, Cmd_info *);
77*ba7866cdSAli Bahrami static void mesg(int, char *, Cmd_info *);
78*ba7866cdSAli Bahrami static void movefil(ARFILE *, struct stat *);
79*ba7866cdSAli Bahrami static FILE *stats(char *, struct stat *);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate * Commands
837c478bd9Sstevel@tonic-gate */
84*ba7866cdSAli Bahrami void
rcmd(Cmd_info * cmd_info)857c478bd9Sstevel@tonic-gate rcmd(Cmd_info *cmd_info)
867c478bd9Sstevel@tonic-gate {
87d6555420Smike_s FILE *f;
88d6555420Smike_s ARFILE *fileptr;
89d6555420Smike_s ARFILE *abifile = NULL;
90d6555420Smike_s ARFILE *backptr = NULL;
917c478bd9Sstevel@tonic-gate ARFILE *endptr;
927c478bd9Sstevel@tonic-gate ARFILE *moved_files;
937c478bd9Sstevel@tonic-gate ARFILE *prev_entry, *new_listhead, *new_listend;
947c478bd9Sstevel@tonic-gate int deleted;
957c478bd9Sstevel@tonic-gate struct stat stbuf;
967c478bd9Sstevel@tonic-gate char *gfile;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate new_listhead = NULL;
997c478bd9Sstevel@tonic-gate new_listend = NULL;
1007c478bd9Sstevel@tonic-gate prev_entry = NULL;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate for (fileptr = getfile(cmd_info);
1037c478bd9Sstevel@tonic-gate fileptr; fileptr = getfile(cmd_info)) {
1047c478bd9Sstevel@tonic-gate deleted = 0;
1057c478bd9Sstevel@tonic-gate if (!abifile && cmd_info->ponam &&
1067c478bd9Sstevel@tonic-gate strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
1077c478bd9Sstevel@tonic-gate abifile = fileptr;
1087c478bd9Sstevel@tonic-gate else if (!abifile)
1097c478bd9Sstevel@tonic-gate backptr = fileptr;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (cmd_info->namc == 0 ||
1127c478bd9Sstevel@tonic-gate (gfile = match(fileptr->ar_longname, cmd_info)) != NULL) {
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * NOTE:
1157c478bd9Sstevel@tonic-gate * Refer to "Incompatible Archive Header"
1167c478bd9Sstevel@tonic-gate * blocked comment at the beginning of this file.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate f = stats(gfile, &stbuf); /* gfile is set by match */
1197c478bd9Sstevel@tonic-gate if (f == NULL) {
120*ba7866cdSAli Bahrami if (cmd_info->namc) {
121*ba7866cdSAli Bahrami int err = errno;
122*ba7866cdSAli Bahrami (void) fprintf(stderr,
123*ba7866cdSAli Bahrami MSG_INTL(MSG_SYS_OPEN),
124*ba7866cdSAli Bahrami gfile, strerror(err));
125*ba7866cdSAli Bahrami }
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * Created
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate mesg('c', gfile, cmd_info);
1307c478bd9Sstevel@tonic-gate } else {
131*ba7866cdSAli Bahrami if ((cmd_info->opt_flgs & u_FLAG) &&
1327c478bd9Sstevel@tonic-gate stbuf.st_mtime <= fileptr->ar_date) {
1337c478bd9Sstevel@tonic-gate (void) fclose(f);
1347c478bd9Sstevel@tonic-gate continue;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * Replaced
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate mesg('r', fileptr->ar_longname, cmd_info);
1407c478bd9Sstevel@tonic-gate movefil(fileptr, &stbuf);
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * Clear the previous contents.
1437c478bd9Sstevel@tonic-gate */
144*ba7866cdSAli Bahrami if (fileptr->ar_flag & F_ELFRAW) {
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * clear ar_elf
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate (void) elf_end(fileptr->ar_elf);
1497c478bd9Sstevel@tonic-gate fileptr->ar_elf = 0;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate /* clear 'ar_flag' */
152*ba7866cdSAli Bahrami fileptr->ar_flag &= ~F_ELFRAW;
153*ba7866cdSAli Bahrami
154*ba7866cdSAli Bahrami /*
155*ba7866cdSAli Bahrami * Defer reading contents until needed, and
156*ba7866cdSAli Bahrami * then use an in-kernel file-to-file transfer
157*ba7866cdSAli Bahrami * to avoid excessive in-process memory use.
158*ba7866cdSAli Bahrami */
159*ba7866cdSAli Bahrami fileptr->ar_contents = NULL;
160*ba7866cdSAli Bahrami
1617c478bd9Sstevel@tonic-gate if (fileptr->ar_pathname != NULL)
1627c478bd9Sstevel@tonic-gate free(fileptr->ar_pathname);
1637c478bd9Sstevel@tonic-gate if ((fileptr->ar_pathname =
1647c478bd9Sstevel@tonic-gate malloc(strlen(gfile) + 1)) == NULL) {
165*ba7866cdSAli Bahrami int err = errno;
166*ba7866cdSAli Bahrami (void) fprintf(stderr,
167*ba7866cdSAli Bahrami MSG_INTL(MSG_MALLOC),
168*ba7866cdSAli Bahrami strerror(err));
1697c478bd9Sstevel@tonic-gate exit(1);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate (void) strcpy(fileptr->ar_pathname, gfile);
1737c478bd9Sstevel@tonic-gate (void) fclose(f);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (cmd_info->ponam && (abifile != fileptr)) {
1767c478bd9Sstevel@tonic-gate deleted = 1;
1777c478bd9Sstevel@tonic-gate /* remove from archive list */
1787c478bd9Sstevel@tonic-gate if (prev_entry != NULL)
1797c478bd9Sstevel@tonic-gate prev_entry->ar_next = NULL;
1807c478bd9Sstevel@tonic-gate else
1817c478bd9Sstevel@tonic-gate listhead = NULL;
1827c478bd9Sstevel@tonic-gate listend = prev_entry;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /* add to moved list */
1857c478bd9Sstevel@tonic-gate if (new_listhead == NULL)
1867c478bd9Sstevel@tonic-gate new_listhead = fileptr;
1877c478bd9Sstevel@tonic-gate else
1887c478bd9Sstevel@tonic-gate new_listend->ar_next = fileptr;
1897c478bd9Sstevel@tonic-gate new_listend = fileptr;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate cmd_info->modified++;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate else
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * Unchaged
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate mesg('u', fileptr->ar_longname, cmd_info);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate if (deleted)
2017c478bd9Sstevel@tonic-gate deleted = 0;
2027c478bd9Sstevel@tonic-gate else
2037c478bd9Sstevel@tonic-gate prev_entry = fileptr;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate endptr = listend;
2077c478bd9Sstevel@tonic-gate cleanup(cmd_info);
2087c478bd9Sstevel@tonic-gate if (cmd_info->ponam && endptr &&
2097c478bd9Sstevel@tonic-gate (((moved_files = endptr->ar_next) != NULL) || new_listhead)) {
2107c478bd9Sstevel@tonic-gate if (!abifile) {
211*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
212*ba7866cdSAli Bahrami cmd_info->ponam);
2137c478bd9Sstevel@tonic-gate exit(2);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate endptr->ar_next = NULL;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * link new/moved files into archive entry list...
2197c478bd9Sstevel@tonic-gate * 1: prepend newlist to moved/appended list
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate if (new_listhead) {
2227c478bd9Sstevel@tonic-gate if (!moved_files)
2237c478bd9Sstevel@tonic-gate listend = new_listend;
2247c478bd9Sstevel@tonic-gate new_listend->ar_next = moved_files;
2257c478bd9Sstevel@tonic-gate moved_files = new_listhead;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate /* 2: insert at appropriate position... */
228*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & b_FLAG)
2297c478bd9Sstevel@tonic-gate abifile = backptr;
2307c478bd9Sstevel@tonic-gate if (abifile) {
2317c478bd9Sstevel@tonic-gate listend->ar_next = abifile->ar_next;
2327c478bd9Sstevel@tonic-gate abifile->ar_next = moved_files;
2337c478bd9Sstevel@tonic-gate } else {
2347c478bd9Sstevel@tonic-gate listend->ar_next = listhead;
2357c478bd9Sstevel@tonic-gate listhead = moved_files;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate listend = endptr;
2387c478bd9Sstevel@tonic-gate } else if (cmd_info->ponam && !abifile)
239*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
240*ba7866cdSAli Bahrami cmd_info->ponam);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
243*ba7866cdSAli Bahrami void
dcmd(Cmd_info * cmd_info)2447c478bd9Sstevel@tonic-gate dcmd(Cmd_info *cmd_info)
2457c478bd9Sstevel@tonic-gate {
246d6555420Smike_s ARFILE *fptr;
247d6555420Smike_s ARFILE *backptr = NULL;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info)) {
2507c478bd9Sstevel@tonic-gate if (match(fptr->ar_longname, cmd_info) != NULL) {
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * NOTE:
2537c478bd9Sstevel@tonic-gate * Refer to "Incompatible Archive Header"
2547c478bd9Sstevel@tonic-gate * blocked comment at the beginning of this file.
2557c478bd9Sstevel@tonic-gate */
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * Deleted
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate mesg('d', fptr->ar_longname, cmd_info);
2617c478bd9Sstevel@tonic-gate if (backptr == NULL) {
2627c478bd9Sstevel@tonic-gate listhead = NULL;
2637c478bd9Sstevel@tonic-gate listend = NULL;
2647c478bd9Sstevel@tonic-gate } else {
2657c478bd9Sstevel@tonic-gate backptr->ar_next = NULL;
2667c478bd9Sstevel@tonic-gate listend = backptr;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate cmd_info->modified = 1;
2697c478bd9Sstevel@tonic-gate } else {
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * Unchaged
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate mesg('u', fptr->ar_longname, cmd_info);
2747c478bd9Sstevel@tonic-gate backptr = fptr;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
279*ba7866cdSAli Bahrami void
xcmd(Cmd_info * cmd_info)2807c478bd9Sstevel@tonic-gate xcmd(Cmd_info *cmd_info)
2817c478bd9Sstevel@tonic-gate {
282d6555420Smike_s int f;
283d6555420Smike_s ARFILE *next;
2847c478bd9Sstevel@tonic-gate int rawname = 0;
285*ba7866cdSAli Bahrami long f_len = 0;
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate * If -T is specified, get the maximum file name length.
2897c478bd9Sstevel@tonic-gate */
290*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & T_FLAG) {
291*ba7866cdSAli Bahrami f_len = pathconf(MSG_ORIG(MSG_STR_PERIOD), _PC_NAME_MAX);
2927c478bd9Sstevel@tonic-gate if (f_len == -1) {
293*ba7866cdSAli Bahrami int err = errno;
294*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_PATHCONF),
295*ba7866cdSAli Bahrami strerror(err));
2967c478bd9Sstevel@tonic-gate exit(1);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
3007c478bd9Sstevel@tonic-gate if ((next->ar_longname[0] == 0) && (next->ar_rawname[0] != 0))
3017c478bd9Sstevel@tonic-gate rawname = 1;
3027c478bd9Sstevel@tonic-gate if (cmd_info->namc == 0 ||
3037c478bd9Sstevel@tonic-gate match(next->ar_longname, cmd_info) != NULL ||
3047c478bd9Sstevel@tonic-gate match(next->ar_rawname, cmd_info) != NULL) {
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate * NOTE:
3077c478bd9Sstevel@tonic-gate * Refer to "Incompatible Archive Header"
3087c478bd9Sstevel@tonic-gate * blocked comment at the beginning of this file.
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate f = create_extract(next, rawname, f_len, cmd_info);
3117c478bd9Sstevel@tonic-gate if (f >= 0) {
3127c478bd9Sstevel@tonic-gate if (rawname) {
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate * eXtracted
3157c478bd9Sstevel@tonic-gate */
3167c478bd9Sstevel@tonic-gate mesg('x', next->ar_rawname, cmd_info);
3177c478bd9Sstevel@tonic-gate if (write(f, next->ar_contents,
3187c478bd9Sstevel@tonic-gate (unsigned)next->ar_size) !=
3197c478bd9Sstevel@tonic-gate next->ar_size) {
320*ba7866cdSAli Bahrami int err = errno;
321*ba7866cdSAli Bahrami (void) fprintf(stderr,
322*ba7866cdSAli Bahrami MSG_INTL(MSG_SYS_WRITE),
323*ba7866cdSAli Bahrami next->ar_rawname,
324*ba7866cdSAli Bahrami strerror(err));
3257c478bd9Sstevel@tonic-gate exit(1);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate } else {
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * eXtracted
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate mesg('x', next->ar_longname, cmd_info);
3327c478bd9Sstevel@tonic-gate if (write(f, next->ar_contents,
3337c478bd9Sstevel@tonic-gate (unsigned)next->ar_size) !=
3347c478bd9Sstevel@tonic-gate next->ar_size) {
335*ba7866cdSAli Bahrami int err = errno;
336*ba7866cdSAli Bahrami (void) fprintf(stderr,
337*ba7866cdSAli Bahrami MSG_INTL(MSG_SYS_WRITE),
338*ba7866cdSAli Bahrami next->ar_longname,
339*ba7866cdSAli Bahrami strerror(err));
3407c478bd9Sstevel@tonic-gate exit(1);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate (void) close(f);
3447c478bd9Sstevel@tonic-gate } else
3457c478bd9Sstevel@tonic-gate exit(1);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate rawname = 0;
3487c478bd9Sstevel@tonic-gate } /* for */
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
351*ba7866cdSAli Bahrami void
pcmd(Cmd_info * cmd_info)3527c478bd9Sstevel@tonic-gate pcmd(Cmd_info *cmd_info)
3537c478bd9Sstevel@tonic-gate {
354d6555420Smike_s ARFILE *next;
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
3577c478bd9Sstevel@tonic-gate if (cmd_info->namc == 0 ||
3587c478bd9Sstevel@tonic-gate match(next->ar_longname, cmd_info) != NULL ||
3597c478bd9Sstevel@tonic-gate match(next->ar_rawname, cmd_info) != NULL) {
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * NOTE:
3627c478bd9Sstevel@tonic-gate * Refer to "Incompatible Archive Header"
3637c478bd9Sstevel@tonic-gate * blocked comment at the beginning of this file.
3647c478bd9Sstevel@tonic-gate */
365*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & v_FLAG) {
3667c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
367*ba7866cdSAli Bahrami MSG_ORIG(MSG_FMT_P_TITLE),
368*ba7866cdSAli Bahrami next->ar_longname);
3697c478bd9Sstevel@tonic-gate (void) fflush(stdout);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate (void) fwrite(next->ar_contents, sizeof (char),
3727c478bd9Sstevel@tonic-gate next->ar_size, stdout);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate
377*ba7866cdSAli Bahrami void
mcmd(Cmd_info * cmd_info)3787c478bd9Sstevel@tonic-gate mcmd(Cmd_info *cmd_info)
3797c478bd9Sstevel@tonic-gate {
380d6555420Smike_s ARFILE *fileptr;
381d6555420Smike_s ARFILE *abifile = NULL;
382d6555420Smike_s ARFILE *tmphead = NULL;
383d6555420Smike_s ARFILE *tmpend = NULL;
3847c478bd9Sstevel@tonic-gate ARFILE *backptr1 = NULL;
3857c478bd9Sstevel@tonic-gate ARFILE *backptr2 = NULL;
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate for (fileptr = getfile(cmd_info);
3887c478bd9Sstevel@tonic-gate fileptr; fileptr = getfile(cmd_info)) {
3897c478bd9Sstevel@tonic-gate if (match(fileptr->ar_longname, cmd_info) != NULL) {
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate * position Modified
3927c478bd9Sstevel@tonic-gate */
3937c478bd9Sstevel@tonic-gate mesg('m', fileptr->ar_longname, cmd_info);
3947c478bd9Sstevel@tonic-gate if (tmphead)
3957c478bd9Sstevel@tonic-gate tmpend->ar_next = fileptr;
3967c478bd9Sstevel@tonic-gate else
3977c478bd9Sstevel@tonic-gate tmphead = fileptr;
3987c478bd9Sstevel@tonic-gate tmpend = fileptr;
3997c478bd9Sstevel@tonic-gate if (backptr1) {
4007c478bd9Sstevel@tonic-gate listend = backptr1;
4017c478bd9Sstevel@tonic-gate listend->ar_next = NULL;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate else
4047c478bd9Sstevel@tonic-gate listhead = NULL;
4057c478bd9Sstevel@tonic-gate continue;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * position Unchaged
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate mesg('u', fileptr->ar_longname, cmd_info);
4117c478bd9Sstevel@tonic-gate backptr1 = fileptr;
4127c478bd9Sstevel@tonic-gate if (cmd_info->ponam && !abifile) {
4137c478bd9Sstevel@tonic-gate if (strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
4147c478bd9Sstevel@tonic-gate abifile = fileptr;
4157c478bd9Sstevel@tonic-gate else
4167c478bd9Sstevel@tonic-gate backptr2 = fileptr;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate if (!tmphead)
421*ba7866cdSAli Bahrami return;
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate if (!cmd_info->ponam)
4247c478bd9Sstevel@tonic-gate listend->ar_next = tmphead;
4257c478bd9Sstevel@tonic-gate else {
4267c478bd9Sstevel@tonic-gate if (!abifile) {
427*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
428*ba7866cdSAli Bahrami cmd_info->ponam);
4297c478bd9Sstevel@tonic-gate exit(2);
4307c478bd9Sstevel@tonic-gate }
431*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & b_FLAG)
4327c478bd9Sstevel@tonic-gate abifile = backptr2;
4337c478bd9Sstevel@tonic-gate if (abifile) {
4347c478bd9Sstevel@tonic-gate tmpend->ar_next = abifile->ar_next;
4357c478bd9Sstevel@tonic-gate abifile->ar_next = tmphead;
4367c478bd9Sstevel@tonic-gate } else {
4377c478bd9Sstevel@tonic-gate tmphead->ar_next = listhead;
4387c478bd9Sstevel@tonic-gate listhead = tmphead;
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate (cmd_info->modified)++;
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
444*ba7866cdSAli Bahrami void
tcmd(Cmd_info * cmd_info)4457c478bd9Sstevel@tonic-gate tcmd(Cmd_info *cmd_info)
4467c478bd9Sstevel@tonic-gate {
447d6555420Smike_s ARFILE *next;
448d6555420Smike_s int **mp;
4497c478bd9Sstevel@tonic-gate char buf[DATESIZE];
450*ba7866cdSAli Bahrami int m1[] = {1, S_IRUSR, 'r', '-'};
451*ba7866cdSAli Bahrami int m2[] = {1, S_IWUSR, 'w', '-'};
452*ba7866cdSAli Bahrami int m3[] = {2, S_ISUID, 's', S_IXUSR, 'x', '-'};
453*ba7866cdSAli Bahrami int m4[] = {1, S_IRGRP, 'r', '-'};
454*ba7866cdSAli Bahrami int m5[] = {1, S_IWGRP, 'w', '-'};
455*ba7866cdSAli Bahrami int m6[] = {2, S_ISGID, 's', S_IXGRP, 'x', '-'};
456*ba7866cdSAli Bahrami int m7[] = {1, S_IROTH, 'r', '-'};
457*ba7866cdSAli Bahrami int m8[] = {1, S_IWOTH, 'w', '-'};
458*ba7866cdSAli Bahrami int m9[] = {2, S_ISVTX, 't', S_IXOTH, 'x', '-'};
4597c478bd9Sstevel@tonic-gate int *m[10];
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate m[0] = m1;
4627c478bd9Sstevel@tonic-gate m[1] = m2;
4637c478bd9Sstevel@tonic-gate m[2] = m3;
4647c478bd9Sstevel@tonic-gate m[3] = m4;
4657c478bd9Sstevel@tonic-gate m[4] = m5;
4667c478bd9Sstevel@tonic-gate m[5] = m6;
4677c478bd9Sstevel@tonic-gate m[6] = m7;
4687c478bd9Sstevel@tonic-gate m[7] = m8;
4697c478bd9Sstevel@tonic-gate m[8] = m9;
4707c478bd9Sstevel@tonic-gate m[9] = 0;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
4737c478bd9Sstevel@tonic-gate if (cmd_info->namc == 0 ||
4747c478bd9Sstevel@tonic-gate match(next->ar_longname, cmd_info) != NULL ||
4757c478bd9Sstevel@tonic-gate match(next->ar_rawname, cmd_info) != NULL) {
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate * NOTE:
4787c478bd9Sstevel@tonic-gate * Refer to "Incompatible Archive Header"
4797c478bd9Sstevel@tonic-gate * blocked comment at the beginning of this file.
4807c478bd9Sstevel@tonic-gate */
481*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & v_FLAG) {
4827c478bd9Sstevel@tonic-gate for (mp = &m[0]; mp < &m[9]; )
4837c478bd9Sstevel@tonic-gate ar_select(*mp++, next->ar_mode);
4847c478bd9Sstevel@tonic-gate
485*ba7866cdSAli Bahrami (void) fprintf(stdout, MSG_ORIG(MSG_FMT_T_IDSZ),
486*ba7866cdSAli Bahrami next->ar_uid, next->ar_gid,
487*ba7866cdSAli Bahrami EC_XWORD(next->ar_size));
4887c478bd9Sstevel@tonic-gate if ((strftime(buf,
489*ba7866cdSAli Bahrami DATESIZE, MSG_ORIG(MSG_FMT_T_DATE),
4907c478bd9Sstevel@tonic-gate localtime(&(next->ar_date)))) == 0) {
491*ba7866cdSAli Bahrami (void) fprintf(stderr,
492*ba7866cdSAli Bahrami MSG_INTL(MSG_LOCALTIME));
4937c478bd9Sstevel@tonic-gate exit(1);
4947c478bd9Sstevel@tonic-gate }
495*ba7866cdSAli Bahrami (void) fprintf(stdout,
496*ba7866cdSAli Bahrami MSG_ORIG(MSG_FMT_SPSTRSP), buf);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate if ((next->ar_longname[0] == 0) &&
4997c478bd9Sstevel@tonic-gate (next->ar_rawname[0] != 0))
5007c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
501*ba7866cdSAli Bahrami MSG_ORIG(MSG_FMT_STRNL),
502*ba7866cdSAli Bahrami trim(next->ar_rawname));
5037c478bd9Sstevel@tonic-gate else
5047c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
505*ba7866cdSAli Bahrami MSG_ORIG(MSG_FMT_STRNL),
506*ba7866cdSAli Bahrami trim(next->ar_longname));
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate } /* for */
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
511*ba7866cdSAli Bahrami void
qcmd(Cmd_info * cmd_info)5127c478bd9Sstevel@tonic-gate qcmd(Cmd_info *cmd_info)
5137c478bd9Sstevel@tonic-gate {
514d6555420Smike_s ARFILE *fptr;
5157c478bd9Sstevel@tonic-gate
516*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & (a_FLAG | b_FLAG)) {
517*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_USAGE_05));
5187c478bd9Sstevel@tonic-gate exit(1);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info))
5217c478bd9Sstevel@tonic-gate ;
5227c478bd9Sstevel@tonic-gate cleanup(cmd_info);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate * Supplementary functions
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate static char *
match(char * file,Cmd_info * cmd_info)5297c478bd9Sstevel@tonic-gate match(char *file, Cmd_info *cmd_info)
5307c478bd9Sstevel@tonic-gate {
531d6555420Smike_s int i;
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate for (i = 0; i < cmd_info->namc; i++) {
5347c478bd9Sstevel@tonic-gate if (cmd_info->namv[i] == 0)
5357c478bd9Sstevel@tonic-gate continue;
5367c478bd9Sstevel@tonic-gate if (strcmp(trim(cmd_info->namv[i]), file) == 0) {
5377c478bd9Sstevel@tonic-gate file = cmd_info->namv[i];
5387c478bd9Sstevel@tonic-gate cmd_info->namv[i] = 0;
5397c478bd9Sstevel@tonic-gate return (file);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate return (NULL);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * puts the file which was in the list in the linked list
5477c478bd9Sstevel@tonic-gate */
5487c478bd9Sstevel@tonic-gate static void
cleanup(Cmd_info * cmd_info)5497c478bd9Sstevel@tonic-gate cleanup(Cmd_info *cmd_info)
5507c478bd9Sstevel@tonic-gate {
551d6555420Smike_s int i;
552d6555420Smike_s FILE *f;
553d6555420Smike_s ARFILE *fileptr;
5547c478bd9Sstevel@tonic-gate struct stat stbuf;
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate for (i = 0; i < cmd_info->namc; i++) {
5577c478bd9Sstevel@tonic-gate if (cmd_info->namv[i] == 0)
5587c478bd9Sstevel@tonic-gate continue;
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate * Appended
5617c478bd9Sstevel@tonic-gate */
5627c478bd9Sstevel@tonic-gate mesg('a', cmd_info->namv[i], cmd_info);
5637c478bd9Sstevel@tonic-gate f = stats(cmd_info->namv[i], &stbuf);
564*ba7866cdSAli Bahrami if (f == NULL) {
565*ba7866cdSAli Bahrami int err = errno;
566*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN),
567*ba7866cdSAli Bahrami cmd_info->namv[i], strerror(err));
568*ba7866cdSAli Bahrami } else {
5697c478bd9Sstevel@tonic-gate fileptr = newfile();
5707c478bd9Sstevel@tonic-gate /* if short name */
5717c478bd9Sstevel@tonic-gate (void) strncpy(fileptr->ar_name,
5727c478bd9Sstevel@tonic-gate trim(cmd_info->namv[i]), SNAME);
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate if ((fileptr->ar_longname =
5757c478bd9Sstevel@tonic-gate malloc(strlen(trim(cmd_info->namv[i])) + 1)) ==
5767c478bd9Sstevel@tonic-gate NULL) {
577*ba7866cdSAli Bahrami int err = errno;
578*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
579*ba7866cdSAli Bahrami strerror(err));
5807c478bd9Sstevel@tonic-gate exit(1);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate (void) strcpy(fileptr->ar_longname,
5847c478bd9Sstevel@tonic-gate trim(cmd_info->namv[i]));
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate if ((fileptr->ar_pathname =
5877c478bd9Sstevel@tonic-gate malloc(strlen(cmd_info->namv[i]) + 1)) == NULL) {
588*ba7866cdSAli Bahrami int err = errno;
589*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
590*ba7866cdSAli Bahrami strerror(err));
5917c478bd9Sstevel@tonic-gate exit(1);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate (void) strcpy(fileptr->ar_pathname, cmd_info->namv[i]);
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate movefil(fileptr, &stbuf);
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate /* clear 'ar_flag' */
599*ba7866cdSAli Bahrami fileptr->ar_flag &= ~F_ELFRAW;
6007c478bd9Sstevel@tonic-gate
601*ba7866cdSAli Bahrami /*
602*ba7866cdSAli Bahrami * Defer reading contents until needed, and then use
603*ba7866cdSAli Bahrami * an in-kernel file-to-file transfer to avoid
604*ba7866cdSAli Bahrami * excessive in-process memory use.
605*ba7866cdSAli Bahrami */
606*ba7866cdSAli Bahrami fileptr->ar_contents = NULL;
607*ba7866cdSAli Bahrami
6087c478bd9Sstevel@tonic-gate (void) fclose(f);
6097c478bd9Sstevel@tonic-gate (cmd_info->modified)++;
6107c478bd9Sstevel@tonic-gate cmd_info->namv[i] = 0;
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate * insert the file 'file' into the temporary file
6177c478bd9Sstevel@tonic-gate */
6187c478bd9Sstevel@tonic-gate static void
movefil(ARFILE * fileptr,struct stat * stbuf)6197c478bd9Sstevel@tonic-gate movefil(ARFILE *fileptr, struct stat *stbuf)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate fileptr->ar_size = stbuf->st_size;
6227c478bd9Sstevel@tonic-gate fileptr->ar_date = stbuf->st_mtime;
6237c478bd9Sstevel@tonic-gate fileptr->ar_mode = stbuf->st_mode;
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate * The format of an 'ar' file includes a 6 character
6277c478bd9Sstevel@tonic-gate * decimal string to contain the uid.
6287c478bd9Sstevel@tonic-gate *
6297c478bd9Sstevel@tonic-gate * If the uid or gid is too big to fit, then set it to
6307c478bd9Sstevel@tonic-gate * nobody (for want of a better value). Clear the
6317c478bd9Sstevel@tonic-gate * setuid/setgid bits in the mode to avoid setuid nobody
6327c478bd9Sstevel@tonic-gate * or setgid nobody files unexpectedly coming into existence.
6337c478bd9Sstevel@tonic-gate */
6347c478bd9Sstevel@tonic-gate if ((fileptr->ar_uid = stbuf->st_uid) > 999999) {
6357c478bd9Sstevel@tonic-gate fileptr->ar_uid = UID_NOBODY;
6367c478bd9Sstevel@tonic-gate if (S_ISREG(fileptr->ar_mode))
6377c478bd9Sstevel@tonic-gate fileptr->ar_mode &= ~S_ISUID;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate if ((fileptr->ar_gid = stbuf->st_gid) > 999999) {
6407c478bd9Sstevel@tonic-gate fileptr->ar_gid = GID_NOBODY;
6417c478bd9Sstevel@tonic-gate if (S_ISREG(fileptr->ar_mode))
6427c478bd9Sstevel@tonic-gate fileptr->ar_mode &= ~S_ISGID;
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate static FILE *
stats(char * file,struct stat * stbuf)6477c478bd9Sstevel@tonic-gate stats(char *file, struct stat *stbuf)
6487c478bd9Sstevel@tonic-gate {
649d6555420Smike_s FILE *f;
6507c478bd9Sstevel@tonic-gate
651*ba7866cdSAli Bahrami f = fopen(file, MSG_ORIG(MSG_STR_LCR));
6527c478bd9Sstevel@tonic-gate if (f == NULL)
6537c478bd9Sstevel@tonic-gate return (f);
6547c478bd9Sstevel@tonic-gate if (stat(file, stbuf) < 0) {
6557c478bd9Sstevel@tonic-gate (void) fclose(f);
6567c478bd9Sstevel@tonic-gate return (NULL);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate return (f);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Used by xcmd()
6637c478bd9Sstevel@tonic-gate */
6647c478bd9Sstevel@tonic-gate int
create_extract(ARFILE * a,int rawname,int f_len,Cmd_info * cmd_info)6657c478bd9Sstevel@tonic-gate create_extract(ARFILE *a, int rawname, int f_len, Cmd_info *cmd_info)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate int f;
6697c478bd9Sstevel@tonic-gate char *f_name;
6707c478bd9Sstevel@tonic-gate char *dup = NULL;
6717c478bd9Sstevel@tonic-gate if (rawname)
6727c478bd9Sstevel@tonic-gate f_name = a->ar_rawname;
6737c478bd9Sstevel@tonic-gate else
6747c478bd9Sstevel@tonic-gate f_name = a->ar_longname;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate * If -T is specified, check the file length.
6787c478bd9Sstevel@tonic-gate */
679*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & T_FLAG) {
6807c478bd9Sstevel@tonic-gate int len;
6817c478bd9Sstevel@tonic-gate len = strlen(f_name);
6827c478bd9Sstevel@tonic-gate if (f_len <= len) {
6837c478bd9Sstevel@tonic-gate dup = malloc(f_len+1);
6847c478bd9Sstevel@tonic-gate if (dup == NULL) {
685*ba7866cdSAli Bahrami int err = errno;
686*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
687*ba7866cdSAli Bahrami strerror(err));
6887c478bd9Sstevel@tonic-gate exit(1);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate (void) strncpy(dup, f_name, f_len);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate f_name = dup;
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate * Bug 4052067 - If a file to be extracted has the same
6977c478bd9Sstevel@tonic-gate * filename as the archive, the archive gets overwritten
6987c478bd9Sstevel@tonic-gate * which can lead to a corrupted archive or worse, a ufs
6997c478bd9Sstevel@tonic-gate * deadlock because libelf has mmap'ed the archive! We
7007c478bd9Sstevel@tonic-gate * can't rely on strcmp() to test for this case because
7017c478bd9Sstevel@tonic-gate * the archive could be prefixed with a partial or full
7027c478bd9Sstevel@tonic-gate * path (and we could be using the rawname from the archive)
7037c478bd9Sstevel@tonic-gate * This means we have to do the same thing we did for mv,
7047c478bd9Sstevel@tonic-gate * which is to explicitly check if the file we would extract
7057c478bd9Sstevel@tonic-gate * to is identical to the archive. Because part of this
7067c478bd9Sstevel@tonic-gate * test is essentially what the -C flag does, I've merged
7077c478bd9Sstevel@tonic-gate * the code together.
7087c478bd9Sstevel@tonic-gate */
7097c478bd9Sstevel@tonic-gate if (access(f_name, F_OK) != -1) {
7107c478bd9Sstevel@tonic-gate struct stat s1, s2;
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate * If -C is specified, this is an error anyway
7147c478bd9Sstevel@tonic-gate */
715*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & C_FLAG) {
716*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_OVERRIDE_WARN),
717*ba7866cdSAli Bahrami f_name);
7187c478bd9Sstevel@tonic-gate if (dup != NULL)
7197c478bd9Sstevel@tonic-gate free(dup);
7207c478bd9Sstevel@tonic-gate return (-1);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate * Okay, -C wasn't specified. However, now we do
7257c478bd9Sstevel@tonic-gate * the check to see if the archive would be overwritten
7267c478bd9Sstevel@tonic-gate * by extracting this file. stat() both objects and
7277c478bd9Sstevel@tonic-gate * test to see if their identical.
7287c478bd9Sstevel@tonic-gate */
7297c478bd9Sstevel@tonic-gate if ((stat(f_name, &s1) == 0) &&
7307c478bd9Sstevel@tonic-gate (stat(cmd_info->arnam, &s2) == 0)) {
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate if ((s1.st_dev == s2.st_dev) &&
7337c478bd9Sstevel@tonic-gate (s1.st_ino == s2.st_ino)) {
7347c478bd9Sstevel@tonic-gate
735*ba7866cdSAli Bahrami (void) fprintf(stderr,
736*ba7866cdSAli Bahrami MSG_INTL(MSG_OVERRIDE_WARN), f_name);
7377c478bd9Sstevel@tonic-gate if (dup != NULL)
7387c478bd9Sstevel@tonic-gate free(dup);
7397c478bd9Sstevel@tonic-gate return (-1);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate * Okay to create extraction file...
7467c478bd9Sstevel@tonic-gate */
7477c478bd9Sstevel@tonic-gate f = creat(f_name, (mode_t)a->ar_mode & 0777);
7487c478bd9Sstevel@tonic-gate if (f < 0) {
749*ba7866cdSAli Bahrami int err = errno;
750*ba7866cdSAli Bahrami (void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), f_name,
751*ba7866cdSAli Bahrami strerror(err));
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate * Created
7547c478bd9Sstevel@tonic-gate */
7557c478bd9Sstevel@tonic-gate mesg('c', f_name, cmd_info);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate if (dup)
7587c478bd9Sstevel@tonic-gate free(dup);
7597c478bd9Sstevel@tonic-gate return (f);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate static void
mesg(int c,char * file,Cmd_info * cmd_info)7637c478bd9Sstevel@tonic-gate mesg(int c, char *file, Cmd_info *cmd_info)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate #ifdef XPG4
7667c478bd9Sstevel@tonic-gate /*
7677c478bd9Sstevel@tonic-gate * XPG4 does not have any message defined for
7687c478bd9Sstevel@tonic-gate * 'c' operation.
7697c478bd9Sstevel@tonic-gate * In fact, XPG only defines messages for
7707c478bd9Sstevel@tonic-gate * d, r, a and x at the present. (03/05/'96)
7717c478bd9Sstevel@tonic-gate */
7727c478bd9Sstevel@tonic-gate if (c == 'c' || c == 'u' || c == 'm')
7737c478bd9Sstevel@tonic-gate return;
7747c478bd9Sstevel@tonic-gate #endif
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate * If 'u' is passed, convert it to 'c'.
7777c478bd9Sstevel@tonic-gate * 'u' makes more sense since the operation did not
7787c478bd9Sstevel@tonic-gate * do anything, Unchanged, but 'c' has been used so
7797c478bd9Sstevel@tonic-gate * I do no want to break the compatibility at this moment.
7807c478bd9Sstevel@tonic-gate * (03/05/'96).
7817c478bd9Sstevel@tonic-gate */
7827c478bd9Sstevel@tonic-gate if (c == 'u')
7837c478bd9Sstevel@tonic-gate c = 'c';
784*ba7866cdSAli Bahrami if (cmd_info->opt_flgs & v_FLAG)
7857c478bd9Sstevel@tonic-gate if (c != 'c')
786*ba7866cdSAli Bahrami (void) fprintf(stdout, MSG_ORIG(MSG_FMT_FILE), c, file);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate static void
ar_select(int * pairp,unsigned long mode)7907c478bd9Sstevel@tonic-gate ar_select(int *pairp, unsigned long mode)
7917c478bd9Sstevel@tonic-gate {
792d6555420Smike_s int n, *ap;
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate ap = pairp;
7957c478bd9Sstevel@tonic-gate n = *ap++;
7967c478bd9Sstevel@tonic-gate while (--n >= 0 && (mode & *ap++) == 0)
7977c478bd9Sstevel@tonic-gate ap++;
7987c478bd9Sstevel@tonic-gate (void) putchar(*ap);
7997c478bd9Sstevel@tonic-gate }
800