xref: /illumos-gate/usr/src/cmd/ttymon/sttydefs.c (revision 3bb2c156)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*3bb2c156SToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <termio.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <stdarg.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "tmstruct.h"
42*3bb2c156SToomas Soome #include "tmextern.h"
437c478bd9Sstevel@tonic-gate #include "ttymon.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static	int  nflg = 0;		/* -n seen */
467c478bd9Sstevel@tonic-gate static	int  iflg = 0;		/* -i seen */
477c478bd9Sstevel@tonic-gate static	int  fflg = 0;		/* -f seen */
487c478bd9Sstevel@tonic-gate static	int  lflg = 0;		/* -l seen */
497c478bd9Sstevel@tonic-gate 
50*3bb2c156SToomas Soome static	void	usage(void);
51*3bb2c156SToomas Soome static	void	check_ref(void);
52*3bb2c156SToomas Soome static	void	add_entry(struct Gdef *) __NORETURN;
53*3bb2c156SToomas Soome static	void	remove_entry(char *);
54*3bb2c156SToomas Soome static	int	copy_file(FILE *, FILE *, int, int);
55*3bb2c156SToomas Soome static	int	verify(char *, int);
56*3bb2c156SToomas Soome static	FILE	*open_temp(char *);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  *	sttydefs - add, remove or check entries in /etc/ttydefs
607c478bd9Sstevel@tonic-gate  *
61*3bb2c156SToomas Soome  *	Usage:	   sttydefs -a ttylabel [-n nextlabel] [-i initail-flags]
627c478bd9Sstevel@tonic-gate  *			    [-f final-flags] [-b]
637c478bd9Sstevel@tonic-gate  *		   sttydefs -r ttylabel
647c478bd9Sstevel@tonic-gate  *		   sttydefs -l [ttylabel]
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])697c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int c;			/* option letter */
727c478bd9Sstevel@tonic-gate 	int errflg = 0;		/* error indicator */
737c478bd9Sstevel@tonic-gate 	int  aflg = 0;		/* -a seen */
747c478bd9Sstevel@tonic-gate 	int  bflg = 0;		/* -b seen */
757c478bd9Sstevel@tonic-gate 	int	ret;
76*3bb2c156SToomas Soome 	const char *argtmp;
777c478bd9Sstevel@tonic-gate 	char	*nextlabel;
787c478bd9Sstevel@tonic-gate 	struct Gdef ttydef, *ptr;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (argc == 1)
817c478bd9Sstevel@tonic-gate 		usage();
827c478bd9Sstevel@tonic-gate 
83*3bb2c156SToomas Soome 	/* Initialize ttydef structure */
84*3bb2c156SToomas Soome 	memset(&ttydef, 0, sizeof (ttydef));
85*3bb2c156SToomas Soome 
867c478bd9Sstevel@tonic-gate 	ptr = &ttydef;
877c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "a:n:i:f:br:l")) != -1) {
887c478bd9Sstevel@tonic-gate 		switch (c) {
897c478bd9Sstevel@tonic-gate 		case 'a':
907c478bd9Sstevel@tonic-gate 			aflg = TRUE;
917c478bd9Sstevel@tonic-gate 			ptr->g_id = optarg;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case 'n':
947c478bd9Sstevel@tonic-gate 			nflg = TRUE;
957c478bd9Sstevel@tonic-gate 			ptr->g_nextid = optarg;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case 'i':
987c478bd9Sstevel@tonic-gate 			iflg = TRUE;
997c478bd9Sstevel@tonic-gate 			ptr->g_iflags = optarg;
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 		case 'f':
1027c478bd9Sstevel@tonic-gate 			fflg = TRUE;
1037c478bd9Sstevel@tonic-gate 			ptr->g_fflags = optarg;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 'b':
1067c478bd9Sstevel@tonic-gate 			bflg = TRUE;
1077c478bd9Sstevel@tonic-gate 			ptr->g_autobaud |= A_FLAG;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		case 'r':
1107c478bd9Sstevel@tonic-gate 			if ((argc > 3) || (optind < argc))
1117c478bd9Sstevel@tonic-gate 				usage();
1127c478bd9Sstevel@tonic-gate 			remove_entry(optarg);
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'l':
1157c478bd9Sstevel@tonic-gate 			lflg = TRUE;
116*3bb2c156SToomas Soome 			if (argc > 3)
1177c478bd9Sstevel@tonic-gate 				usage();
1187c478bd9Sstevel@tonic-gate 			if ((ret = check_version(TTYDEFS_VERS, TTYDEFS)) != 0) {
1197c478bd9Sstevel@tonic-gate 				if (ret != 2) {
120*3bb2c156SToomas Soome 					(void) fprintf(stderr,
121*3bb2c156SToomas Soome 					    "%s version number is incorrect "
122*3bb2c156SToomas Soome 					    "or missing.\n", TTYDEFS);
1237c478bd9Sstevel@tonic-gate 					exit(1);
1247c478bd9Sstevel@tonic-gate 				}
125*3bb2c156SToomas Soome 				(void) fprintf(stderr,
126*3bb2c156SToomas Soome 				    "sttydefs: can't open %s.\n", TTYDEFS);
1277c478bd9Sstevel@tonic-gate 				exit(1);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 			if (argv[optind] == NULL) {
130*3bb2c156SToomas Soome 				read_ttydefs(NULL, TRUE);
1317c478bd9Sstevel@tonic-gate 				printf("\n");
1327c478bd9Sstevel@tonic-gate 				check_ref();
133*3bb2c156SToomas Soome 			} else {
1347c478bd9Sstevel@tonic-gate 				if (argc == 3) { /* -l ttylabel */
135*3bb2c156SToomas Soome 					if (verify(argv[optind], 0) != 0) {
1367c478bd9Sstevel@tonic-gate 						errflg++;
1377c478bd9Sstevel@tonic-gate 						break;
1387c478bd9Sstevel@tonic-gate 					}
1397c478bd9Sstevel@tonic-gate 					argtmp = argv[optind];
140*3bb2c156SToomas Soome 				} else { /* -lttylabel */
141*3bb2c156SToomas Soome 					argtmp = argv[optind] + 2;
1427c478bd9Sstevel@tonic-gate 				}
143*3bb2c156SToomas Soome 				read_ttydefs(argtmp, TRUE);
1447c478bd9Sstevel@tonic-gate 				if (Ndefs == 0) {
145*3bb2c156SToomas Soome 					(void) fprintf(stderr,
146*3bb2c156SToomas Soome 					    "ttylabel <%s> not found.\n",
147*3bb2c156SToomas Soome 					    argtmp);
1487c478bd9Sstevel@tonic-gate 					exit(1);
1497c478bd9Sstevel@tonic-gate 				}
1507c478bd9Sstevel@tonic-gate 				nextlabel = Gdef[--Ndefs].g_nextid;
1517c478bd9Sstevel@tonic-gate 				Ndefs = 0;
152*3bb2c156SToomas Soome 				read_ttydefs(nextlabel, FALSE);
1537c478bd9Sstevel@tonic-gate 				if (Ndefs == 0) {
154*3bb2c156SToomas Soome 					(void) printf("\nWarning -- nextlabel "
155*3bb2c156SToomas Soome 					    "<%s> of <%s> does not reference "
156*3bb2c156SToomas Soome 					    "any existing ttylabel.\n",
157*3bb2c156SToomas Soome 					    nextlabel, argtmp);
1587c478bd9Sstevel@tonic-gate 				}
1597c478bd9Sstevel@tonic-gate 			}
1607c478bd9Sstevel@tonic-gate 			exit(0);
1617c478bd9Sstevel@tonic-gate 		case '?':
1627c478bd9Sstevel@tonic-gate 			errflg++;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		} /* end switch */
165*3bb2c156SToomas Soome 		if (errflg)
1667c478bd9Sstevel@tonic-gate 			usage();
1677c478bd9Sstevel@tonic-gate 	} /* end while */
168*3bb2c156SToomas Soome 	if (optind < argc)
1697c478bd9Sstevel@tonic-gate 		usage();
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (aflg) {
172*3bb2c156SToomas Soome 		add_entry(ptr);		/* never return */
1737c478bd9Sstevel@tonic-gate 	}
174*3bb2c156SToomas Soome 	if ((iflg) || (fflg) || (bflg) || (nflg))
1757c478bd9Sstevel@tonic-gate 		usage();
176*3bb2c156SToomas Soome 	return (0);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  *	verify	- to check if arg is valid
1817c478bd9Sstevel@tonic-gate  *		- i.e. arg cannot start with '-' and
1827c478bd9Sstevel@tonic-gate  *		  arg must not longer than maxarglen
1837c478bd9Sstevel@tonic-gate  *		- return 0 if ok. Otherwise return -1
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static	int
verify(char * arg,int maxarglen)186*3bb2c156SToomas Soome verify(char *arg, int maxarglen)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	if (*arg == '-') {
189*3bb2c156SToomas Soome 		(void) fprintf(stderr, "Invalid argument -- %s.\n", arg);
190*3bb2c156SToomas Soome 		return (-1);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if ((maxarglen) && ((int)strlen(arg) > maxarglen)) {
1937c478bd9Sstevel@tonic-gate 		arg[maxarglen] = '\0';
194*3bb2c156SToomas Soome 		(void) fprintf(stderr, "string too long, truncated to %s.\n",
195*3bb2c156SToomas Soome 		    arg);
196*3bb2c156SToomas Soome 		return (-1);
1977c478bd9Sstevel@tonic-gate 	}
198*3bb2c156SToomas Soome 	return (0);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * usage - print out a usage message
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static	void
usage(void)206*3bb2c156SToomas Soome usage(void)
2077c478bd9Sstevel@tonic-gate {
208*3bb2c156SToomas Soome 	(void) fprintf(stderr, "Usage:\tsttydefs -a ttylabel [-n nextlabel] "
209*3bb2c156SToomas Soome 	    "[-i initial-flags]\n\t\t [-f final-flags] [-b]\n");
210*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\tsttydefs -r ttylabel\n");
211*3bb2c156SToomas Soome 	(void) fprintf(stderr, "\tsttydefs -l [ttylabel]\n");
2127c478bd9Sstevel@tonic-gate 	exit(2);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  *	add_entry - add an entry to /etc/ttydefs
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static	void
add_entry(struct Gdef * ttydef)220*3bb2c156SToomas Soome add_entry(struct Gdef *ttydef)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	FILE *fp;
2237c478bd9Sstevel@tonic-gate 	int	errflg = 0;
2247c478bd9Sstevel@tonic-gate 	char tbuf[BUFSIZ], *tp;
2257c478bd9Sstevel@tonic-gate 	int  add_version = FALSE;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (getuid()) {
228*3bb2c156SToomas Soome 		(void) fprintf(stderr, "User not privileged for operation.\n");
2297c478bd9Sstevel@tonic-gate 		exit(1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	tp = tbuf;
2327c478bd9Sstevel@tonic-gate 	*tp = '\0';
2337c478bd9Sstevel@tonic-gate 	if ((fp = fopen(TTYDEFS, "r")) != NULL) {
2347c478bd9Sstevel@tonic-gate 		if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
235*3bb2c156SToomas Soome 			(void) fprintf(stderr,
236*3bb2c156SToomas Soome 			    "%s version number is incorrect or missing.\n",
237*3bb2c156SToomas Soome 			    TTYDEFS);
2387c478bd9Sstevel@tonic-gate 			exit(1);
2397c478bd9Sstevel@tonic-gate 		}
240*3bb2c156SToomas Soome 		if (find_label(fp, ttydef->g_id)) {
241*3bb2c156SToomas Soome 			(void) fclose(fp);
242*3bb2c156SToomas Soome 			(void) fprintf(stderr,
243*3bb2c156SToomas Soome 			    "Invalid request -- ttylabel <%s> already "
244*3bb2c156SToomas Soome 			    "exists.\n",
245*3bb2c156SToomas Soome 			    ttydef->g_id);
2467c478bd9Sstevel@tonic-gate 			exit(1);
247*3bb2c156SToomas Soome 		}
248*3bb2c156SToomas Soome 		(void) fclose(fp);
249*3bb2c156SToomas Soome 	} else  {
2507c478bd9Sstevel@tonic-gate 		add_version = TRUE;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if ((fp = fopen(TTYDEFS, "a+")) == NULL) {
2537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
2547c478bd9Sstevel@tonic-gate 		    strerror(errno));
2557c478bd9Sstevel@tonic-gate 		exit(1);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (add_version) {
259*3bb2c156SToomas Soome 		(void) fprintf(fp, "# VERSION=%d\n", TTYDEFS_VERS);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* if optional fields are not provided, set to default */
2647c478bd9Sstevel@tonic-gate 	if (!iflg)
2657c478bd9Sstevel@tonic-gate 		ttydef->g_iflags = DEFAULT.g_iflags;
2667c478bd9Sstevel@tonic-gate 	else
267*3bb2c156SToomas Soome 		if (check_flags(ttydef->g_iflags) != 0)
2687c478bd9Sstevel@tonic-gate 			errflg++;
2697c478bd9Sstevel@tonic-gate 	if (!fflg)
2707c478bd9Sstevel@tonic-gate 		ttydef->g_fflags = DEFAULT.g_fflags;
2717c478bd9Sstevel@tonic-gate 	else
272*3bb2c156SToomas Soome 		if (check_flags(ttydef->g_fflags) != 0)
2737c478bd9Sstevel@tonic-gate 			errflg++;
2747c478bd9Sstevel@tonic-gate 	if (errflg)
2757c478bd9Sstevel@tonic-gate 		exit(1);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (!nflg)
2787c478bd9Sstevel@tonic-gate 		ttydef->g_nextid = ttydef->g_id;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (ttydef->g_autobaud & A_FLAG)  {
281*3bb2c156SToomas Soome 		(void) fprintf(fp, "%s:%s:%s:A:%s\n", ttydef->g_id,
282*3bb2c156SToomas Soome 		    ttydef->g_iflags, ttydef->g_fflags, ttydef->g_nextid);
283*3bb2c156SToomas Soome 	} else {
284*3bb2c156SToomas Soome 		(void) fprintf(fp, "%s:%s:%s::%s\n", ttydef->g_id,
285*3bb2c156SToomas Soome 		    ttydef->g_iflags, ttydef->g_fflags, ttydef->g_nextid);
2867c478bd9Sstevel@tonic-gate 	}
287*3bb2c156SToomas Soome 	(void) fclose(fp);
2887c478bd9Sstevel@tonic-gate 	exit(0);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static	void
remove_entry(char * ttylabel)292*3bb2c156SToomas Soome remove_entry(char *ttylabel)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	FILE *tfp;		/* file pointer for temp file */
2957c478bd9Sstevel@tonic-gate 	int line;		/* line number entry is on */
2967c478bd9Sstevel@tonic-gate 	FILE *fp;		/* scratch file pointer */
2977c478bd9Sstevel@tonic-gate 	char *tname = "/etc/.ttydefs";
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (getuid()) {
300*3bb2c156SToomas Soome 		(void) fprintf(stderr, "User not privileged for operation.\n");
3017c478bd9Sstevel@tonic-gate 		exit(1);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	fp = fopen(TTYDEFS, "r");
3047c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
3067c478bd9Sstevel@tonic-gate 		    strerror(errno));
3077c478bd9Sstevel@tonic-gate 		exit(1);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
310*3bb2c156SToomas Soome 		(void) fprintf(stderr,
311*3bb2c156SToomas Soome 		    "%s version number is incorrect or missing.\n", TTYDEFS);
3127c478bd9Sstevel@tonic-gate 		exit(1);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	if ((line = find_label(fp, ttylabel)) == 0) {
315*3bb2c156SToomas Soome 		(void) fprintf(stderr,
316*3bb2c156SToomas Soome 		    "Invalid request, ttylabel <%s> does not exist.\n",
317*3bb2c156SToomas Soome 		    ttylabel);
3187c478bd9Sstevel@tonic-gate 		exit(1);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	tfp = open_temp(tname);
321*3bb2c156SToomas Soome 	if (line != 1)
3227c478bd9Sstevel@tonic-gate 		if (copy_file(fp, tfp, 1, line - 1)) {
323*3bb2c156SToomas Soome 			(void) fprintf(stderr, "Error accessing temp file.\n");
3247c478bd9Sstevel@tonic-gate 			exit(1);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 	if (copy_file(fp, tfp, line + 1, -1)) {
327*3bb2c156SToomas Soome 		(void) fprintf(stderr, "Error accessing temp file.\n");
3287c478bd9Sstevel@tonic-gate 		exit(1);
3297c478bd9Sstevel@tonic-gate 	}
330*3bb2c156SToomas Soome 	(void) fclose(fp);
3317c478bd9Sstevel@tonic-gate 	if (fclose(tfp) == EOF) {
332*3bb2c156SToomas Soome 		(void) unlink(tname);
333*3bb2c156SToomas Soome 		(void) fprintf(stderr, "Error closing temp file.\n");
3347c478bd9Sstevel@tonic-gate 		exit(1);
3357c478bd9Sstevel@tonic-gate 	}
336*3bb2c156SToomas Soome 	(void) unlink(TTYDEFS);
337*3bb2c156SToomas Soome 	if (rename(tname, TTYDEFS) != 0) {
3387c478bd9Sstevel@tonic-gate 		perror("Rename failed");
339*3bb2c156SToomas Soome 		(void) unlink(tname);
3407c478bd9Sstevel@tonic-gate 		exit(1);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	exit(0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * open_temp - open up a temp file
3477c478bd9Sstevel@tonic-gate  *
3487c478bd9Sstevel@tonic-gate  *	args:	tname - temp file name
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static	FILE *
open_temp(char * tname)352*3bb2c156SToomas Soome open_temp(char *tname)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	FILE *fp;			/* fp associated with tname */
3557c478bd9Sstevel@tonic-gate 	struct sigaction sigact;	/* for signal handling */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	sigact.sa_flags = 0;
3587c478bd9Sstevel@tonic-gate 	sigact.sa_handler = SIG_IGN;
3597c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sigact.sa_mask);
3607c478bd9Sstevel@tonic-gate 	(void) sigaddset(&sigact.sa_mask, SIGHUP);
3617c478bd9Sstevel@tonic-gate 	(void) sigaddset(&sigact.sa_mask, SIGINT);
3627c478bd9Sstevel@tonic-gate 	(void) sigaddset(&sigact.sa_mask, SIGQUIT);
3637c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &sigact, NULL);
3647c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sigact, NULL);
3657c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGQUIT, &sigact, NULL);
366*3bb2c156SToomas Soome 	(void) umask(0333);
3677c478bd9Sstevel@tonic-gate 	if (access(tname, 0) != -1) {
368*3bb2c156SToomas Soome 		(void) fprintf(stderr, "tempfile busy; try again later.\n");
3697c478bd9Sstevel@tonic-gate 		exit(1);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	fp = fopen(tname, "w");
3727c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3737c478bd9Sstevel@tonic-gate 		perror("Cannot create tempfile");
3747c478bd9Sstevel@tonic-gate 		exit(1);
3757c478bd9Sstevel@tonic-gate 	}
376*3bb2c156SToomas Soome 	return (fp);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * copy_file - copy information from one file to another, return 0 on
3817c478bd9Sstevel@tonic-gate  *	success, -1 on failure
3827c478bd9Sstevel@tonic-gate  *
3837c478bd9Sstevel@tonic-gate  *	args:	fp - source file's file pointer
3847c478bd9Sstevel@tonic-gate  *		tfp - destination file's file pointer
3857c478bd9Sstevel@tonic-gate  *		start - starting line number
3867c478bd9Sstevel@tonic-gate  *		finish - ending line number (-1 indicates entire file)
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static	int
copy_file(FILE * fp,FILE * tfp,int start,int finish)391*3bb2c156SToomas Soome copy_file(FILE *fp, FILE *tfp, int start, int finish)
3927c478bd9Sstevel@tonic-gate {
393*3bb2c156SToomas Soome 	int i;		/* loop variable */
3947c478bd9Sstevel@tonic-gate 	char dummy[BUFSIZ];	/* scratch buffer */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * always start from the beginning because line numbers are absolute
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	rewind(fp);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * get to the starting point of interest
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (start != 1) {
4077c478bd9Sstevel@tonic-gate 		for (i = 1; i < start; i++)
4087c478bd9Sstevel@tonic-gate 			if (!fgets(dummy, BUFSIZ, fp))
409*3bb2c156SToomas Soome 				return (-1);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * copy as much as was requested
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (finish != -1) {
4177c478bd9Sstevel@tonic-gate 		for (i = start; i <= finish; i++) {
4187c478bd9Sstevel@tonic-gate 			if (!fgets(dummy, BUFSIZ, fp))
419*3bb2c156SToomas Soome 				return (-1);
4207c478bd9Sstevel@tonic-gate 			if (fputs(dummy, tfp) == EOF)
421*3bb2c156SToomas Soome 				return (-1);
4227c478bd9Sstevel@tonic-gate 		}
423*3bb2c156SToomas Soome 	} else {
4247c478bd9Sstevel@tonic-gate 		for (;;) {
4257c478bd9Sstevel@tonic-gate 			if (fgets(dummy, BUFSIZ, fp) == NULL) {
4267c478bd9Sstevel@tonic-gate 				if (feof(fp))
4277c478bd9Sstevel@tonic-gate 					break;
4287c478bd9Sstevel@tonic-gate 				else
429*3bb2c156SToomas Soome 					return (-1);
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 			if (fputs(dummy, tfp) == EOF)
432*3bb2c156SToomas Soome 				return (-1);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
435*3bb2c156SToomas Soome 	return (0);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  *	check_ref	- to check if nextlabel are referencing
4407c478bd9Sstevel@tonic-gate  *			  existing ttylabel
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate static	void
check_ref(void)443*3bb2c156SToomas Soome check_ref(void)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	int	i;
4467c478bd9Sstevel@tonic-gate 	struct	Gdef	*np;
447*3bb2c156SToomas Soome 
4487c478bd9Sstevel@tonic-gate 	np = &Gdef[0];
4497c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ndefs; i++, np++) {
4507c478bd9Sstevel@tonic-gate 		if (find_def(np->g_nextid) == NULL) {
451*3bb2c156SToomas Soome 			(void) printf("Warning -- nextlabel <%s> of <%s> "
452*3bb2c156SToomas Soome 			    "does not reference any existing ttylabel.\n",
453*3bb2c156SToomas Soome 			    np->g_nextid, np->g_id);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  *	log	- print a message to stdout
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate void
log(const char * msg,...)4637c478bd9Sstevel@tonic-gate log(const char *msg, ...)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	va_list ap;
4667c478bd9Sstevel@tonic-gate 	if (lflg) {
4677c478bd9Sstevel@tonic-gate 		va_start(ap, msg);
4687c478bd9Sstevel@tonic-gate 		(void) vprintf(msg, ap);
4697c478bd9Sstevel@tonic-gate 		va_end(ap);
4707c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4717c478bd9Sstevel@tonic-gate 	} else {
4727c478bd9Sstevel@tonic-gate 		va_start(ap, msg);
4737c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, msg, ap);
4747c478bd9Sstevel@tonic-gate 		va_end(ap);
475*3bb2c156SToomas Soome 		(void) fprintf(stderr, "\n");
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate }
478