xref: /onnv-gate/usr/src/cmd/svr4pkg/pkgscripts/cmdexec.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
32*9781SMoriah.Waterland@Sun.COM #include <ctype.h>
33*9781SMoriah.Waterland@Sun.COM #include <string.h>
34*9781SMoriah.Waterland@Sun.COM #include <locale.h>
35*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
36*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
37*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
38*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
39*9781SMoriah.Waterland@Sun.COM 
40*9781SMoriah.Waterland@Sun.COM #define	COMMAND '!'
41*9781SMoriah.Waterland@Sun.COM #define	LSIZE 256
42*9781SMoriah.Waterland@Sun.COM 
43*9781SMoriah.Waterland@Sun.COM #define	ERR_NOTROOT	"You must be \"root\" for %s to execute properly."
44*9781SMoriah.Waterland@Sun.COM 
45*9781SMoriah.Waterland@Sun.COM static void	usage(void);
46*9781SMoriah.Waterland@Sun.COM static int	docmd(char *cmd, char *file, char *input);
47*9781SMoriah.Waterland@Sun.COM 
48*9781SMoriah.Waterland@Sun.COM int
main(int argc,char * argv[])49*9781SMoriah.Waterland@Sun.COM main(int argc, char *argv[])
50*9781SMoriah.Waterland@Sun.COM {
51*9781SMoriah.Waterland@Sun.COM 	FILE	*fpout, *fp;
52*9781SMoriah.Waterland@Sun.COM 	char	line[LSIZE],
53*9781SMoriah.Waterland@Sun.COM 		*pt,
54*9781SMoriah.Waterland@Sun.COM 		*keyword, 	/* keyword = install || remove */
55*9781SMoriah.Waterland@Sun.COM 		*input, 	/* sed input file */
56*9781SMoriah.Waterland@Sun.COM 		*cmd,
57*9781SMoriah.Waterland@Sun.COM 		*srcfile, 	/* sed data file */
58*9781SMoriah.Waterland@Sun.COM 		*destfile; 	/* target file to be updated */
59*9781SMoriah.Waterland@Sun.COM 	int	flag;
60*9781SMoriah.Waterland@Sun.COM 	char	*prog;
61*9781SMoriah.Waterland@Sun.COM 
62*9781SMoriah.Waterland@Sun.COM 	(void) setlocale(LC_ALL, "");
63*9781SMoriah.Waterland@Sun.COM 
64*9781SMoriah.Waterland@Sun.COM #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
65*9781SMoriah.Waterland@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"
66*9781SMoriah.Waterland@Sun.COM #endif
67*9781SMoriah.Waterland@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
68*9781SMoriah.Waterland@Sun.COM 
69*9781SMoriah.Waterland@Sun.COM 	prog = set_prog_name(argv[0]);
70*9781SMoriah.Waterland@Sun.COM 
71*9781SMoriah.Waterland@Sun.COM 	if (getuid()) {
72*9781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_NOTROOT), prog);
73*9781SMoriah.Waterland@Sun.COM 		exit(1);
74*9781SMoriah.Waterland@Sun.COM 	}
75*9781SMoriah.Waterland@Sun.COM 
76*9781SMoriah.Waterland@Sun.COM 	if (argc != 5)
77*9781SMoriah.Waterland@Sun.COM 		usage();
78*9781SMoriah.Waterland@Sun.COM 
79*9781SMoriah.Waterland@Sun.COM 	cmd = argv[1];
80*9781SMoriah.Waterland@Sun.COM 	keyword = argv[2];
81*9781SMoriah.Waterland@Sun.COM 	srcfile = argv[3];
82*9781SMoriah.Waterland@Sun.COM 	destfile = argv[4];
83*9781SMoriah.Waterland@Sun.COM 
84*9781SMoriah.Waterland@Sun.COM 	srcfile = argv[3];
85*9781SMoriah.Waterland@Sun.COM 	if ((fp = fopen(srcfile, "r")) == NULL) {
86*9781SMoriah.Waterland@Sun.COM 		progerr(gettext("unable to open %s"), srcfile);
87*9781SMoriah.Waterland@Sun.COM 		exit(1);
88*9781SMoriah.Waterland@Sun.COM 	}
89*9781SMoriah.Waterland@Sun.COM 
90*9781SMoriah.Waterland@Sun.COM 	input = tempnam(NULL, "sedinp");
91*9781SMoriah.Waterland@Sun.COM 	if ((fpout = fopen(input, "w")) == NULL) {
92*9781SMoriah.Waterland@Sun.COM 		progerr(gettext("unable to open %s"), input);
93*9781SMoriah.Waterland@Sun.COM 		exit(2);
94*9781SMoriah.Waterland@Sun.COM 	}
95*9781SMoriah.Waterland@Sun.COM 
96*9781SMoriah.Waterland@Sun.COM 	flag = (-1);
97*9781SMoriah.Waterland@Sun.COM 	while (fgets(line, LSIZE, fp)) {
98*9781SMoriah.Waterland@Sun.COM 		for (pt = line; isspace(*pt); /* void */)
99*9781SMoriah.Waterland@Sun.COM 			++pt;
100*9781SMoriah.Waterland@Sun.COM 		if (*pt == '#')
101*9781SMoriah.Waterland@Sun.COM 			continue;
102*9781SMoriah.Waterland@Sun.COM 		if (*pt == COMMAND) {
103*9781SMoriah.Waterland@Sun.COM 			if (flag > 0)
104*9781SMoriah.Waterland@Sun.COM 				break; /* no more lines to read */
105*9781SMoriah.Waterland@Sun.COM 			pt = strtok(pt+1, " \t\n");
106*9781SMoriah.Waterland@Sun.COM 			if (!pt) {
107*9781SMoriah.Waterland@Sun.COM 				progerr(gettext("null token after '!'"));
108*9781SMoriah.Waterland@Sun.COM 				exit(1);
109*9781SMoriah.Waterland@Sun.COM 			}
110*9781SMoriah.Waterland@Sun.COM 			flag = (strcmp(pt, keyword) ? 0 : 1);
111*9781SMoriah.Waterland@Sun.COM 		} else if (flag == 1) { /* bug # 1083359 */
112*9781SMoriah.Waterland@Sun.COM 			(void) fputs(line, fpout);
113*9781SMoriah.Waterland@Sun.COM 		}
114*9781SMoriah.Waterland@Sun.COM 	}
115*9781SMoriah.Waterland@Sun.COM 	(void) fclose(fpout);
116*9781SMoriah.Waterland@Sun.COM 	if (flag > 0) {
117*9781SMoriah.Waterland@Sun.COM 		if (docmd(cmd, destfile, input)) {
118*9781SMoriah.Waterland@Sun.COM 			progerr(gettext("command failed <%s>"), cmd);
119*9781SMoriah.Waterland@Sun.COM 			exit(1);
120*9781SMoriah.Waterland@Sun.COM 		}
121*9781SMoriah.Waterland@Sun.COM 	}
122*9781SMoriah.Waterland@Sun.COM 	(void) unlink(input);
123*9781SMoriah.Waterland@Sun.COM 	return (0);
124*9781SMoriah.Waterland@Sun.COM }
125*9781SMoriah.Waterland@Sun.COM 
126*9781SMoriah.Waterland@Sun.COM static int
docmd(char * cmd,char * file,char * input)127*9781SMoriah.Waterland@Sun.COM docmd(char *cmd, char *file, char *input)
128*9781SMoriah.Waterland@Sun.COM {
129*9781SMoriah.Waterland@Sun.COM 	char *tempout;
130*9781SMoriah.Waterland@Sun.COM 	char command[256];
131*9781SMoriah.Waterland@Sun.COM 
132*9781SMoriah.Waterland@Sun.COM 	tempout = tempnam(NULL, "temp1");
133*9781SMoriah.Waterland@Sun.COM 	if (!tempout)
134*9781SMoriah.Waterland@Sun.COM 		return (-1);
135*9781SMoriah.Waterland@Sun.COM 
136*9781SMoriah.Waterland@Sun.COM 	(void) sprintf(command, "%s -f %s <%s >%s", cmd, input, file, tempout);
137*9781SMoriah.Waterland@Sun.COM 	if (system(command))
138*9781SMoriah.Waterland@Sun.COM 		return (-1);
139*9781SMoriah.Waterland@Sun.COM 
140*9781SMoriah.Waterland@Sun.COM 	(void) sprintf(command, "cp %s %s", tempout, file);
141*9781SMoriah.Waterland@Sun.COM 	if (system(command))
142*9781SMoriah.Waterland@Sun.COM 		return (-1);
143*9781SMoriah.Waterland@Sun.COM 
144*9781SMoriah.Waterland@Sun.COM 	(void) unlink(tempout);
145*9781SMoriah.Waterland@Sun.COM 	free(tempout);
146*9781SMoriah.Waterland@Sun.COM 	return (0);
147*9781SMoriah.Waterland@Sun.COM }
148*9781SMoriah.Waterland@Sun.COM 
149*9781SMoriah.Waterland@Sun.COM static void
usage(void)150*9781SMoriah.Waterland@Sun.COM usage(void)
151*9781SMoriah.Waterland@Sun.COM {
152*9781SMoriah.Waterland@Sun.COM 	(void) fprintf(stderr, gettext("usage: %s cmd keyword src dest\n"),
153*9781SMoriah.Waterland@Sun.COM 	    get_prog_name());
154*9781SMoriah.Waterland@Sun.COM 	exit(2);
155*9781SMoriah.Waterland@Sun.COM }
156