xref: /illumos-gate/usr/src/cmd/sgs/ar/common/cmd.c (revision fb25420ba8dbfa4c292d42c87555eee97a474854)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2021 Oxide Computer Company
24  */
25 
26 /*
27  *	Copyright (c) 1988 AT&T
28  *	  All Rights Reserved
29  *
30  */
31 
32 /*
33  * Incompatible Archive Header
34  *
35  * The archive file member header used in SunOS 4.1 archive files and
36  * Solaris archive files are incompatible. The header file is:
37  *	/usr/include/ar.h, struct ar_hdr.
38  * The member ar_name[] in Solaris comforms with Standard and the
39  * member name terminates with '/'. The SunOS's member does not terminate
40  * with '/' character. A bug 4046054 was filed:
41  *	The ar command in Solaris 2.5.1 is incompatible with archives
42  *	created on 4.x.
43  *
44  * To handle archive files created in SunOS 4.1 system on Solaris, the
45  * following changes were made:
46  *
47  *	1. file.c/writefile()
48  *		Before writing each member files into the output
49  *		archive file, ar_name[] is checked. If it is NULL,
50  *		it means that the original archive header for this
51  *		member was incompatible with Solaris format.
52  *
53  *		The original Solaris ar command ended up having
54  *		NULL name for the header. The change here uses the
55  *		ar_rawname, which is much closer to the original
56  *		name.
57  *
58  *	2. cmd.c
59  *		For the p command, the code used to use only ar_longname
60  *		to seach the matching name. The member is set to NULL
61  *		if the archive member header was incompatible.
62  *		The ar_rawname is also used to find the matching member name.
63  *
64  *		For commands to update the archive file, we do not
65  *		use ar_rawname, and just use the ar_longname. The commands are
66  *		r (replace), m (modify the position) and d (delete).
67  */
68 
69 #include "inc.h"
70 
71 /*
72  * Forward Declarations
73  */
74 static void	ar_select(int *, unsigned long);
75 static void	cleanup(Cmd_info *);
76 static int	create_extract(ARFILE *, int, int, Cmd_info *);
77 static char	*match(char *, Cmd_info *);
78 static void	mesg(int, char *, Cmd_info *);
79 static void	movefil(ARFILE *, struct stat *);
80 static FILE	*stats(char *, struct stat *);
81 
82 /*
83  * Commands
84  */
85 void
rcmd(Cmd_info * cmd_info)86 rcmd(Cmd_info *cmd_info)
87 {
88 	FILE		*f;
89 	ARFILE		*fileptr;
90 	ARFILE		*abifile = NULL;
91 	ARFILE		*backptr = NULL;
92 	ARFILE		*endptr;
93 	ARFILE		*moved_files;
94 	ARFILE		*prev_entry, *new_listhead, *new_listend;
95 	int		deleted;
96 	struct stat	stbuf;
97 	char		*gfile;
98 
99 	new_listhead  = NULL;
100 	new_listend   = NULL;
101 	prev_entry    = NULL;
102 
103 	for (fileptr = getfile(cmd_info);
104 	    fileptr; fileptr = getfile(cmd_info)) {
105 		deleted = 0;
106 		if (!abifile && cmd_info->ponam &&
107 		    strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
108 			abifile = fileptr;
109 		else if (!abifile)
110 			backptr = fileptr;
111 
112 		if (cmd_info->namc == 0 ||
113 		    (gfile = match(fileptr->ar_longname, cmd_info)) != NULL) {
114 			/*
115 			 * NOTE:
116 			 *	Refer to "Incompatible Archive Header"
117 			 *	blocked comment at the beginning of this file.
118 			 */
119 			f = stats(gfile, &stbuf); /* gfile is set by match */
120 			if (f == NULL) {
121 				if (cmd_info->namc) {
122 					int err = errno;
123 					(void) fprintf(stderr,
124 					    MSG_INTL(MSG_SYS_OPEN),
125 					    gfile, strerror(err));
126 				}
127 				/*
128 				 * Created
129 				 */
130 				mesg('c', gfile, cmd_info);
131 			} else {
132 				if ((cmd_info->opt_flgs & u_FLAG) &&
133 				    stbuf.st_mtime <= fileptr->ar_date) {
134 					(void) fclose(f);
135 					continue;
136 				}
137 				/*
138 				 * Replaced
139 				 */
140 				mesg('r', fileptr->ar_longname, cmd_info);
141 				movefil(fileptr, &stbuf);
142 				/*
143 				 * Clear the previous contents.
144 				 */
145 				if (fileptr->ar_flag & F_ELFRAW) {
146 					/*
147 					 * clear ar_elf
148 					 */
149 					(void) elf_end(fileptr->ar_elf);
150 					fileptr->ar_elf = 0;
151 				}
152 				/* clear 'ar_flag' */
153 				fileptr->ar_flag &= ~F_ELFRAW;
154 
155 				/*
156 				 * Defer reading contents until needed, and
157 				 * then use an in-kernel file-to-file transfer
158 				 * to avoid excessive in-process memory use.
159 				 */
160 				fileptr->ar_contents = NULL;
161 
162 				if (fileptr->ar_pathname != NULL)
163 					free(fileptr->ar_pathname);
164 				if ((fileptr->ar_pathname =
165 				    malloc(strlen(gfile) + 1)) == NULL) {
166 					int err = errno;
167 					(void) fprintf(stderr,
168 					    MSG_INTL(MSG_MALLOC),
169 					    strerror(err));
170 					exit(1);
171 				}
172 
173 				(void) strcpy(fileptr->ar_pathname, gfile);
174 				(void) fclose(f);
175 
176 				if (cmd_info->ponam && (abifile != fileptr)) {
177 					deleted = 1;
178 					/* remove from archive list */
179 					if (prev_entry != NULL)
180 						prev_entry->ar_next = NULL;
181 					else
182 						listhead = NULL;
183 					listend = prev_entry;
184 
185 					/* add to moved list */
186 					if (new_listhead == NULL)
187 						new_listhead = fileptr;
188 					else
189 						new_listend->ar_next = fileptr;
190 					new_listend = fileptr;
191 				}
192 				cmd_info->modified++;
193 			}
194 		}
195 		else
196 			/*
197 			 * Unchaged
198 			 */
199 			mesg('u', fileptr->ar_longname, cmd_info);
200 
201 		if (deleted)
202 			deleted = 0;
203 		else
204 			prev_entry = fileptr;
205 	}
206 
207 	endptr = listend;
208 	cleanup(cmd_info);
209 	if (cmd_info->ponam && endptr &&
210 	    (((moved_files = endptr->ar_next) != NULL) || new_listhead)) {
211 		if (!abifile) {
212 			(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
213 			    cmd_info->ponam);
214 			exit(2);
215 		}
216 		endptr->ar_next = NULL;
217 
218 		/*
219 		 * link new/moved files into archive entry list...
220 		 * 1: prepend newlist to moved/appended list
221 		 */
222 		if (new_listhead) {
223 			if (!moved_files)
224 				listend = new_listend;
225 			new_listend->ar_next = moved_files;
226 			moved_files = new_listhead;
227 		}
228 		/* 2: insert at appropriate position... */
229 		if (cmd_info->opt_flgs & b_FLAG)
230 			abifile = backptr;
231 		if (abifile) {
232 			listend->ar_next = abifile->ar_next;
233 			abifile->ar_next = moved_files;
234 		} else {
235 			listend->ar_next = listhead;
236 			listhead = moved_files;
237 		}
238 		listend = endptr;
239 	} else if (cmd_info->ponam && !abifile)
240 		(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
241 		    cmd_info->ponam);
242 }
243 
244 void
dcmd(Cmd_info * cmd_info)245 dcmd(Cmd_info *cmd_info)
246 {
247 	ARFILE	*fptr;
248 	ARFILE *backptr = NULL;
249 
250 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info)) {
251 		if (match(fptr->ar_longname, cmd_info) != NULL) {
252 			/*
253 			 * NOTE:
254 			 *	Refer to "Incompatible Archive Header"
255 			 *	blocked comment at the beginning of this file.
256 			 */
257 
258 			/*
259 			 * Deleted
260 			 */
261 			mesg('d', fptr->ar_longname, cmd_info);
262 			if (backptr == NULL) {
263 				listhead = NULL;
264 				listend = NULL;
265 			} else {
266 				backptr->ar_next = NULL;
267 				listend = backptr;
268 			}
269 			cmd_info->modified = 1;
270 		} else {
271 			/*
272 			 * Unchaged
273 			 */
274 			mesg('u', fptr->ar_longname, cmd_info);
275 			backptr = fptr;
276 		}
277 	}
278 }
279 
280 void
xcmd(Cmd_info * cmd_info)281 xcmd(Cmd_info *cmd_info)
282 {
283 	int	f;
284 	ARFILE	*next;
285 	int	rawname = 0;
286 	long	f_len = 0;
287 
288 	/*
289 	 * If -T is specified, get the maximum file name length.
290 	 */
291 	if (cmd_info->opt_flgs & T_FLAG) {
292 		f_len = pathconf(MSG_ORIG(MSG_STR_PERIOD), _PC_NAME_MAX);
293 		if (f_len == -1) {
294 			int err = errno;
295 			(void) fprintf(stderr, MSG_INTL(MSG_PATHCONF),
296 			    strerror(err));
297 			exit(1);
298 		}
299 	}
300 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
301 		if ((next->ar_longname[0] == 0) && (next->ar_rawname[0] != 0))
302 			rawname = 1;
303 		if (cmd_info->namc == 0 ||
304 		    match(next->ar_longname, cmd_info) != NULL ||
305 		    match(next->ar_rawname, cmd_info) != NULL) {
306 			/*
307 			 * NOTE:
308 			 *	Refer to "Incompatible Archive Header"
309 			 *	blocked comment at the beginning of this file.
310 			 */
311 			f = create_extract(next, rawname, f_len, cmd_info);
312 			if (f >= 0) {
313 				if (rawname) {
314 					/*
315 					 * eXtracted
316 					 */
317 					mesg('x', next->ar_rawname, cmd_info);
318 					if (write(f, next->ar_contents,
319 					    (unsigned)next->ar_size) !=
320 					    next->ar_size) {
321 						int err = errno;
322 						(void) fprintf(stderr,
323 						    MSG_INTL(MSG_SYS_WRITE),
324 						    next->ar_rawname,
325 						    strerror(err));
326 						exit(1);
327 					}
328 				} else {
329 					/*
330 					 * eXtracted
331 					 */
332 					mesg('x', next->ar_longname, cmd_info);
333 					if (write(f, next->ar_contents,
334 					    (unsigned)next->ar_size) !=
335 					    next->ar_size) {
336 						int err = errno;
337 						(void) fprintf(stderr,
338 						    MSG_INTL(MSG_SYS_WRITE),
339 						    next->ar_longname,
340 						    strerror(err));
341 						exit(1);
342 					}
343 				}
344 				(void) close(f);
345 			} else
346 				exit(1);
347 		}
348 		rawname = 0;
349 	} /* for */
350 }
351 
352 void
pcmd(Cmd_info * cmd_info)353 pcmd(Cmd_info *cmd_info)
354 {
355 	ARFILE	*next;
356 
357 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
358 		if (cmd_info->namc == 0 ||
359 		    match(next->ar_longname, cmd_info) != NULL ||
360 		    match(next->ar_rawname, cmd_info) != NULL) {
361 			/*
362 			 * NOTE:
363 			 *	Refer to "Incompatible Archive Header"
364 			 *	blocked comment at the beginning of this file.
365 			 */
366 			if (cmd_info->opt_flgs & v_FLAG) {
367 				(void) fprintf(stdout,
368 				    MSG_ORIG(MSG_FMT_P_TITLE),
369 				    next->ar_longname);
370 				(void) fflush(stdout);
371 			}
372 			(void) fwrite(next->ar_contents, sizeof (char),
373 			    next->ar_size, stdout);
374 		}
375 	}
376 }
377 
378 void
mcmd(Cmd_info * cmd_info)379 mcmd(Cmd_info *cmd_info)
380 {
381 	ARFILE	*fileptr;
382 	ARFILE	*abifile = NULL;
383 	ARFILE	*tmphead = NULL;
384 	ARFILE	*tmpend = NULL;
385 	ARFILE	*backptr1 = NULL;
386 	ARFILE	*backptr2 = NULL;
387 
388 	for (fileptr = getfile(cmd_info);
389 	    fileptr; fileptr = getfile(cmd_info)) {
390 		if (match(fileptr->ar_longname, cmd_info) != NULL) {
391 			/*
392 			 * position Modified
393 			 */
394 			mesg('m', fileptr->ar_longname, cmd_info);
395 			if (tmphead)
396 				tmpend->ar_next = fileptr;
397 			else
398 				tmphead = fileptr;
399 			tmpend = fileptr;
400 			if (backptr1) {
401 				listend = backptr1;
402 				listend->ar_next = NULL;
403 			}
404 			else
405 				listhead = NULL;
406 			continue;
407 		}
408 		/*
409 		 * position Unchaged
410 		 */
411 		mesg('u', fileptr->ar_longname, cmd_info);
412 		backptr1 = fileptr;
413 		if (cmd_info->ponam && !abifile) {
414 			if (strcmp(fileptr->ar_longname, cmd_info->ponam) == 0)
415 				abifile = fileptr;
416 			else
417 				backptr2 = fileptr;
418 		}
419 	}
420 
421 	if (!tmphead)
422 		return;
423 
424 	if (!cmd_info->ponam)
425 		listend->ar_next = tmphead;
426 	else {
427 		if (!abifile) {
428 			(void) fprintf(stderr, MSG_INTL(MSG_NOT_FOUND_POSNAM),
429 			    cmd_info->ponam);
430 			exit(2);
431 		}
432 		if (cmd_info->opt_flgs & b_FLAG)
433 			abifile = backptr2;
434 		if (abifile) {
435 			tmpend->ar_next = abifile->ar_next;
436 			abifile->ar_next = tmphead;
437 		} else {
438 			tmphead->ar_next = listhead;
439 			listhead = tmphead;
440 		}
441 	}
442 	(cmd_info->modified)++;
443 }
444 
445 void
tcmd(Cmd_info * cmd_info)446 tcmd(Cmd_info *cmd_info)
447 {
448 	ARFILE	*next;
449 	int	**mp;
450 	char   buf[DATESIZE];
451 	int m1[] = {1, S_IRUSR, 'r', '-'};
452 	int m2[] = {1, S_IWUSR, 'w', '-'};
453 	int m3[] = {2, S_ISUID, 's', S_IXUSR, 'x', '-'};
454 	int m4[] = {1, S_IRGRP, 'r', '-'};
455 	int m5[] = {1, S_IWGRP, 'w', '-'};
456 	int m6[] = {2, S_ISGID, 's', S_IXGRP, 'x', '-'};
457 	int m7[] = {1, S_IROTH, 'r', '-'};
458 	int m8[] = {1, S_IWOTH, 'w', '-'};
459 	int m9[] = {2, S_ISVTX, 't', S_IXOTH, 'x', '-'};
460 	int *m[10];
461 
462 	m[0] = m1;
463 	m[1] = m2;
464 	m[2] = m3;
465 	m[3] = m4;
466 	m[4] = m5;
467 	m[5] = m6;
468 	m[6] = m7;
469 	m[7] = m8;
470 	m[8] = m9;
471 	m[9] = 0;
472 
473 	for (next = getfile(cmd_info); next; next = getfile(cmd_info)) {
474 		if (cmd_info->namc == 0 ||
475 		    match(next->ar_longname, cmd_info) != NULL ||
476 		    match(next->ar_rawname, cmd_info) != NULL) {
477 			/*
478 			 * NOTE:
479 			 *	Refer to "Incompatible Archive Header"
480 			 *	blocked comment at the beginning of this file.
481 			 */
482 			if ((cmd_info->opt_flgs & (t_FLAG | v_FLAG)) ==
483 			    (t_FLAG | v_FLAG)) {
484 				for (mp = &m[0]; mp < &m[9]; )
485 					ar_select(*mp++, next->ar_mode);
486 
487 				(void) fprintf(stdout, MSG_ORIG(MSG_FMT_T_IDSZ),
488 				    next->ar_uid, next->ar_gid,
489 				    EC_XWORD(next->ar_size));
490 				if ((strftime(buf,
491 				    DATESIZE, MSG_ORIG(MSG_FMT_T_DATE),
492 				    localtime(&(next->ar_date)))) == 0) {
493 					(void) fprintf(stderr,
494 					    MSG_INTL(MSG_LOCALTIME));
495 					exit(1);
496 				}
497 				(void) fprintf(stdout,
498 				    MSG_ORIG(MSG_FMT_SPSTRSP), buf);
499 			}
500 			if (cmd_info->opt_flgs & t_FLAG) {
501 				if ((next->ar_longname[0] == 0) &&
502 				    (next->ar_rawname[0] != 0)) {
503 					(void) fprintf(stdout,
504 					    MSG_ORIG(MSG_FMT_STRNL),
505 					    trim(next->ar_rawname));
506 				} else {
507 					(void) fprintf(stdout,
508 					    MSG_ORIG(MSG_FMT_STRNL),
509 					    trim(next->ar_longname));
510 				}
511 			}
512 		}
513 	}
514 }
515 
516 void
qcmd(Cmd_info * cmd_info)517 qcmd(Cmd_info *cmd_info)
518 {
519 	ARFILE *fptr;
520 
521 	if (cmd_info->opt_flgs & (a_FLAG | b_FLAG)) {
522 		(void) fprintf(stderr, MSG_INTL(MSG_USAGE_Q_BAD_ARG));
523 		exit(1);
524 	}
525 	for (fptr = getfile(cmd_info); fptr; fptr = getfile(cmd_info))
526 		;
527 	cleanup(cmd_info);
528 }
529 
530 /*
531  * Supplementary functions
532  */
533 static char *
match(char * file,Cmd_info * cmd_info)534 match(char *file, Cmd_info *cmd_info)
535 {
536 	int i;
537 
538 	for (i = 0; i < cmd_info->namc; i++) {
539 		if (cmd_info->namv[i] == 0)
540 			continue;
541 		if (strcmp(trim(cmd_info->namv[i]), file) == 0) {
542 			file = cmd_info->namv[i];
543 			cmd_info->namv[i] = 0;
544 			return (file);
545 		}
546 	}
547 	return (NULL);
548 }
549 
550 /*
551  * puts the file which was in the list in the linked list
552  */
553 static void
cleanup(Cmd_info * cmd_info)554 cleanup(Cmd_info *cmd_info)
555 {
556 	int i;
557 	FILE	*f;
558 	ARFILE	*fileptr;
559 	struct stat stbuf;
560 
561 	for (i = 0; i < cmd_info->namc; i++) {
562 		if (cmd_info->namv[i] == 0)
563 			continue;
564 		/*
565 		 * Appended
566 		 */
567 		mesg('a', cmd_info->namv[i], cmd_info);
568 		f = stats(cmd_info->namv[i], &stbuf);
569 		if (f == NULL) {
570 			int err = errno;
571 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN),
572 			    cmd_info->namv[i], strerror(err));
573 		} else {
574 			fileptr = newfile();
575 			/* if short name */
576 			(void) strncpy(fileptr->ar_name,
577 			    trim(cmd_info->namv[i]), SNAME);
578 
579 			if ((fileptr->ar_longname =
580 			    malloc(strlen(trim(cmd_info->namv[i])) + 1)) ==
581 			    NULL) {
582 				int err = errno;
583 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
584 				    strerror(err));
585 				exit(1);
586 			}
587 
588 			(void) strcpy(fileptr->ar_longname,
589 			    trim(cmd_info->namv[i]));
590 
591 			if ((fileptr->ar_pathname =
592 			    malloc(strlen(cmd_info->namv[i]) + 1)) == NULL) {
593 				int err = errno;
594 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
595 				    strerror(err));
596 				exit(1);
597 			}
598 
599 			(void) strcpy(fileptr->ar_pathname, cmd_info->namv[i]);
600 
601 			movefil(fileptr, &stbuf);
602 
603 			/* clear 'ar_flag' */
604 			fileptr->ar_flag &= ~F_ELFRAW;
605 
606 			/*
607 			 * Defer reading contents until needed, and then use
608 			 * an in-kernel file-to-file transfer to avoid
609 			 * excessive in-process memory use.
610 			 */
611 			fileptr->ar_contents = NULL;
612 
613 			(void) fclose(f);
614 			(cmd_info->modified)++;
615 			cmd_info->namv[i] = 0;
616 		}
617 	}
618 }
619 
620 /*
621  * insert the file 'file' into the temporary file
622  */
623 static void
movefil(ARFILE * fileptr,struct stat * stbuf)624 movefil(ARFILE *fileptr, struct stat *stbuf)
625 {
626 	fileptr->ar_size = stbuf->st_size;
627 	fileptr->ar_date = stbuf->st_mtime;
628 	fileptr->ar_mode = stbuf->st_mode;
629 
630 	/*
631 	 * The format of an 'ar' file includes a 6 character
632 	 * decimal string to contain the uid.
633 	 *
634 	 * If the uid or gid is too big to fit, then set it to
635 	 * nobody (for want of a better value).  Clear the
636 	 * setuid/setgid bits in the mode to avoid setuid nobody
637 	 * or setgid nobody files unexpectedly coming into existence.
638 	 */
639 	if ((fileptr->ar_uid = stbuf->st_uid) > 999999) {
640 		fileptr->ar_uid = UID_NOBODY;
641 		if (S_ISREG(fileptr->ar_mode))
642 			fileptr->ar_mode &= ~S_ISUID;
643 	}
644 	if ((fileptr->ar_gid = stbuf->st_gid) > 999999) {
645 		fileptr->ar_gid = GID_NOBODY;
646 		if (S_ISREG(fileptr->ar_mode))
647 			fileptr->ar_mode &= ~S_ISGID;
648 	}
649 }
650 
651 static FILE *
stats(char * file,struct stat * stbuf)652 stats(char *file, struct stat *stbuf)
653 {
654 	FILE *f;
655 
656 	f = fopen(file, MSG_ORIG(MSG_STR_LCR));
657 	if (f == NULL)
658 		return (f);
659 	if (stat(file, stbuf) < 0) {
660 		(void) fclose(f);
661 		return (NULL);
662 	}
663 	return (f);
664 }
665 
666 /*
667  * Used by xcmd()
668  */
669 int
create_extract(ARFILE * a,int rawname,int f_len,Cmd_info * cmd_info)670 create_extract(ARFILE *a, int rawname, int f_len, Cmd_info *cmd_info)
671 {
672 
673 	int f;
674 	char *f_name;
675 	char *dup = NULL;
676 	if (rawname)
677 		f_name = a->ar_rawname;
678 	else
679 		f_name = a->ar_longname;
680 
681 	/*
682 	 * If -T is specified, check the file length.
683 	 */
684 	if (cmd_info->opt_flgs & T_FLAG) {
685 		int len;
686 		len = strlen(f_name);
687 		if (f_len <= len) {
688 			dup = malloc(f_len+1);
689 			if (dup == NULL) {
690 				int err = errno;
691 				(void) fprintf(stderr, MSG_INTL(MSG_MALLOC),
692 				    strerror(err));
693 				exit(1);
694 			}
695 			(void) strncpy(dup, f_name, f_len);
696 		}
697 		f_name = dup;
698 	}
699 
700 	/*
701 	 * Bug 4052067 - If a file to be extracted has the same
702 	 * filename as the archive, the archive gets overwritten
703 	 * which can lead to a corrupted archive or worse, a ufs
704 	 * deadlock because libelf has mmap'ed the archive!  We
705 	 * can't rely on strcmp() to test for this case because
706 	 * the archive could be prefixed with a partial or full
707 	 * path (and we could be using the rawname from the archive)
708 	 * This means we have to do the same thing we did for mv,
709 	 * which is to explicitly check if the file we would extract
710 	 * to is identical to the archive.  Because part of this
711 	 * test is essentially what the -C flag does, I've merged
712 	 * the code together.
713 	 */
714 	if (access(f_name, F_OK) != -1) {
715 		struct stat s1, s2;
716 
717 		/*
718 		 * If -C is specified, this is an error anyway
719 		 */
720 		if (cmd_info->opt_flgs & C_FLAG) {
721 			(void) fprintf(stderr, MSG_INTL(MSG_OVERRIDE_WARN),
722 			    f_name);
723 			if (dup != NULL)
724 				free(dup);
725 			return (-1);
726 		}
727 
728 		/*
729 		 * Okay, -C wasn't specified.  However, now we do
730 		 * the check to see if the archive would be overwritten
731 		 * by extracting this file.  stat() both objects and
732 		 * test to see if their identical.
733 		 */
734 		if ((stat(f_name, &s1) == 0) &&
735 		    (stat(cmd_info->arnam, &s2) == 0)) {
736 
737 			if ((s1.st_dev == s2.st_dev) &&
738 			    (s1.st_ino == s2.st_ino)) {
739 
740 				(void) fprintf(stderr,
741 				    MSG_INTL(MSG_OVERRIDE_WARN), f_name);
742 				if (dup != NULL)
743 					free(dup);
744 				return (-1);
745 			}
746 		}
747 	}
748 
749 	/*
750 	 * Okay to create extraction file...
751 	 */
752 	f = creat(f_name, (mode_t)a->ar_mode & 0777);
753 	if (f < 0) {
754 		int err = errno;
755 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), f_name,
756 		    strerror(err));
757 		/*
758 		 * Created
759 		 */
760 		mesg('c', f_name, cmd_info);
761 	}
762 	if (dup)
763 		free(dup);
764 	return (f);
765 }
766 
767 static void
mesg(int c,char * file,Cmd_info * cmd_info)768 mesg(int c, char *file, Cmd_info *cmd_info)
769 {
770 #ifdef XPG4
771 	/*
772 	 * XPG4 does not have any message defined for
773 	 * 'c' operation.
774 	 * In fact, XPG only defines messages for
775 	 *	d, r, a and x at the present. (03/05/'96)
776 	 */
777 	if (c == 'c' || c == 'u' || c == 'm')
778 		return;
779 #endif
780 	/*
781 	 * If 'u' is passed, convert it to 'c'.
782 	 * 'u' makes more sense since the operation did not
783 	 * do anything, Unchanged, but 'c' has been used so
784 	 * I do no want to break the compatibility at this moment.
785 	 * (03/05/'96).
786 	 */
787 	if (c == 'u')
788 		c = 'c';
789 	if (cmd_info->opt_flgs & v_FLAG)
790 		if (c != 'c')
791 			(void) fprintf(stdout, MSG_ORIG(MSG_FMT_FILE), c, file);
792 }
793 
794 static void
ar_select(int * pairp,unsigned long mode)795 ar_select(int *pairp, unsigned long mode)
796 {
797 	int n, *ap;
798 
799 	ap = pairp;
800 	n = *ap++;
801 	while (--n >= 0 && (mode & *ap++) == 0)
802 		ap++;
803 	(void) putchar(*ap);
804 }
805