xref: /onnv-gate/usr/src/cmd/sgs/ar/common/main.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * Copyright 1995-2003 Sun Microsystems, Inc.  All rights reserved.
27*0Sstevel@tonic-gate  * Use is subject to license terms.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include "inc.h"
33*0Sstevel@tonic-gate #include "extern.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate static char *arnam;
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * Function prototypes
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate static void setup(int, char **, Cmd_info *);
41*0Sstevel@tonic-gate static void setcom(Cmd_info *, int (*)());
42*0Sstevel@tonic-gate static void usage();
43*0Sstevel@tonic-gate static void sigexit();
44*0Sstevel@tonic-gate static int notfound(Cmd_info *);
45*0Sstevel@tonic-gate static void check_swap();
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #ifdef BROWSER
48*0Sstevel@tonic-gate extern  void    sbfocus_close();
49*0Sstevel@tonic-gate #endif
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define	OPTSTR	":a:b:i:vucsrdxtplmqVCTzM"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate int
54*0Sstevel@tonic-gate main(int argc, char **argv)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	register int i;
57*0Sstevel@tonic-gate 	int fd;
58*0Sstevel@tonic-gate 	Cmd_info *cmd_info;
59*0Sstevel@tonic-gate 	int ret;
60*0Sstevel@tonic-gate 	char *new = NULL;
61*0Sstevel@tonic-gate 	char *data = NULL;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
64*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
65*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	for (i = 0; signum[i]; i++)
70*0Sstevel@tonic-gate 		if (signal(signum[i], SIG_IGN) != SIG_IGN)
71*0Sstevel@tonic-gate 			(void) signal(signum[i], sigexit);
72*0Sstevel@tonic-gate 	/*
73*0Sstevel@tonic-gate 	 * Initialize cmd_info
74*0Sstevel@tonic-gate 	 */
75*0Sstevel@tonic-gate 	cmd_info = (Cmd_info *)calloc(1, sizeof (Cmd_info));
76*0Sstevel@tonic-gate 	if (cmd_info == NULL) {
77*0Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0);
78*0Sstevel@tonic-gate 		exit(1);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (argc < 2)
82*0Sstevel@tonic-gate 		usage();
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/*
85*0Sstevel@tonic-gate 	 * Option handling.
86*0Sstevel@tonic-gate 	 */
87*0Sstevel@tonic-gate 	if (argv[1][0] != '-') {
88*0Sstevel@tonic-gate 		new = (char *)malloc(strlen(argv[1]) + 2);
89*0Sstevel@tonic-gate 		if (new == NULL) {
90*0Sstevel@tonic-gate 			error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0);
91*0Sstevel@tonic-gate 			exit(1);
92*0Sstevel@tonic-gate 		}
93*0Sstevel@tonic-gate 		(void) strcpy(new, "-");
94*0Sstevel@tonic-gate 		(void) strcat(new, argv[1]);
95*0Sstevel@tonic-gate 		argv[1] = new;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	setup(argc, argv, cmd_info);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/*
100*0Sstevel@tonic-gate 	 * Check SWAP
101*0Sstevel@tonic-gate 	 */
102*0Sstevel@tonic-gate 	if (opt_FLAG((cmd_info), z_FLAG))
103*0Sstevel@tonic-gate 		check_swap();
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if (cmd_info->comfun == 0) {
106*0Sstevel@tonic-gate 		if (!(opt_FLAG((cmd_info), d_FLAG) ||
107*0Sstevel@tonic-gate 		    opt_FLAG(cmd_info, r_FLAG) ||
108*0Sstevel@tonic-gate 		    opt_FLAG(cmd_info, q_FLAG) || opt_FLAG(cmd_info, t_FLAG) ||
109*0Sstevel@tonic-gate 		    opt_FLAG(cmd_info, p_FLAG) || opt_FLAG(cmd_info, m_FLAG) ||
110*0Sstevel@tonic-gate 		    opt_FLAG(cmd_info, x_FLAG))) {
111*0Sstevel@tonic-gate 			error_message(USAGE_01_ERROR, PLAIN_ERROR, (char *)0);
112*0Sstevel@tonic-gate 			exit(1);
113*0Sstevel@tonic-gate 		}
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	cmd_info->modified = opt_FLAG(cmd_info, s_FLAG);
117*0Sstevel@tonic-gate 	fd = getaf(cmd_info);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if ((fd == -1) &&
120*0Sstevel@tonic-gate 	    (opt_FLAG(cmd_info, d_FLAG) || opt_FLAG(cmd_info, t_FLAG) ||
121*0Sstevel@tonic-gate 	    opt_FLAG(cmd_info, p_FLAG) || opt_FLAG(cmd_info, m_FLAG) ||
122*0Sstevel@tonic-gate 	    opt_FLAG(cmd_info, x_FLAG) ||
123*0Sstevel@tonic-gate 	    (opt_FLAG(cmd_info, r_FLAG) && (opt_FLAG(cmd_info, a_FLAG) ||
124*0Sstevel@tonic-gate 	    opt_FLAG(cmd_info, b_FLAG))))) {
125*0Sstevel@tonic-gate 		error_message(NOT_FOUND_01_ERROR,
126*0Sstevel@tonic-gate 		PLAIN_ERROR, (char *)0, arnam);
127*0Sstevel@tonic-gate 		exit(1);
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	(*cmd_info->comfun)(cmd_info);
131*0Sstevel@tonic-gate #ifdef BROWSER
132*0Sstevel@tonic-gate 	sb_data.fd = NULL;
133*0Sstevel@tonic-gate 	sb_data.failed = 0;
134*0Sstevel@tonic-gate #endif
135*0Sstevel@tonic-gate 	if (cmd_info->modified) {
136*0Sstevel@tonic-gate 		data = writefile(cmd_info);
137*0Sstevel@tonic-gate 	} else
138*0Sstevel@tonic-gate 		(void) close(fd);
139*0Sstevel@tonic-gate #ifdef BROWSER
140*0Sstevel@tonic-gate 	sbfocus_close(&sb_data);
141*0Sstevel@tonic-gate #endif
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	ret = notfound(cmd_info);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * Check SWAP
147*0Sstevel@tonic-gate 	 */
148*0Sstevel@tonic-gate 	if (opt_FLAG((cmd_info), z_FLAG))
149*0Sstevel@tonic-gate 		check_swap();
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	free(data);
152*0Sstevel@tonic-gate 	free(new);
153*0Sstevel@tonic-gate 	free(cmd_info);
154*0Sstevel@tonic-gate 	return (ret);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Option hadning function.
160*0Sstevel@tonic-gate  *	Using getopt(), following xcu4 convention.
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate static void
163*0Sstevel@tonic-gate setup(int argc, char *argv[], Cmd_info *cmd_info)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	int Vflag = 0;
166*0Sstevel@tonic-gate 	int c;
167*0Sstevel@tonic-gate 	int usage_err = 0;
168*0Sstevel@tonic-gate 	extern char *optarg;
169*0Sstevel@tonic-gate 	extern int optind;
170*0Sstevel@tonic-gate 	extern int optopt;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, OPTSTR)) != -1) {
173*0Sstevel@tonic-gate 		switch (c) {
174*0Sstevel@tonic-gate 		case 'a': /* position after named archive member file */
175*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= a_FLAG;
176*0Sstevel@tonic-gate 			cmd_info->ponam = trim(optarg);
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 		case 'b': /* position before named archive member file */
179*0Sstevel@tonic-gate 		case 'i': /* position before named archive member: same as b */
180*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= b_FLAG;
181*0Sstevel@tonic-gate 			cmd_info->ponam = trim(optarg);
182*0Sstevel@tonic-gate 			break;
183*0Sstevel@tonic-gate 		case 'c': /* supress messages */
184*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= c_FLAG;
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 		case 'd':
187*0Sstevel@tonic-gate 			/*
188*0Sstevel@tonic-gate 			 * key operation:
189*0Sstevel@tonic-gate 			 * delete files from the archive
190*0Sstevel@tonic-gate 			 */
191*0Sstevel@tonic-gate 			setcom(cmd_info, dcmd);
192*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= d_FLAG;
193*0Sstevel@tonic-gate 			break;
194*0Sstevel@tonic-gate 		case 'l': /* temporary directory */
195*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= l_FLAG;
196*0Sstevel@tonic-gate 			break;
197*0Sstevel@tonic-gate 		case 'm':
198*0Sstevel@tonic-gate 			/*
199*0Sstevel@tonic-gate 			 * key operation:
200*0Sstevel@tonic-gate 			 * move files to end of the archive
201*0Sstevel@tonic-gate 			 * or as indicated by position flag
202*0Sstevel@tonic-gate 			 */
203*0Sstevel@tonic-gate 			setcom(cmd_info, mcmd);
204*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= m_FLAG;
205*0Sstevel@tonic-gate 			break;
206*0Sstevel@tonic-gate 		case 'p':
207*0Sstevel@tonic-gate 			/*
208*0Sstevel@tonic-gate 			 * key operation:
209*0Sstevel@tonic-gate 			 * print files in the archive
210*0Sstevel@tonic-gate 			 */
211*0Sstevel@tonic-gate 			setcom(cmd_info, pcmd);
212*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= p_FLAG;
213*0Sstevel@tonic-gate 			break;
214*0Sstevel@tonic-gate 		case 'q':
215*0Sstevel@tonic-gate 			/*
216*0Sstevel@tonic-gate 			 * key operation:
217*0Sstevel@tonic-gate 			 * quickly append files to end of the archive
218*0Sstevel@tonic-gate 			 */
219*0Sstevel@tonic-gate 			setcom(cmd_info, qcmd);
220*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= q_FLAG;
221*0Sstevel@tonic-gate 			break;
222*0Sstevel@tonic-gate 		case 'r':
223*0Sstevel@tonic-gate 			/*
224*0Sstevel@tonic-gate 			 * key operation:
225*0Sstevel@tonic-gate 			 * replace or add files to the archive
226*0Sstevel@tonic-gate 			 */
227*0Sstevel@tonic-gate 			setcom(cmd_info, rcmd);
228*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= r_FLAG;
229*0Sstevel@tonic-gate 			break;
230*0Sstevel@tonic-gate 		case 's': /* force symbol table regeneration */
231*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= s_FLAG;
232*0Sstevel@tonic-gate 			break;
233*0Sstevel@tonic-gate 		case 't':
234*0Sstevel@tonic-gate 			/*
235*0Sstevel@tonic-gate 			 * key operation:
236*0Sstevel@tonic-gate 			 * print table of contents
237*0Sstevel@tonic-gate 			 */
238*0Sstevel@tonic-gate 			setcom(cmd_info, tcmd);
239*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= t_FLAG;
240*0Sstevel@tonic-gate 			break;
241*0Sstevel@tonic-gate 		case 'u': /* update: change archive dependent on file dates */
242*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= u_FLAG;
243*0Sstevel@tonic-gate 			break;
244*0Sstevel@tonic-gate 		case 'v': /* verbose */
245*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= v_FLAG;
246*0Sstevel@tonic-gate 			break;
247*0Sstevel@tonic-gate 		case 'x':
248*0Sstevel@tonic-gate 			/*
249*0Sstevel@tonic-gate 			 * key operation:
250*0Sstevel@tonic-gate 			 * extract files from the archive
251*0Sstevel@tonic-gate 			 */
252*0Sstevel@tonic-gate 			setcom(cmd_info, xcmd);
253*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= x_FLAG;
254*0Sstevel@tonic-gate 			break;
255*0Sstevel@tonic-gate 		case 'z':
256*0Sstevel@tonic-gate 			cmd_info->opt_flgs |= z_FLAG;
257*0Sstevel@tonic-gate 			break;
258*0Sstevel@tonic-gate 		case 'V':
259*0Sstevel@tonic-gate 			/*
260*0Sstevel@tonic-gate 			 * print version information.
261*0Sstevel@tonic-gate 			 * adjust command line access accounting
262*0Sstevel@tonic-gate 			 */
263*0Sstevel@tonic-gate 			if (Vflag == 0) {
264*0Sstevel@tonic-gate 				(void) fprintf(stderr, "ar: %s %s\n",
265*0Sstevel@tonic-gate 				    (const char *)SGU_PKG,
266*0Sstevel@tonic-gate 				    (const char *)SGU_REL);
267*0Sstevel@tonic-gate 					Vflag++;
268*0Sstevel@tonic-gate 			}
269*0Sstevel@tonic-gate 			break;
270*0Sstevel@tonic-gate 		case 'C':
271*0Sstevel@tonic-gate 			cmd_info->OPT_flgs |= C_FLAG;
272*0Sstevel@tonic-gate 			break;
273*0Sstevel@tonic-gate 		case 'M':
274*0Sstevel@tonic-gate 			cmd_info->OPT_flgs |= M_FLAG;
275*0Sstevel@tonic-gate 			break;
276*0Sstevel@tonic-gate 		case 'T':
277*0Sstevel@tonic-gate 			cmd_info->OPT_flgs |= T_FLAG;
278*0Sstevel@tonic-gate 			break;
279*0Sstevel@tonic-gate 		case ':':
280*0Sstevel@tonic-gate 			error_message(USAGE_02_ERROR,
281*0Sstevel@tonic-gate 			PLAIN_ERROR, (char *)0, optopt);
282*0Sstevel@tonic-gate 			usage_err++;
283*0Sstevel@tonic-gate 			break;
284*0Sstevel@tonic-gate 		case '?':
285*0Sstevel@tonic-gate 			error_message(USAGE_03_ERROR,
286*0Sstevel@tonic-gate 			PLAIN_ERROR, (char *)0, optopt);
287*0Sstevel@tonic-gate 			usage_err++;
288*0Sstevel@tonic-gate 			break;
289*0Sstevel@tonic-gate 		}
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if (usage_err || argc - optind < 1)
293*0Sstevel@tonic-gate 		usage();
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	cmd_info->arnam = arnam = argv[optind];
296*0Sstevel@tonic-gate 	cmd_info->namv = &argv[optind+1];
297*0Sstevel@tonic-gate 	cmd_info->namc = argc - optind - 1;
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  * Set the function to be called to do the key operation.
303*0Sstevel@tonic-gate  * Check that only one key is indicated.
304*0Sstevel@tonic-gate  */
305*0Sstevel@tonic-gate static void
306*0Sstevel@tonic-gate setcom(Cmd_info *cmd_info, int (*fun)())
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	if (cmd_info->comfun != 0) {
309*0Sstevel@tonic-gate 		error_message(USAGE_04_ERROR, PLAIN_ERROR, (char *)0);
310*0Sstevel@tonic-gate 		exit(1);
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 	cmd_info->comfun = fun;
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate static void
316*0Sstevel@tonic-gate usage()
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
319*0Sstevel@tonic-gate "usage: ar -d[-vV] archive file ...\n"
320*0Sstevel@tonic-gate "       ar -m[-abivV] [posname] archive file ...\n"
321*0Sstevel@tonic-gate "       ar -p[-vV][-s] archive [file ...]\n"
322*0Sstevel@tonic-gate "       ar -q[-cuvV] [-abi] [posname] [file ...]\n"
323*0Sstevel@tonic-gate "       ar -r[-cuvV] [-abi] [posname] [file ...]\n"
324*0Sstevel@tonic-gate "       ar -t[-vV][-s] archive [file ...]\n"
325*0Sstevel@tonic-gate "       ar -x[-vV][-sCT] archive [file ...]\n"));
326*0Sstevel@tonic-gate 	exit(1);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate /*ARGSUSED0*/
330*0Sstevel@tonic-gate static void
331*0Sstevel@tonic-gate sigexit(i)
332*0Sstevel@tonic-gate int i;
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate 	exit(100);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate /* tells the user which of the listed files were not found in the archive */
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate static int
340*0Sstevel@tonic-gate notfound(Cmd_info *cmd_info)
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate 	register int i, n;
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	n = 0;
345*0Sstevel@tonic-gate 	for (i = 0; i < cmd_info->namc; i++)
346*0Sstevel@tonic-gate 		if (cmd_info->namv[i]) {
347*0Sstevel@tonic-gate 			error_message(NOT_FOUND_03_ERROR,
348*0Sstevel@tonic-gate 			PLAIN_ERROR, (char *)0, cmd_info->namv[i]);
349*0Sstevel@tonic-gate 			n++;
350*0Sstevel@tonic-gate 		}
351*0Sstevel@tonic-gate 	return (n);
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate /*
355*0Sstevel@tonic-gate  * Debugging info
356*0Sstevel@tonic-gate  */
357*0Sstevel@tonic-gate static void
358*0Sstevel@tonic-gate check_swap()
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	(void) system("/usr/sbin/swap -s");
361*0Sstevel@tonic-gate }
362