4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/types.h>
46 * error - print out an error message and die
48 * args: msg - message to be printed, Saferrno previously set
55 (void) fprintf(stderr
, "%s\n", msg
);
61 * quit - exit the program with the status in Saferrno
72 * make_tempname - generate a temp name to be used for updating files.
73 * Names will be of the form HOME/xxx/.name, where HOME
76 * args: bname - the basename of the file. For example foo/_config
77 * will generate a tempname of HOME/foo/._config
85 static char buf
[SIZE
]; /* this is where we put the new name */
86 char *p
; /* work pointer */
88 p
= strrchr(bname
, '/');
90 (void) sprintf(buf
, "%s/.%s", HOME
, bname
);
92 (void) strcpy(buf
, HOME
);
93 /* this zaps the trailing slash so the '.' can be stuck in */
95 (void) strcat(buf
, "/");
96 (void) strcat(buf
, bname
);
97 (void) strcat(buf
, "/.");
98 (void) strcat(buf
, (p
+ 1));
106 * open_temp - open up a temp file
108 * args: tname - temp file name
117 FILE *fp
; /* fp associated with tname */
118 struct sigaction sigact
; /* for signal handling */
121 sigact
.sa_handler
= SIG_IGN
;
122 (void) sigemptyset(&sigact
.sa_mask
);
123 (void) sigaddset(&sigact
.sa_mask
, SIGHUP
);
124 (void) sigaddset(&sigact
.sa_mask
, SIGINT
);
125 (void) sigaddset(&sigact
.sa_mask
, SIGQUIT
);
126 (void) sigaction(SIGHUP
, &sigact
, NULL
);
127 (void) sigaction(SIGINT
, &sigact
, NULL
);
128 (void) sigaction(SIGQUIT
, &sigact
, NULL
);
130 if (access(tname
, 0) != -1) {
132 error("tempfile busy; try again later");
134 fp
= fopen(tname
, "w");
137 error("cannot create tempfile");
144 * replace - replace one file with another, only returns on success
146 * args: fname - name of target file
147 * tname - name of source file
152 replace(fname
, tname
)
156 char buf
[SIZE
]; /* scratch buffer */
158 (void) sprintf(buf
, "%s/%s", HOME
, fname
);
160 if (rename(tname
, buf
) < 0) {
162 (void) unlink(tname
);
169 * copy_file - copy information from one file to another, return 0 on
170 * success, -1 on failure
172 * args: fp - source file's file pointer
173 * tfp - destination file's file pointer
174 * start - starting line number
175 * finish - ending line number (-1 indicates entire file)
179 copy_file(FILE *fp
, FILE *tfp
, int start
, int finish
)
181 int i
; /* loop variable */
182 char dummy
[SIZE
]; /* scratch buffer */
185 * always start from the beginning because line numbers are absolute
191 * get to the starting point of interest
195 for (i
= 1; i
< start
; i
++)
196 if (!fgets(dummy
, SIZE
, fp
))
201 * copy as much as was requested
205 for (i
= start
; i
<= finish
; i
++) {
206 if (!fgets(dummy
, SIZE
, fp
))
208 if (fputs(dummy
, tfp
) == EOF
)
214 if (fgets(dummy
, SIZE
, fp
) == NULL
) {
220 if (fputs(dummy
, tfp
) == EOF
)
229 * find_pm - find an entry in _sactab for a particular port monitor
231 * args: fp - file pointer for _sactab
232 * pmtag - tag of port monitor we're looking for
236 find_pm(FILE *fp
, char *pmtag
)
238 char *p
; /* working pointer */
239 int line
= 0; /* line number we found entry on */
240 struct sactab stab
; /* place to hold parsed info */
241 char buf
[SIZE
]; /* scratch buffer */
243 while (fgets(buf
, SIZE
, fp
)) {
249 if (!(strcmp(stab
.sc_tag
, pmtag
)))
254 error("error reading _sactab");
264 * do_config - take a config script and put it where it belongs or
265 * output an existing one. Saferrno is set if any errors
266 * are encountered. Calling routine may choose to quit or
267 * continue, in which case Saferrno will stay set, but may
268 * change value if another error is encountered.
270 * args: script - name of file containing script (if NULL, means output
271 * existing one instead)
272 * basename - name of script (relative to HOME (from misc.h))
276 do_config(char *script
, char *basename
)
278 FILE *ifp
; /* file pointer for source file */
279 FILE *ofp
; /* file pointer for target file */
280 struct stat statbuf
; /* file status info */
281 char *tname
; /* name of tempfile */
282 char buf
[SIZE
]; /* scratch buffer */
285 /* we're installing a new configuration script */
286 if (access(script
, 0) == 0) {
287 if (stat(script
, &statbuf
) < 0) {
289 (void) fprintf(stderr
, "Could not stat <%s>\n", script
);
292 if ((statbuf
.st_mode
& S_IFMT
) != S_IFREG
) {
293 (void) fprintf(stderr
, "warning - %s not a regular file - ignored\n", script
);
298 Saferrno
= E_NOEXIST
;
299 (void) fprintf(stderr
, "Invalid request, %s does not exist\n", script
);
302 ifp
= fopen(script
, "r");
304 (void) fprintf(stderr
, "Invalid request, can not open %s\n", script
);
308 tname
= make_tempname(basename
);
309 /* note - open_temp only returns if successful */
310 ofp
= open_temp(tname
);
311 while(fgets(buf
, SIZE
, ifp
)) {
312 if (fputs(buf
, ofp
) == EOF
) {
313 (void) unlink(tname
);
315 error("error in writing tempfile");
319 if (fclose(ofp
) == EOF
) {
320 (void) unlink(tname
);
322 error("error closing tempfile");
324 /* note - replace only returns if successful */
325 replace(basename
, tname
);
329 /* we're outputting a configuration script */
330 (void) sprintf(buf
, "%s/%s", HOME
, basename
);
331 if (access(buf
, 0) < 0) {
332 (void) fprintf(stderr
, "Invalid request, script does not exist\n");
333 Saferrno
= E_NOEXIST
;
336 ifp
= fopen(buf
, "r");
338 (void) fprintf(stderr
, "Invalid request, can not open script\n");
342 while (fgets(buf
, SIZE
, ifp
))
343 (void) fputs(buf
, stdout
);