17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include <stdio.h>
347c478bdstevel@tonic-gate#include <unistd.h>
357c478bdstevel@tonic-gate#include <stdlib.h>
367c478bdstevel@tonic-gate#include <errno.h>
377c478bdstevel@tonic-gate#include <sys/types.h>
387c478bdstevel@tonic-gate#include <ctype.h>
397c478bdstevel@tonic-gate#include <string.h>
407c478bdstevel@tonic-gate#include <termio.h>
417c478bdstevel@tonic-gate#include <sys/stat.h>
427c478bdstevel@tonic-gate#include <signal.h>
437c478bdstevel@tonic-gate#include <stdarg.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#include "tmstruct.h"
467c478bdstevel@tonic-gate#include "ttymon.h"
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gatestatic	int  nflg = 0;		/* -n seen */
497c478bdstevel@tonic-gatestatic	int  iflg = 0;		/* -i seen */
507c478bdstevel@tonic-gatestatic	int  fflg = 0;		/* -f seen */
517c478bdstevel@tonic-gatestatic	int  lflg = 0;		/* -l seen */
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gatestruct  Gdef Gdef[MAXDEFS];	/* array to hold entries in /etc/ttydefs */
547c478bdstevel@tonic-gateint	Ndefs = 0;		/* highest index to Gdef that was used   */
557c478bdstevel@tonic-gatestatic	struct Gdef DEFAULT = {		/* default terminal settings	*/
567c478bdstevel@tonic-gate	"default",
577c478bdstevel@tonic-gate	"9600",
587c478bdstevel@tonic-gate	"9600 sane",
597c478bdstevel@tonic-gate	0,
607c478bdstevel@tonic-gate	/*
617c478bdstevel@tonic-gate	 * next label is set to 4800 so we can start searching ttydefs.
627c478bdstevel@tonic-gate	 * if 4800 is not in ttydefs, we will loop back to use DEFAULT
637c478bdstevel@tonic-gate	 */
647c478bdstevel@tonic-gate	"4800"
657c478bdstevel@tonic-gate};
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gatestatic	void	usage();
697c478bdstevel@tonic-gatestatic	void	check_ref();
707c478bdstevel@tonic-gatestatic	void	add_entry();
717c478bdstevel@tonic-gatestatic	void	remove_entry();
727c478bdstevel@tonic-gatestatic	int	copy_file();
737c478bdstevel@tonic-gatestatic	int	verify();
747c478bdstevel@tonic-gatestatic	FILE	*open_temp();
757c478bdstevel@tonic-gateextern  void	read_ttydefs();
767c478bdstevel@tonic-gateextern  int	check_version();
777c478bdstevel@tonic-gateextern  int	find_label();
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate/*
807c478bdstevel@tonic-gate *	sttydefs - add, remove or check entries in /etc/ttydefs
817c478bdstevel@tonic-gate *
827c478bdstevel@tonic-gate *	Usage:	   sttydefs -a ttylabel [-n nextlabel] [-i initail-flags]
837c478bdstevel@tonic-gate *			    [-f final-flags] [-b]
847c478bdstevel@tonic-gate *		   sttydefs -r ttylabel
857c478bdstevel@tonic-gate *		   sttydefs -l [ttylabel]
867c478bdstevel@tonic-gate *
877c478bdstevel@tonic-gate */
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gateint
907c478bdstevel@tonic-gatemain(int argc, char *argv[])
917c478bdstevel@tonic-gate{
927c478bdstevel@tonic-gate	int c;			/* option letter */
937c478bdstevel@tonic-gate	int errflg = 0;		/* error indicator */
947c478bdstevel@tonic-gate	int  aflg = 0;		/* -a seen */
957c478bdstevel@tonic-gate	int  bflg = 0;		/* -b seen */
967c478bdstevel@tonic-gate	int	ret;
977c478bdstevel@tonic-gate#ifdef __STDC__
987c478bdstevel@tonic-gate	const
997c478bdstevel@tonic-gate#endif
1007c478bdstevel@tonic-gate	char	*argtmp;
1017c478bdstevel@tonic-gate	char	*nextlabel;
1027c478bdstevel@tonic-gate	struct Gdef ttydef, *ptr;
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	extern	char	*optarg;
1057c478bdstevel@tonic-gate	extern	int	optind;
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	if (argc == 1)
1087c478bdstevel@tonic-gate		usage();
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate        /* Initialize ttydef structure */
1117c478bdstevel@tonic-gate        memset (&ttydef, 0, sizeof(ttydef));
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate	ptr = &ttydef;
1147c478bdstevel@tonic-gate	while ((c = getopt(argc, argv, "a:n:i:f:br:l")) != -1) {
1157c478bdstevel@tonic-gate		switch (c) {
1167c478bdstevel@tonic-gate		case 'a':
1177c478bdstevel@tonic-gate			aflg = TRUE;
1187c478bdstevel@tonic-gate			ptr->g_id = optarg;
1197c478bdstevel@tonic-gate			break;
1207c478bdstevel@tonic-gate		case 'n':
1217c478bdstevel@tonic-gate			nflg = TRUE;
1227c478bdstevel@tonic-gate			ptr->g_nextid = optarg;
1237c478bdstevel@tonic-gate			break;
1247c478bdstevel@tonic-gate		case 'i':
1257c478bdstevel@tonic-gate			iflg = TRUE;
1267c478bdstevel@tonic-gate			ptr->g_iflags = optarg;
1277c478bdstevel@tonic-gate			break;
1287c478bdstevel@tonic-gate		case 'f':
1297c478bdstevel@tonic-gate			fflg = TRUE;
1307c478bdstevel@tonic-gate			ptr->g_fflags = optarg;
1317c478bdstevel@tonic-gate			break;
1327c478bdstevel@tonic-gate		case 'b':
1337c478bdstevel@tonic-gate			bflg = TRUE;
1347c478bdstevel@tonic-gate			ptr->g_autobaud |= A_FLAG;
1357c478bdstevel@tonic-gate			break;
1367c478bdstevel@tonic-gate		case 'r':
1377c478bdstevel@tonic-gate			if ((argc > 3) || (optind < argc))
1387c478bdstevel@tonic-gate				usage();
1397c478bdstevel@tonic-gate			remove_entry(optarg);
1407c478bdstevel@tonic-gate			break;
1417c478bdstevel@tonic-gate		case 'l':
1427c478bdstevel@tonic-gate			lflg = TRUE;
1437c478bdstevel@tonic-gate			if (argc > 3)
1447c478bdstevel@tonic-gate				usage();
1457c478bdstevel@tonic-gate			if ((ret = check_version(TTYDEFS_VERS, TTYDEFS)) != 0) {
1467c478bdstevel@tonic-gate				if (ret != 2) {
1477c478bdstevel@tonic-gate					(void)fprintf(stderr, "%s version number is incorrect or missing.\n",TTYDEFS);
1487c478bdstevel@tonic-gate					exit(1);
1497c478bdstevel@tonic-gate				}
1507c478bdstevel@tonic-gate				(void)fprintf(stderr, "sttydefs: can't open %s.\n",TTYDEFS);
1517c478bdstevel@tonic-gate				exit(1);
1527c478bdstevel@tonic-gate			}
1537c478bdstevel@tonic-gate			if (argv[optind] == NULL) {
1547c478bdstevel@tonic-gate				read_ttydefs(NULL,TRUE);
1557c478bdstevel@tonic-gate				printf("\n");
1567c478bdstevel@tonic-gate				check_ref();
1577c478bdstevel@tonic-gate			}
1587c478bdstevel@tonic-gate			else {
1597c478bdstevel@tonic-gate				if (argc == 3) { /* -l ttylabel */
1607c478bdstevel@tonic-gate					if (verify(argv[optind],0) != 0) {
1617c478bdstevel@tonic-gate						errflg++;
1627c478bdstevel@tonic-gate						break;
1637c478bdstevel@tonic-gate					}
1647c478bdstevel@tonic-gate					argtmp = argv[optind];
1657c478bdstevel@tonic-gate				}
1667c478bdstevel@tonic-gate				else { /* -lttylabel */
1677c478bdstevel@tonic-gate					argtmp = argv[optind]+2;
1687c478bdstevel@tonic-gate				}
1697c478bdstevel@tonic-gate				read_ttydefs(argtmp,TRUE);
1707c478bdstevel@tonic-gate				if (Ndefs == 0) {
1717c478bdstevel@tonic-gate					(void)fprintf(stderr,
1727c478bdstevel@tonic-gate					"ttylabel <%s> not found.\n", argtmp);
1737c478bdstevel@tonic-gate					exit(1);
1747c478bdstevel@tonic-gate				}
1757c478bdstevel@tonic-gate				nextlabel = Gdef[--Ndefs].g_nextid;
1767c478bdstevel@tonic-gate				Ndefs = 0;
1777c478bdstevel@tonic-gate				read_ttydefs(nextlabel,FALSE);
1787c478bdstevel@tonic-gate				if (Ndefs == 0) {
1797c478bdstevel@tonic-gate					(void)printf("\nWarning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
1807c478bdstevel@tonic-gate					nextlabel, argtmp);
1817c478bdstevel@tonic-gate				}
1827c478bdstevel@tonic-gate			}
1837c478bdstevel@tonic-gate			exit(0);
1847c478bdstevel@tonic-gate			break;		/*NOTREACHED*/
1857c478bdstevel@tonic-gate		case '?':
1867c478bdstevel@tonic-gate			errflg++;
1877c478bdstevel@tonic-gate			break;
1887c478bdstevel@tonic-gate		} /* end switch */
1897c478bdstevel@tonic-gate		if (errflg)
1907c478bdstevel@tonic-gate			usage();
1917c478bdstevel@tonic-gate	} /* end while */
1927c478bdstevel@tonic-gate	if (optind < argc)
1937c478bdstevel@tonic-gate		usage();
1947c478bdstevel@tonic-gate
1957c478bdstevel@tonic-gate	if (aflg) {
1967c478bdstevel@tonic-gate		add_entry(ptr); 	/* never return */
1977c478bdstevel@tonic-gate	}
1987c478bdstevel@tonic-gate	if ((iflg) || (fflg) || (bflg) || (nflg))
1997c478bdstevel@tonic-gate		usage();
2007c478bdstevel@tonic-gate	return (0);
2017c478bdstevel@tonic-gate}
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate/*
2047c478bdstevel@tonic-gate *	verify	- to check if arg is valid
2057c478bdstevel@tonic-gate *		- i.e. arg cannot start with '-' and
2067c478bdstevel@tonic-gate *		  arg must not longer than maxarglen
2077c478bdstevel@tonic-gate *		- return 0 if ok. Otherwise return -1
2087c478bdstevel@tonic-gate */
2097c478bdstevel@tonic-gatestatic	int
2107c478bdstevel@tonic-gateverify(arg,maxarglen)
2117c478bdstevel@tonic-gatechar	*arg;
2127c478bdstevel@tonic-gateint	maxarglen;
2137c478bdstevel@tonic-gate{
2147c478bdstevel@tonic-gate	if (*arg == '-') {
2157c478bdstevel@tonic-gate		(void)fprintf(stderr, "Invalid argument -- %s.\n", arg);
2167c478bdstevel@tonic-gate		return(-1);
2177c478bdstevel@tonic-gate	}
2187c478bdstevel@tonic-gate	if ((maxarglen) && ((int)strlen(arg) > maxarglen)) {
2197c478bdstevel@tonic-gate		arg[maxarglen] = '\0';
2207c478bdstevel@tonic-gate		(void)fprintf(stderr,"string too long, truncated to %s.\n",arg);
2217c478bdstevel@tonic-gate		return(-1);
2227c478bdstevel@tonic-gate	}
2237c478bdstevel@tonic-gate	return(0);
2247c478bdstevel@tonic-gate}
2257c478bdstevel@tonic-gate
2267c478bdstevel@tonic-gate/*
2277c478bdstevel@tonic-gate * usage - print out a usage message
2287c478bdstevel@tonic-gate */
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gatestatic	void
2317c478bdstevel@tonic-gateusage()
2327c478bdstevel@tonic-gate{
2337c478bdstevel@tonic-gate	(void)fprintf(stderr, "Usage:\tsttydefs -a ttylabel [-n nextlabel] [-i initial-flags]\n\t\t [-f final-flags] [-b]\n");
2347c478bdstevel@tonic-gate	(void)fprintf(stderr, "\tsttydefs -r ttylabel\n");
2357c478bdstevel@tonic-gate	(void)fprintf(stderr, "\tsttydefs -l [ttylabel]\n");
2367c478bdstevel@tonic-gate	exit(2);
2377c478bdstevel@tonic-gate}
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate/*
2407c478bdstevel@tonic-gate *	add_entry - add an entry to /etc/ttydefs
2417c478bdstevel@tonic-gate */
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gatestatic	void
2447c478bdstevel@tonic-gateadd_entry(ttydef)
2457c478bdstevel@tonic-gatestruct Gdef *ttydef;
2467c478bdstevel@tonic-gate{
2477c478bdstevel@tonic-gate	FILE *fp;
2487c478bdstevel@tonic-gate	int	errflg = 0;
2497c478bdstevel@tonic-gate	char tbuf[BUFSIZ], *tp;
2507c478bdstevel@tonic-gate	int  add_version = FALSE;
2517c478bdstevel@tonic-gate	extern	int	check_flags();
2527c478bdstevel@tonic-gate
2537c478bdstevel@tonic-gate	if (getuid()) {
2547c478bdstevel@tonic-gate		(void)fprintf(stderr, "User not privileged for operation.\n");
2557c478bdstevel@tonic-gate		exit(1);
2567c478bdstevel@tonic-gate	}
2577c478bdstevel@tonic-gate	tp = tbuf;
2587c478bdstevel@tonic-gate	*tp = '\0';
2597c478bdstevel@tonic-gate	if ((fp = fopen(TTYDEFS, "r")) != NULL) {
2607c478bdstevel@tonic-gate		if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
2617c478bdstevel@tonic-gate			(void)fprintf(stderr,
2627c478bdstevel@tonic-gate			"%s version number is incorrect or missing.\n",TTYDEFS);
2637c478bdstevel@tonic-gate			exit(1);
2647c478bdstevel@tonic-gate		}
2657c478bdstevel@tonic-gate		if (find_label(fp,ttydef->g_id)) {
2667c478bdstevel@tonic-gate			(void)fclose(fp);
2677c478bdstevel@tonic-gate			(void)fprintf(stderr,
2687c478bdstevel@tonic-gate			"Invalid request -- ttylabel <%s> already exists.\n",
2697c478bdstevel@tonic-gate			ttydef->g_id);
2707c478bdstevel@tonic-gate			exit(1);
2717c478bdstevel@tonic-gate		}
2727c478bdstevel@tonic-gate		(void)fclose(fp);
2737c478bdstevel@tonic-gate	}
2747c478bdstevel@tonic-gate	else  {
2757c478bdstevel@tonic-gate		add_version = TRUE;
2767c478bdstevel@tonic-gate	}
2777c478bdstevel@tonic-gate	if ((fp = fopen(TTYDEFS, "a+")) == NULL) {
2787c478bdstevel@tonic-gate		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
2797c478bdstevel@tonic-gate		    strerror(errno));
2807c478bdstevel@tonic-gate		exit(1);
2817c478bdstevel@tonic-gate	}
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate	if (add_version) {
2847c478bdstevel@tonic-gate	   (void)fprintf(fp,"# VERSION=%d\n", TTYDEFS_VERS);
2857c478bdstevel@tonic-gate	}
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate
2887c478bdstevel@tonic-gate	/* if optional fields are not provided, set to default */
2897c478bdstevel@tonic-gate	if (!iflg)
2907c478bdstevel@tonic-gate		ttydef->g_iflags = DEFAULT.g_iflags;
2917c478bdstevel@tonic-gate	else
2927c478bdstevel@tonic-gate		if (check_flags(ttydef->g_iflags) != 0 )
2937c478bdstevel@tonic-gate			errflg++;
2947c478bdstevel@tonic-gate	if (!fflg)
2957c478bdstevel@tonic-gate		ttydef->g_fflags = DEFAULT.g_fflags;
2967c478bdstevel@tonic-gate	else
2977c478bdstevel@tonic-gate		if (check_flags(ttydef->g_fflags) != 0 )
2987c478bdstevel@tonic-gate			errflg++;
2997c478bdstevel@tonic-gate	if (errflg)
3007c478bdstevel@tonic-gate		exit(1);
3017c478bdstevel@tonic-gate
3027c478bdstevel@tonic-gate	if (!nflg)
3037c478bdstevel@tonic-gate		ttydef->g_nextid = ttydef->g_id;
3047c478bdstevel@tonic-gate
3057c478bdstevel@tonic-gate	if (ttydef->g_autobaud & A_FLAG)  {
3067c478bdstevel@tonic-gate	   (void)fprintf(fp,"%s:%s:%s:A:%s\n", ttydef->g_id, ttydef->g_iflags,
3077c478bdstevel@tonic-gate		ttydef->g_fflags, ttydef->g_nextid);
3087c478bdstevel@tonic-gate	}
3097c478bdstevel@tonic-gate	else {
3107c478bdstevel@tonic-gate	   (void)fprintf(fp,"%s:%s:%s::%s\n", ttydef->g_id, ttydef->g_iflags,
3117c478bdstevel@tonic-gate		ttydef->g_fflags, ttydef->g_nextid);
3127c478bdstevel@tonic-gate	}
3137c478bdstevel@tonic-gate	(void)fclose(fp);
3147c478bdstevel@tonic-gate	exit(0);
3157c478bdstevel@tonic-gate}
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gatestatic	void
3187c478bdstevel@tonic-gateremove_entry(ttylabel)
3197c478bdstevel@tonic-gatechar	*ttylabel;
3207c478bdstevel@tonic-gate{
3217c478bdstevel@tonic-gate	FILE *tfp;		/* file pointer for temp file */
3227c478bdstevel@tonic-gate	int line;		/* line number entry is on */
3237c478bdstevel@tonic-gate	FILE *fp;		/* scratch file pointer */
3247c478bdstevel@tonic-gate	char *tname = "/etc/.ttydefs";
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate	if (getuid()) {
3277c478bdstevel@tonic-gate		(void)fprintf(stderr, "User not privileged for operation.\n");
3287c478bdstevel@tonic-gate		exit(1);
3297c478bdstevel@tonic-gate	}
3307c478bdstevel@tonic-gate	fp = fopen(TTYDEFS, "r");
3317c478bdstevel@tonic-gate	if (fp == NULL) {
3327c478bdstevel@tonic-gate		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
3337c478bdstevel@tonic-gate		    strerror(errno));
3347c478bdstevel@tonic-gate		exit(1);
3357c478bdstevel@tonic-gate	}
3367c478bdstevel@tonic-gate	if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
3377c478bdstevel@tonic-gate		(void)fprintf(stderr,
3387c478bdstevel@tonic-gate			"%s version number is incorrect or missing.\n",TTYDEFS);
3397c478bdstevel@tonic-gate		exit(1);
3407c478bdstevel@tonic-gate	}
3417c478bdstevel@tonic-gate	if ((line = find_label(fp, ttylabel)) == 0) {
3427c478bdstevel@tonic-gate		(void)fprintf(stderr,
3437c478bdstevel@tonic-gate		"Invalid request, ttylabel <%s> does not exist.\n", ttylabel);
3447c478bdstevel@tonic-gate		exit(1);
3457c478bdstevel@tonic-gate	}
3467c478bdstevel@tonic-gate	tfp = open_temp(tname);
3477c478bdstevel@tonic-gate	if (line != 1)
3487c478bdstevel@tonic-gate		if (copy_file(fp, tfp, 1, line - 1)) {
3497c478bdstevel@tonic-gate			(void)fprintf(stderr,"Error accessing temp file.\n");
3507c478bdstevel@tonic-gate			exit(1);
3517c478bdstevel@tonic-gate		}
3527c478bdstevel@tonic-gate	if (copy_file(fp, tfp, line + 1, -1)) {
3537c478bdstevel@tonic-gate		(void)fprintf(stderr,"Error accessing temp file.\n");
3547c478bdstevel@tonic-gate		exit(1);
3557c478bdstevel@tonic-gate	}
3567c478bdstevel@tonic-gate	(void)fclose(fp);
3577c478bdstevel@tonic-gate	if (fclose(tfp) == EOF) {
3587c478bdstevel@tonic-gate		(void)unlink(tname);
3597c478bdstevel@tonic-gate		(void)fprintf(stderr,"Error closing temp file.\n");
3607c478bdstevel@tonic-gate		exit(1);
3617c478bdstevel@tonic-gate	}
3627c478bdstevel@tonic-gate	(void)unlink(TTYDEFS);
3637c478bdstevel@tonic-gate	if (rename(tname, TTYDEFS) != 0 ) {
3647c478bdstevel@tonic-gate		perror("Rename failed");
3657c478bdstevel@tonic-gate		(void)unlink(tname);
3667c478bdstevel@tonic-gate		exit(1);
3677c478bdstevel@tonic-gate	}
3687c478bdstevel@tonic-gate	exit(0);
3697c478bdstevel@tonic-gate}
3707c478bdstevel@tonic-gate
3717c478bdstevel@tonic-gate/*
3727c478bdstevel@tonic-gate * open_temp - open up a temp file
3737c478bdstevel@tonic-gate *
3747c478bdstevel@tonic-gate *	args:	tname - temp file name
3757c478bdstevel@tonic-gate */
3767c478bdstevel@tonic-gate
3777c478bdstevel@tonic-gatestatic	FILE *
3787c478bdstevel@tonic-gateopen_temp(tname)
3797c478bdstevel@tonic-gatechar *tname;
3807c478bdstevel@tonic-gate{
3817c478bdstevel@tonic-gate	FILE *fp;			/* fp associated with tname */
3827c478bdstevel@tonic-gate	struct sigaction sigact;	/* for signal handling */
3837c478bdstevel@tonic-gate
3847c478bdstevel@tonic-gate	sigact.sa_flags = 0;
3857c478bdstevel@tonic-gate	sigact.sa_handler = SIG_IGN;
3867c478bdstevel@tonic-gate	(void) sigemptyset(&sigact.sa_mask);
3877c478bdstevel@tonic-gate	(void) sigaddset(&sigact.sa_mask, SIGHUP);
3887c478bdstevel@tonic-gate	(void) sigaddset(&sigact.sa_mask, SIGINT);
3897c478bdstevel@tonic-gate	(void) sigaddset(&sigact.sa_mask, SIGQUIT);
3907c478bdstevel@tonic-gate	(void) sigaction(SIGHUP, &sigact, NULL);
3917c478bdstevel@tonic-gate	(void) sigaction(SIGINT, &sigact, NULL);
3927c478bdstevel@tonic-gate	(void) sigaction(SIGQUIT, &sigact, NULL);
3937c478bdstevel@tonic-gate	(void)umask(0333);
3947c478bdstevel@tonic-gate	if (access(tname, 0) != -1) {
3957c478bdstevel@tonic-gate		(void)fprintf(stderr,"tempfile busy; try again later.\n");
3967c478bdstevel@tonic-gate		exit(1);
3977c478bdstevel@tonic-gate	}
3987c478bdstevel@tonic-gate	fp = fopen(tname, "w");
3997c478bdstevel@tonic-gate	if (fp == NULL) {
4007c478bdstevel@tonic-gate		perror("Cannot create tempfile");
4017c478bdstevel@tonic-gate		exit(1);
4027c478bdstevel@tonic-gate	}
4037c478bdstevel@tonic-gate	return(fp);
4047c478bdstevel@tonic-gate}
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gate/*
4077c478bdstevel@tonic-gate * copy_file - copy information from one file to another, return 0 on
4087c478bdstevel@tonic-gate *	success, -1 on failure
4097c478bdstevel@tonic-gate *
4107c478bdstevel@tonic-gate *	args:	fp - source file's file pointer
4117c478bdstevel@tonic-gate *		tfp - destination file's file pointer
4127c478bdstevel@tonic-gate *		start - starting line number
4137c478bdstevel@tonic-gate *		finish - ending line number (-1 indicates entire file)
4147c478bdstevel@tonic-gate */
4157c478bdstevel@tonic-gate
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gatestatic	int
4187c478bdstevel@tonic-gatecopy_file(fp, tfp, start, finish)
4197c478bdstevel@tonic-gateFILE *fp;
4207c478bdstevel@tonic-gateFILE *tfp;
4217c478bdstevel@tonic-gateregister int start;
4227c478bdstevel@tonic-gateregister int finish;
4237c478bdstevel@tonic-gate{
4247c478bdstevel@tonic-gate	register int i;		/* loop variable */
4257c478bdstevel@tonic-gate	char dummy[BUFSIZ];	/* scratch buffer */
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate/*
4287c478bdstevel@tonic-gate * always start from the beginning because line numbers are absolute
4297c478bdstevel@tonic-gate */
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	rewind(fp);
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate/*
4347c478bdstevel@tonic-gate * get to the starting point of interest
4357c478bdstevel@tonic-gate */
4367c478bdstevel@tonic-gate
4377c478bdstevel@tonic-gate	if (start != 1) {
4387c478bdstevel@tonic-gate		for (i = 1; i < start; i++)
4397c478bdstevel@tonic-gate			if (!fgets(dummy, BUFSIZ, fp))
4407c478bdstevel@tonic-gate				return(-1);
4417c478bdstevel@tonic-gate	}
4427c478bdstevel@tonic-gate
4437c478bdstevel@tonic-gate/*
4447c478bdstevel@tonic-gate * copy as much as was requested
4457c478bdstevel@tonic-gate */
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate	if (finish != -1) {
4487c478bdstevel@tonic-gate		for (i = start; i <= finish; i++) {
4497c478bdstevel@tonic-gate			if (!fgets(dummy, BUFSIZ, fp))
4507c478bdstevel@tonic-gate				return(-1);
4517c478bdstevel@tonic-gate			if (fputs(dummy, tfp) == EOF)
4527c478bdstevel@tonic-gate				return(-1);
4537c478bdstevel@tonic-gate		}
4547c478bdstevel@tonic-gate	}
4557c478bdstevel@tonic-gate	else {
4567c478bdstevel@tonic-gate		for (;;) {
4577c478bdstevel@tonic-gate			if (fgets(dummy, BUFSIZ, fp) == NULL) {
4587c478bdstevel@tonic-gate				if (feof(fp))
4597c478bdstevel@tonic-gate					break;
4607c478bdstevel@tonic-gate				else
4617c478bdstevel@tonic-gate					return(-1);
4627c478bdstevel@tonic-gate			}
4637c478bdstevel@tonic-gate			if (fputs(dummy, tfp) == EOF)
4647c478bdstevel@tonic-gate				return(-1);
4657c478bdstevel@tonic-gate		}
4667c478bdstevel@tonic-gate	}
4677c478bdstevel@tonic-gate	return(0);
4687c478bdstevel@tonic-gate}
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate/*
4717c478bdstevel@tonic-gate *	check_ref	- to check if nextlabel are referencing
4727c478bdstevel@tonic-gate *			  existing ttylabel
4737c478bdstevel@tonic-gate */
4747c478bdstevel@tonic-gatestatic	void
4757c478bdstevel@tonic-gatecheck_ref()
4767c478bdstevel@tonic-gate{
4777c478bdstevel@tonic-gate	int	i;
4787c478bdstevel@tonic-gate	struct	Gdef	*np;
4797c478bdstevel@tonic-gate	extern	struct	Gdef	*find_def();
4807c478bdstevel@tonic-gate	np = &Gdef[0];
4817c478bdstevel@tonic-gate	for (i = 0; i < Ndefs; i++, np++) {
4827c478bdstevel@tonic-gate		if (find_def(np->g_nextid) == NULL) {
4837c478bdstevel@tonic-gate			(void)printf("Warning -- nextlabel <%s> of <%s> does not reference any existing ttylabel.\n",
4847c478bdstevel@tonic-gate			np->g_nextid, np->g_id);
4857c478bdstevel@tonic-gate		}
4867c478bdstevel@tonic-gate	}
4877c478bdstevel@tonic-gate	return;
4887c478bdstevel@tonic-gate}
4897c478bdstevel@tonic-gate
4907c478bdstevel@tonic-gate/*
4917c478bdstevel@tonic-gate *	log	- print a message to stdout
4927c478bdstevel@tonic-gate */
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gatevoid
4957c478bdstevel@tonic-gatelog(const char *msg, ...)
4967c478bdstevel@tonic-gate{
4977c478bdstevel@tonic-gate	va_list ap;
4987c478bdstevel@tonic-gate	if (lflg) {
4997c478bdstevel@tonic-gate		va_start(ap, msg);
5007c478bdstevel@tonic-gate		(void) vprintf(msg, ap);
5017c478bdstevel@tonic-gate		va_end(ap);
5027c478bdstevel@tonic-gate		(void) printf("\n");
5037c478bdstevel@tonic-gate	} else {
5047c478bdstevel@tonic-gate		va_start(ap, msg);
5057c478bdstevel@tonic-gate		(void) vfprintf(stderr, msg, ap);
5067c478bdstevel@tonic-gate		va_end(ap);
5077c478bdstevel@tonic-gate		(void) fprintf(stderr,"\n");
5087c478bdstevel@tonic-gate	}
5097c478bdstevel@tonic-gate}
510