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 /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27*0Sstevel@tonic-gate /* All Rights Reserved */
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
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <unistd.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <termio.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <stdarg.h>
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate #include "tmstruct.h"
46*0Sstevel@tonic-gate #include "ttymon.h"
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate static int nflg = 0; /* -n seen */
49*0Sstevel@tonic-gate static int iflg = 0; /* -i seen */
50*0Sstevel@tonic-gate static int fflg = 0; /* -f seen */
51*0Sstevel@tonic-gate static int lflg = 0; /* -l seen */
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate struct Gdef Gdef[MAXDEFS]; /* array to hold entries in /etc/ttydefs */
54*0Sstevel@tonic-gate int Ndefs = 0; /* highest index to Gdef that was used */
55*0Sstevel@tonic-gate static struct Gdef DEFAULT = { /* default terminal settings */
56*0Sstevel@tonic-gate "default",
57*0Sstevel@tonic-gate "9600",
58*0Sstevel@tonic-gate "9600 sane",
59*0Sstevel@tonic-gate 0,
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate * next label is set to 4800 so we can start searching ttydefs.
62*0Sstevel@tonic-gate * if 4800 is not in ttydefs, we will loop back to use DEFAULT
63*0Sstevel@tonic-gate */
64*0Sstevel@tonic-gate "4800"
65*0Sstevel@tonic-gate };
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate static void usage();
69*0Sstevel@tonic-gate static void check_ref();
70*0Sstevel@tonic-gate static void add_entry();
71*0Sstevel@tonic-gate static void remove_entry();
72*0Sstevel@tonic-gate static int copy_file();
73*0Sstevel@tonic-gate static int verify();
74*0Sstevel@tonic-gate static FILE *open_temp();
75*0Sstevel@tonic-gate extern void read_ttydefs();
76*0Sstevel@tonic-gate extern int check_version();
77*0Sstevel@tonic-gate extern int find_label();
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate * sttydefs - add, remove or check entries in /etc/ttydefs
81*0Sstevel@tonic-gate *
82*0Sstevel@tonic-gate * Usage: sttydefs -a ttylabel [-n nextlabel] [-i initail-flags]
83*0Sstevel@tonic-gate * [-f final-flags] [-b]
84*0Sstevel@tonic-gate * sttydefs -r ttylabel
85*0Sstevel@tonic-gate * sttydefs -l [ttylabel]
86*0Sstevel@tonic-gate *
87*0Sstevel@tonic-gate */
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate int
main(int argc,char * argv[])90*0Sstevel@tonic-gate main(int argc, char *argv[])
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate int c; /* option letter */
93*0Sstevel@tonic-gate int errflg = 0; /* error indicator */
94*0Sstevel@tonic-gate int aflg = 0; /* -a seen */
95*0Sstevel@tonic-gate int bflg = 0; /* -b seen */
96*0Sstevel@tonic-gate int ret;
97*0Sstevel@tonic-gate #ifdef __STDC__
98*0Sstevel@tonic-gate const
99*0Sstevel@tonic-gate #endif
100*0Sstevel@tonic-gate char *argtmp;
101*0Sstevel@tonic-gate char *nextlabel;
102*0Sstevel@tonic-gate struct Gdef ttydef, *ptr;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate extern char *optarg;
105*0Sstevel@tonic-gate extern int optind;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate if (argc == 1)
108*0Sstevel@tonic-gate usage();
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /* Initialize ttydef structure */
111*0Sstevel@tonic-gate memset (&ttydef, 0, sizeof(ttydef));
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate ptr = &ttydef;
114*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "a:n:i:f:br:l")) != -1) {
115*0Sstevel@tonic-gate switch (c) {
116*0Sstevel@tonic-gate case 'a':
117*0Sstevel@tonic-gate aflg = TRUE;
118*0Sstevel@tonic-gate ptr->g_id = optarg;
119*0Sstevel@tonic-gate break;
120*0Sstevel@tonic-gate case 'n':
121*0Sstevel@tonic-gate nflg = TRUE;
122*0Sstevel@tonic-gate ptr->g_nextid = optarg;
123*0Sstevel@tonic-gate break;
124*0Sstevel@tonic-gate case 'i':
125*0Sstevel@tonic-gate iflg = TRUE;
126*0Sstevel@tonic-gate ptr->g_iflags = optarg;
127*0Sstevel@tonic-gate break;
128*0Sstevel@tonic-gate case 'f':
129*0Sstevel@tonic-gate fflg = TRUE;
130*0Sstevel@tonic-gate ptr->g_fflags = optarg;
131*0Sstevel@tonic-gate break;
132*0Sstevel@tonic-gate case 'b':
133*0Sstevel@tonic-gate bflg = TRUE;
134*0Sstevel@tonic-gate ptr->g_autobaud |= A_FLAG;
135*0Sstevel@tonic-gate break;
136*0Sstevel@tonic-gate case 'r':
137*0Sstevel@tonic-gate if ((argc > 3) || (optind < argc))
138*0Sstevel@tonic-gate usage();
139*0Sstevel@tonic-gate remove_entry(optarg);
140*0Sstevel@tonic-gate break;
141*0Sstevel@tonic-gate case 'l':
142*0Sstevel@tonic-gate lflg = TRUE;
143*0Sstevel@tonic-gate if (argc > 3)
144*0Sstevel@tonic-gate usage();
145*0Sstevel@tonic-gate if ((ret = check_version(TTYDEFS_VERS, TTYDEFS)) != 0) {
146*0Sstevel@tonic-gate if (ret != 2) {
147*0Sstevel@tonic-gate (void)fprintf(stderr, "%s version number is incorrect or missing.\n",TTYDEFS);
148*0Sstevel@tonic-gate exit(1);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate (void)fprintf(stderr, "sttydefs: can't open %s.\n",TTYDEFS);
151*0Sstevel@tonic-gate exit(1);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate if (argv[optind] == NULL) {
154*0Sstevel@tonic-gate read_ttydefs(NULL,TRUE);
155*0Sstevel@tonic-gate printf("\n");
156*0Sstevel@tonic-gate check_ref();
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate else {
159*0Sstevel@tonic-gate if (argc == 3) { /* -l ttylabel */
160*0Sstevel@tonic-gate if (verify(argv[optind],0) != 0) {
161*0Sstevel@tonic-gate errflg++;
162*0Sstevel@tonic-gate break;
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate argtmp = argv[optind];
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate else { /* -lttylabel */
167*0Sstevel@tonic-gate argtmp = argv[optind]+2;
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate read_ttydefs(argtmp,TRUE);
170*0Sstevel@tonic-gate if (Ndefs == 0) {
171*0Sstevel@tonic-gate (void)fprintf(stderr,
172*0Sstevel@tonic-gate "ttylabel <%s> not found.\n", argtmp);
173*0Sstevel@tonic-gate exit(1);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate nextlabel = Gdef[--Ndefs].g_nextid;
176*0Sstevel@tonic-gate Ndefs = 0;
177*0Sstevel@tonic-gate read_ttydefs(nextlabel,FALSE);
178*0Sstevel@tonic-gate if (Ndefs == 0) {
179*0Sstevel@tonic-gate (void)printf("\nWarning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
180*0Sstevel@tonic-gate nextlabel, argtmp);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate exit(0);
184*0Sstevel@tonic-gate break; /*NOTREACHED*/
185*0Sstevel@tonic-gate case '?':
186*0Sstevel@tonic-gate errflg++;
187*0Sstevel@tonic-gate break;
188*0Sstevel@tonic-gate } /* end switch */
189*0Sstevel@tonic-gate if (errflg)
190*0Sstevel@tonic-gate usage();
191*0Sstevel@tonic-gate } /* end while */
192*0Sstevel@tonic-gate if (optind < argc)
193*0Sstevel@tonic-gate usage();
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate if (aflg) {
196*0Sstevel@tonic-gate add_entry(ptr); /* never return */
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate if ((iflg) || (fflg) || (bflg) || (nflg))
199*0Sstevel@tonic-gate usage();
200*0Sstevel@tonic-gate return (0);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate * verify - to check if arg is valid
205*0Sstevel@tonic-gate * - i.e. arg cannot start with '-' and
206*0Sstevel@tonic-gate * arg must not longer than maxarglen
207*0Sstevel@tonic-gate * - return 0 if ok. Otherwise return -1
208*0Sstevel@tonic-gate */
209*0Sstevel@tonic-gate static int
verify(arg,maxarglen)210*0Sstevel@tonic-gate verify(arg,maxarglen)
211*0Sstevel@tonic-gate char *arg;
212*0Sstevel@tonic-gate int maxarglen;
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate if (*arg == '-') {
215*0Sstevel@tonic-gate (void)fprintf(stderr, "Invalid argument -- %s.\n", arg);
216*0Sstevel@tonic-gate return(-1);
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate if ((maxarglen) && ((int)strlen(arg) > maxarglen)) {
219*0Sstevel@tonic-gate arg[maxarglen] = '\0';
220*0Sstevel@tonic-gate (void)fprintf(stderr,"string too long, truncated to %s.\n",arg);
221*0Sstevel@tonic-gate return(-1);
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate return(0);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate * usage - print out a usage message
228*0Sstevel@tonic-gate */
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate static void
usage()231*0Sstevel@tonic-gate usage()
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate (void)fprintf(stderr, "Usage:\tsttydefs -a ttylabel [-n nextlabel] [-i initial-flags]\n\t\t [-f final-flags] [-b]\n");
234*0Sstevel@tonic-gate (void)fprintf(stderr, "\tsttydefs -r ttylabel\n");
235*0Sstevel@tonic-gate (void)fprintf(stderr, "\tsttydefs -l [ttylabel]\n");
236*0Sstevel@tonic-gate exit(2);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate * add_entry - add an entry to /etc/ttydefs
241*0Sstevel@tonic-gate */
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate static void
add_entry(ttydef)244*0Sstevel@tonic-gate add_entry(ttydef)
245*0Sstevel@tonic-gate struct Gdef *ttydef;
246*0Sstevel@tonic-gate {
247*0Sstevel@tonic-gate FILE *fp;
248*0Sstevel@tonic-gate int errflg = 0;
249*0Sstevel@tonic-gate char tbuf[BUFSIZ], *tp;
250*0Sstevel@tonic-gate int add_version = FALSE;
251*0Sstevel@tonic-gate extern int check_flags();
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if (getuid()) {
254*0Sstevel@tonic-gate (void)fprintf(stderr, "User not privileged for operation.\n");
255*0Sstevel@tonic-gate exit(1);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate tp = tbuf;
258*0Sstevel@tonic-gate *tp = '\0';
259*0Sstevel@tonic-gate if ((fp = fopen(TTYDEFS, "r")) != NULL) {
260*0Sstevel@tonic-gate if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
261*0Sstevel@tonic-gate (void)fprintf(stderr,
262*0Sstevel@tonic-gate "%s version number is incorrect or missing.\n",TTYDEFS);
263*0Sstevel@tonic-gate exit(1);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate if (find_label(fp,ttydef->g_id)) {
266*0Sstevel@tonic-gate (void)fclose(fp);
267*0Sstevel@tonic-gate (void)fprintf(stderr,
268*0Sstevel@tonic-gate "Invalid request -- ttylabel <%s> already exists.\n",
269*0Sstevel@tonic-gate ttydef->g_id);
270*0Sstevel@tonic-gate exit(1);
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate (void)fclose(fp);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate else {
275*0Sstevel@tonic-gate add_version = TRUE;
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate if ((fp = fopen(TTYDEFS, "a+")) == NULL) {
278*0Sstevel@tonic-gate (void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
279*0Sstevel@tonic-gate strerror(errno));
280*0Sstevel@tonic-gate exit(1);
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate if (add_version) {
284*0Sstevel@tonic-gate (void)fprintf(fp,"# VERSION=%d\n", TTYDEFS_VERS);
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate /* if optional fields are not provided, set to default */
289*0Sstevel@tonic-gate if (!iflg)
290*0Sstevel@tonic-gate ttydef->g_iflags = DEFAULT.g_iflags;
291*0Sstevel@tonic-gate else
292*0Sstevel@tonic-gate if (check_flags(ttydef->g_iflags) != 0 )
293*0Sstevel@tonic-gate errflg++;
294*0Sstevel@tonic-gate if (!fflg)
295*0Sstevel@tonic-gate ttydef->g_fflags = DEFAULT.g_fflags;
296*0Sstevel@tonic-gate else
297*0Sstevel@tonic-gate if (check_flags(ttydef->g_fflags) != 0 )
298*0Sstevel@tonic-gate errflg++;
299*0Sstevel@tonic-gate if (errflg)
300*0Sstevel@tonic-gate exit(1);
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate if (!nflg)
303*0Sstevel@tonic-gate ttydef->g_nextid = ttydef->g_id;
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate if (ttydef->g_autobaud & A_FLAG) {
306*0Sstevel@tonic-gate (void)fprintf(fp,"%s:%s:%s:A:%s\n", ttydef->g_id, ttydef->g_iflags,
307*0Sstevel@tonic-gate ttydef->g_fflags, ttydef->g_nextid);
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate else {
310*0Sstevel@tonic-gate (void)fprintf(fp,"%s:%s:%s::%s\n", ttydef->g_id, ttydef->g_iflags,
311*0Sstevel@tonic-gate ttydef->g_fflags, ttydef->g_nextid);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate (void)fclose(fp);
314*0Sstevel@tonic-gate exit(0);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate static void
remove_entry(ttylabel)318*0Sstevel@tonic-gate remove_entry(ttylabel)
319*0Sstevel@tonic-gate char *ttylabel;
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate FILE *tfp; /* file pointer for temp file */
322*0Sstevel@tonic-gate int line; /* line number entry is on */
323*0Sstevel@tonic-gate FILE *fp; /* scratch file pointer */
324*0Sstevel@tonic-gate char *tname = "/etc/.ttydefs";
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate if (getuid()) {
327*0Sstevel@tonic-gate (void)fprintf(stderr, "User not privileged for operation.\n");
328*0Sstevel@tonic-gate exit(1);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate fp = fopen(TTYDEFS, "r");
331*0Sstevel@tonic-gate if (fp == NULL) {
332*0Sstevel@tonic-gate (void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
333*0Sstevel@tonic-gate strerror(errno));
334*0Sstevel@tonic-gate exit(1);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
337*0Sstevel@tonic-gate (void)fprintf(stderr,
338*0Sstevel@tonic-gate "%s version number is incorrect or missing.\n",TTYDEFS);
339*0Sstevel@tonic-gate exit(1);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate if ((line = find_label(fp, ttylabel)) == 0) {
342*0Sstevel@tonic-gate (void)fprintf(stderr,
343*0Sstevel@tonic-gate "Invalid request, ttylabel <%s> does not exist.\n", ttylabel);
344*0Sstevel@tonic-gate exit(1);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate tfp = open_temp(tname);
347*0Sstevel@tonic-gate if (line != 1)
348*0Sstevel@tonic-gate if (copy_file(fp, tfp, 1, line - 1)) {
349*0Sstevel@tonic-gate (void)fprintf(stderr,"Error accessing temp file.\n");
350*0Sstevel@tonic-gate exit(1);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate if (copy_file(fp, tfp, line + 1, -1)) {
353*0Sstevel@tonic-gate (void)fprintf(stderr,"Error accessing temp file.\n");
354*0Sstevel@tonic-gate exit(1);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate (void)fclose(fp);
357*0Sstevel@tonic-gate if (fclose(tfp) == EOF) {
358*0Sstevel@tonic-gate (void)unlink(tname);
359*0Sstevel@tonic-gate (void)fprintf(stderr,"Error closing temp file.\n");
360*0Sstevel@tonic-gate exit(1);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate (void)unlink(TTYDEFS);
363*0Sstevel@tonic-gate if (rename(tname, TTYDEFS) != 0 ) {
364*0Sstevel@tonic-gate perror("Rename failed");
365*0Sstevel@tonic-gate (void)unlink(tname);
366*0Sstevel@tonic-gate exit(1);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate exit(0);
369*0Sstevel@tonic-gate }
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate /*
372*0Sstevel@tonic-gate * open_temp - open up a temp file
373*0Sstevel@tonic-gate *
374*0Sstevel@tonic-gate * args: tname - temp file name
375*0Sstevel@tonic-gate */
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate static FILE *
open_temp(tname)378*0Sstevel@tonic-gate open_temp(tname)
379*0Sstevel@tonic-gate char *tname;
380*0Sstevel@tonic-gate {
381*0Sstevel@tonic-gate FILE *fp; /* fp associated with tname */
382*0Sstevel@tonic-gate struct sigaction sigact; /* for signal handling */
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate sigact.sa_flags = 0;
385*0Sstevel@tonic-gate sigact.sa_handler = SIG_IGN;
386*0Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask);
387*0Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGHUP);
388*0Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGINT);
389*0Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGQUIT);
390*0Sstevel@tonic-gate (void) sigaction(SIGHUP, &sigact, NULL);
391*0Sstevel@tonic-gate (void) sigaction(SIGINT, &sigact, NULL);
392*0Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sigact, NULL);
393*0Sstevel@tonic-gate (void)umask(0333);
394*0Sstevel@tonic-gate if (access(tname, 0) != -1) {
395*0Sstevel@tonic-gate (void)fprintf(stderr,"tempfile busy; try again later.\n");
396*0Sstevel@tonic-gate exit(1);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate fp = fopen(tname, "w");
399*0Sstevel@tonic-gate if (fp == NULL) {
400*0Sstevel@tonic-gate perror("Cannot create tempfile");
401*0Sstevel@tonic-gate exit(1);
402*0Sstevel@tonic-gate }
403*0Sstevel@tonic-gate return(fp);
404*0Sstevel@tonic-gate }
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gate /*
407*0Sstevel@tonic-gate * copy_file - copy information from one file to another, return 0 on
408*0Sstevel@tonic-gate * success, -1 on failure
409*0Sstevel@tonic-gate *
410*0Sstevel@tonic-gate * args: fp - source file's file pointer
411*0Sstevel@tonic-gate * tfp - destination file's file pointer
412*0Sstevel@tonic-gate * start - starting line number
413*0Sstevel@tonic-gate * finish - ending line number (-1 indicates entire file)
414*0Sstevel@tonic-gate */
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate
417*0Sstevel@tonic-gate static int
copy_file(fp,tfp,start,finish)418*0Sstevel@tonic-gate copy_file(fp, tfp, start, finish)
419*0Sstevel@tonic-gate FILE *fp;
420*0Sstevel@tonic-gate FILE *tfp;
421*0Sstevel@tonic-gate register int start;
422*0Sstevel@tonic-gate register int finish;
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate register int i; /* loop variable */
425*0Sstevel@tonic-gate char dummy[BUFSIZ]; /* scratch buffer */
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate * always start from the beginning because line numbers are absolute
429*0Sstevel@tonic-gate */
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate rewind(fp);
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate /*
434*0Sstevel@tonic-gate * get to the starting point of interest
435*0Sstevel@tonic-gate */
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gate if (start != 1) {
438*0Sstevel@tonic-gate for (i = 1; i < start; i++)
439*0Sstevel@tonic-gate if (!fgets(dummy, BUFSIZ, fp))
440*0Sstevel@tonic-gate return(-1);
441*0Sstevel@tonic-gate }
442*0Sstevel@tonic-gate
443*0Sstevel@tonic-gate /*
444*0Sstevel@tonic-gate * copy as much as was requested
445*0Sstevel@tonic-gate */
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate if (finish != -1) {
448*0Sstevel@tonic-gate for (i = start; i <= finish; i++) {
449*0Sstevel@tonic-gate if (!fgets(dummy, BUFSIZ, fp))
450*0Sstevel@tonic-gate return(-1);
451*0Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
452*0Sstevel@tonic-gate return(-1);
453*0Sstevel@tonic-gate }
454*0Sstevel@tonic-gate }
455*0Sstevel@tonic-gate else {
456*0Sstevel@tonic-gate for (;;) {
457*0Sstevel@tonic-gate if (fgets(dummy, BUFSIZ, fp) == NULL) {
458*0Sstevel@tonic-gate if (feof(fp))
459*0Sstevel@tonic-gate break;
460*0Sstevel@tonic-gate else
461*0Sstevel@tonic-gate return(-1);
462*0Sstevel@tonic-gate }
463*0Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
464*0Sstevel@tonic-gate return(-1);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate return(0);
468*0Sstevel@tonic-gate }
469*0Sstevel@tonic-gate
470*0Sstevel@tonic-gate /*
471*0Sstevel@tonic-gate * check_ref - to check if nextlabel are referencing
472*0Sstevel@tonic-gate * existing ttylabel
473*0Sstevel@tonic-gate */
474*0Sstevel@tonic-gate static void
check_ref()475*0Sstevel@tonic-gate check_ref()
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate int i;
478*0Sstevel@tonic-gate struct Gdef *np;
479*0Sstevel@tonic-gate extern struct Gdef *find_def();
480*0Sstevel@tonic-gate np = &Gdef[0];
481*0Sstevel@tonic-gate for (i = 0; i < Ndefs; i++, np++) {
482*0Sstevel@tonic-gate if (find_def(np->g_nextid) == NULL) {
483*0Sstevel@tonic-gate (void)printf("Warning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
484*0Sstevel@tonic-gate np->g_nextid, np->g_id);
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate return;
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate
490*0Sstevel@tonic-gate /*
491*0Sstevel@tonic-gate * log - print a message to stdout
492*0Sstevel@tonic-gate */
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate void
log(const char * msg,...)495*0Sstevel@tonic-gate log(const char *msg, ...)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate va_list ap;
498*0Sstevel@tonic-gate if (lflg) {
499*0Sstevel@tonic-gate va_start(ap, msg);
500*0Sstevel@tonic-gate (void) vprintf(msg, ap);
501*0Sstevel@tonic-gate va_end(ap);
502*0Sstevel@tonic-gate (void) printf("\n");
503*0Sstevel@tonic-gate } else {
504*0Sstevel@tonic-gate va_start(ap, msg);
505*0Sstevel@tonic-gate (void) vfprintf(stderr, msg, ap);
506*0Sstevel@tonic-gate va_end(ap);
507*0Sstevel@tonic-gate (void) fprintf(stderr,"\n");
508*0Sstevel@tonic-gate }
509*0Sstevel@tonic-gate }
510