xref: /titanic_50/usr/src/cmd/svr4pkg/pkgscripts/cmdexec.c (revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <pkglib.h>
39 
40 #define	COMMAND '!'
41 #define	LSIZE 256
42 
43 #define	ERR_NOTROOT	"You must be \"root\" for %s to execute properly."
44 
45 static void	usage(void);
46 static int	docmd(char *cmd, char *file, char *input);
47 
48 int
49 main(int argc, char *argv[])
50 {
51 	FILE	*fpout, *fp;
52 	char	line[LSIZE],
53 		*pt,
54 		*keyword, 	/* keyword = install || remove */
55 		*input, 	/* sed input file */
56 		*cmd,
57 		*srcfile, 	/* sed data file */
58 		*destfile; 	/* target file to be updated */
59 	int	flag;
60 	char	*prog;
61 
62 	(void) setlocale(LC_ALL, "");
63 
64 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
65 #define	TEXT_DOMAIN "SYS_TEST"
66 #endif
67 	(void) textdomain(TEXT_DOMAIN);
68 
69 	prog = set_prog_name(argv[0]);
70 
71 	if (getuid()) {
72 		progerr(gettext(ERR_NOTROOT), prog);
73 		exit(1);
74 	}
75 
76 	if (argc != 5)
77 		usage();
78 
79 	cmd = argv[1];
80 	keyword = argv[2];
81 	srcfile = argv[3];
82 	destfile = argv[4];
83 
84 	srcfile = argv[3];
85 	if ((fp = fopen(srcfile, "r")) == NULL) {
86 		progerr(gettext("unable to open %s"), srcfile);
87 		exit(1);
88 	}
89 
90 	input = tempnam(NULL, "sedinp");
91 	if ((fpout = fopen(input, "w")) == NULL) {
92 		progerr(gettext("unable to open %s"), input);
93 		exit(2);
94 	}
95 
96 	flag = (-1);
97 	while (fgets(line, LSIZE, fp)) {
98 		for (pt = line; isspace(*pt); /* void */)
99 			++pt;
100 		if (*pt == '#')
101 			continue;
102 		if (*pt == COMMAND) {
103 			if (flag > 0)
104 				break; /* no more lines to read */
105 			pt = strtok(pt+1, " \t\n");
106 			if (!pt) {
107 				progerr(gettext("null token after '!'"));
108 				exit(1);
109 			}
110 			flag = (strcmp(pt, keyword) ? 0 : 1);
111 		} else if (flag == 1) { /* bug # 1083359 */
112 			(void) fputs(line, fpout);
113 		}
114 	}
115 	(void) fclose(fpout);
116 	if (flag > 0) {
117 		if (docmd(cmd, destfile, input)) {
118 			progerr(gettext("command failed <%s>"), cmd);
119 			exit(1);
120 		}
121 	}
122 	(void) unlink(input);
123 	return (0);
124 }
125 
126 static int
127 docmd(char *cmd, char *file, char *input)
128 {
129 	char *tempout;
130 	char command[256];
131 
132 	tempout = tempnam(NULL, "temp1");
133 	if (!tempout)
134 		return (-1);
135 
136 	(void) sprintf(command, "%s -f %s <%s >%s", cmd, input, file, tempout);
137 	if (system(command))
138 		return (-1);
139 
140 	(void) sprintf(command, "cp %s %s", tempout, file);
141 	if (system(command))
142 		return (-1);
143 
144 	(void) unlink(tempout);
145 	free(tempout);
146 	return (0);
147 }
148 
149 static void
150 usage(void)
151 {
152 	(void) fprintf(stderr, gettext("usage: %s cmd keyword src dest\n"),
153 	    get_prog_name());
154 	exit(2);
155 }
156