118480Sralph #ifndef lint
2*34093Sbostic static char sccsid[] = "@(#)acucntrl.c	5.14	(Berkeley) 04/24/88";
318480Sralph #endif
418480Sralph 
518480Sralph /*  acucntrl - turn around tty line between dialin and dialout
618480Sralph  *
718480Sralph  * Usage:	acucntrl {enable,disable} /dev/ttydX
818480Sralph  *
918480Sralph  * History:
1018480Sralph  *	First written by Allan Wilkes (fisher!allan)
1118480Sralph  *
1218480Sralph  *	Modified June 8,1983 by W.Sebok (astrovax!wls) to poke kernel rather
1318480Sralph  * 	than use kernel hack to turn on/off modem control, using subroutine
1418480Sralph  *	stolen from program written by Tsutomu Shimomura
1518480Sralph  *	{astrovax,escher}!tsutomu
1618480Sralph  *
1718480Sralph  *	Worked over many times by W.Sebok (i.e. hacked to death)
1818480Sralph  *
1918480Sralph  * Operation:
2018480Sralph  *   disable (i.e. setup for dialing out)
2118480Sralph  *	(1) check input arguments
2218480Sralph  *	(2) look in /etc/utmp to check that the line is not in use by another
2318480Sralph  *	(3) disable modem control on terminal
2418480Sralph  *	(4) check for carrier on device
2518480Sralph  *	(5) change owner of device to real id
2618480Sralph  *	(6) edit /etc/ttys,  changing the first character of the appropriate
2718480Sralph  *	    line to 0
2818480Sralph  *	(7) send a hangup to process 1 to poke init to disable getty
2918480Sralph  *	(8) post uid name in capitals in /etc/utmp to let world know device has
3018480Sralph  *	    been grabbed
3118480Sralph  *	(9) make sure that DTR is on
3218480Sralph  *
3318480Sralph  *   enable (i.e.) restore for dialin
3418480Sralph  *	(1) check input arguments
3518480Sralph  *	(2) look in /etc/utmp to check that the line is not in use by another
3618480Sralph  *	(3) make sure modem control on terminal is disabled
3718480Sralph  *	(4) turn off DTR to make sure line is hung up
3818480Sralph  *	(5) condition line: clear exclusive use and set hangup on close modes
3918480Sralph  *	(6) turn on modem control
4018480Sralph  *	(7) edit /etc/ttys,  changing the first character of the appropriate
4118480Sralph  *	    line to 1
4218480Sralph  *	(8) send a hangup to process 1 to poke init to enable getty
4318480Sralph  *	(9) clear uid name for /etc/utmp
4418480Sralph  */
4518480Sralph 
4618480Sralph /* #define SENSECARRIER */
4718480Sralph 
4818480Sralph #include "uucp.h"
4933557Srick #ifdef DIALINOUT
5018480Sralph #include <sys/buf.h>
5118614Sralph #include <signal.h>
5218480Sralph #include <sys/conf.h>
5333557Srick #ifdef vax
5433557Srick #ifdef BSD4_2
5533557Srick #include <vaxuba/ubavar.h>
5633557Srick #else
5733557Srick #include <sys/ubavar.h>
5833557Srick #endif
5933557Srick #endif /* vax */
6018480Sralph #include <sys/stat.h>
6118480Sralph #include <nlist.h>
6218480Sralph #include <sgtty.h>
6318480Sralph #include <utmp.h>
6418480Sralph #include <pwd.h>
6518480Sralph #include <stdio.h>
6625123Sbloom #include <sys/file.h>
6718480Sralph 
6818480Sralph #define NDZLINE	8	/* lines/dz */
6918480Sralph #define NDHLINE	16	/* lines/dh */
7018480Sralph #define NDMFLINE 8	/* lines/dmf */
7118480Sralph 
7218480Sralph #define DZ11	1
7318480Sralph #define DH11	2
7418480Sralph #define DMF	3
7518480Sralph 
7618480Sralph #define NLVALUE(val)	(nl[val].n_value)
7718480Sralph 
7818480Sralph struct nlist nl[] = {
7918480Sralph #define CDEVSW	0
8018480Sralph 	{ "_cdevsw" },
8118480Sralph 
8218480Sralph #define DZOPEN	1
8318480Sralph 	{ "_dzopen" },
8418480Sralph #define DZINFO	2
8518480Sralph 	{ "_dzinfo" },
8618480Sralph #define NDZ11	3
8718480Sralph 	{ "_dz_cnt" },
8818480Sralph #define DZSCAR	4
8918480Sralph 	{ "_dzsoftCAR" },
9018480Sralph 
9118480Sralph #define DHOPEN	5
9218480Sralph 	{ "_dhopen" },
9318480Sralph #define DHINFO	6
9418480Sralph 	{ "_dhinfo" },
9518480Sralph #define NDH11	7
9618480Sralph 	{ "_ndh11" },
9718480Sralph #define DHSCAR	8
9818480Sralph 	{ "_dhsoftCAR" },
9918480Sralph 
10018480Sralph #define DMFOPEN	9
10118480Sralph 	{ "_dmfopen" },
10218480Sralph #define DMFINFO	10
10318480Sralph 	{ "_dmfinfo" },
10418480Sralph #define NDMF	11
10518480Sralph 	{ "_ndmf" },
10618480Sralph #define DMFSCAR	12
10718480Sralph 	{ "_dmfsoftCAR" },
10818480Sralph 
10918480Sralph 	{ "\0" }
11018480Sralph };
11118480Sralph 
11218480Sralph #define ENABLE	1
11318480Sralph #define DISABLE	0
11418480Sralph 
11518480Sralph char Etcutmp[] = "/etc/utmp";
11618480Sralph char Etcttys[] = "/etc/ttys";
11723723Sbloom #ifdef BSD4_3
11825123Sbloom FILE *ttysfile, *nttysfile;
11923723Sbloom char NEtcttys[] = "/etc/ttys.new";
12023723Sbloom extern long ftell();
12123723Sbloom #endif BSD4_3
12218480Sralph char Devhome[] = "/dev";
12318480Sralph 
12418480Sralph char usage[] = "Usage: acucntrl {dis|en}able ttydX\n";
12518480Sralph 
12618480Sralph struct utmp utmp;
12718480Sralph char resettty, resetmodem;
12818480Sralph int etcutmp;
129*34093Sbostic extern int errno;
130*34093Sbostic extern char *sys_errlist[];
13118614Sralph off_t utmploc;
13218614Sralph off_t ttyslnbeg;
133*34093Sbostic off_t lseek();
13418480Sralph 
13518480Sralph #define NAMSIZ	sizeof(utmp.ut_name)
13618480Sralph #define	LINSIZ	sizeof(utmp.ut_line)
13718480Sralph 
13818480Sralph main(argc, argv)
13918480Sralph int argc; char *argv[];
14018480Sralph {
14118480Sralph 	register char *p;
14218480Sralph 	register int i;
14318480Sralph 	char uname[NAMSIZ], Uname[NAMSIZ];
14418480Sralph 	int enable ;
14518480Sralph 	char *device;
14618480Sralph 	int devfile;
14718480Sralph 	int uid, gid;
14818480Sralph 	struct passwd *getpwuid();
14918480Sralph 	char *rindex();
15018480Sralph 
15118480Sralph 	/* check input arguments */
15218480Sralph 	if (argc!=3) {
15318480Sralph 		fprintf(stderr, usage);
15418480Sralph 		exit(1);
15518480Sralph 	}
15618480Sralph 
15718480Sralph 	/* interpret command type */
15823723Sbloom 	if (prefix(argv[1], "disable")  || strcmp(argv[1], "dialout")==0)
15918480Sralph 		enable = 0;
16023723Sbloom 	else if (prefix(argv[1], "enable")  || strcmp(argv[1], "dialin")==0)
16118480Sralph 		enable = 1;
16218480Sralph 	else {
16318480Sralph 		fprintf(stderr, usage);
16418480Sralph 		exit(1);
16518480Sralph 	}
16618480Sralph 
16723723Sbloom 	device = rindex(argv[2], '/');
16818480Sralph 	device = (device == NULL) ? argv[2]: device+1;
16918480Sralph 
17018480Sralph 	/* only recognize devices of the form ttydX */
17123723Sbloom 	if (strncmp(device, "ttyd", 4)!=0) {
17223723Sbloom 		fprintf(stderr, "Bad Device Name %s", device);
17318480Sralph 		exit(1);
17418480Sralph 	}
17518480Sralph 
17618480Sralph 	opnttys(device);
17718480Sralph 
17833557Srick #ifdef vax
17918480Sralph 	/* Get nlist info */
18023723Sbloom 	nlist("/vmunix", nl);
18133557Srick #endif vax
18218480Sralph 
18318480Sralph 	/* Chdir to /dev */
18418480Sralph 	if(chdir(Devhome) < 0) {
18518480Sralph 		fprintf(stderr, "Cannot chdir to %s: %s\r\n",
18618480Sralph 			Devhome, sys_errlist[errno]);
18718480Sralph 		exit(1);
18818480Sralph 	}
18918480Sralph 
19018480Sralph 	/* Get uid information */
19118480Sralph 	uid = getuid();
19218480Sralph 	gid = getgid();
19318480Sralph 
19418480Sralph 	p = getpwuid(uid)->pw_name;
19518480Sralph 	if (p==NULL) {
19623723Sbloom 		fprintf(stderr, "cannot get uid name\n");
19718480Sralph 		exit(1);
19818480Sralph 	}
19918480Sralph 
20018480Sralph 	/*  to upper case */
20118480Sralph 	i = 0;
20218480Sralph 	do {
20318480Sralph 		uname[i] = *p;
20433968Srick 		Uname[i++] = (*p>='a' && *p<='z') ? (*p - ('a'-'A')) : *p;
20533968Srick 	} while (*p++ && i<NAMSIZ);
20618480Sralph 
20718480Sralph 	/* check to see if line is being used */
20818480Sralph 	if( (etcutmp = open(Etcutmp, 2)) < 0) {
20923723Sbloom 		fprintf(stderr, "On open %s open: %s\n",
21023723Sbloom 			Etcutmp, sys_errlist[errno]);
21118480Sralph 		exit(1);
21218480Sralph 	}
21318480Sralph 
21423723Sbloom 	(void)lseek(etcutmp, utmploc, 0);
21518480Sralph 
21623723Sbloom 	i = read(etcutmp, (char *)&utmp, sizeof(struct utmp));
21718480Sralph 
21818480Sralph 	if(
21918480Sralph 		i == sizeof(struct utmp) &&
22018480Sralph 		utmp.ut_line[0] != '\0'  &&
22118480Sralph 		utmp.ut_name[0] != '\0'  &&
22218480Sralph 		(
22323723Sbloom 			!upcase(utmp.ut_name, NAMSIZ) ||
22418480Sralph 			(
22518480Sralph 				uid != 0 &&
22623723Sbloom 				strncmp(utmp.ut_name, Uname, NAMSIZ) != 0
22718480Sralph 			)
22818480Sralph 		)
22918480Sralph 	) {
23018480Sralph 		fprintf(stderr, "%s in use by %s\n", device, utmp.ut_name);
23118480Sralph 		exit(2);
23218480Sralph 	}
23318480Sralph 
23433557Srick #ifndef sequent
23518480Sralph 	/* Disable modem control */
23623723Sbloom 	if (setmodem(device, DISABLE) < 0) {
23723723Sbloom 		fprintf(stderr, "Unable to disable modem control\n");
23818480Sralph 		exit(1);
23918480Sralph 	}
24033557Srick #endif !sequent
24118480Sralph 
24218480Sralph 	if (enable) {
24333557Srick #ifdef sequent
24433557Srick 		if (setmodem(device, ENABLE) < 0) {
24533557Srick 			fprintf(stderr, "Cannot Enable modem control\n");
24633557Srick 			(void)setmodem(device, i);
24733557Srick 			exit(1);
24833557Srick 		}
24933557Srick #endif sequent
25033557Srick #ifndef sequent
25118480Sralph 		if((devfile = open(device, 1)) < 0) {
25223723Sbloom 			fprintf(stderr, "On open of %s: %s\n",
25318480Sralph 				device, sys_errlist[errno]);
25423723Sbloom 			(void)setmodem(device, resetmodem);
25518480Sralph 			exit(1);
25618480Sralph 		}
25718480Sralph 		/* Try one last time to hang up */
25823723Sbloom 		if (ioctl(devfile, (int)TIOCCDTR, (char *)0) < 0)
25923723Sbloom 			fprintf(stderr, "On TIOCCDTR ioctl: %s\n",
26018480Sralph 				sys_errlist[errno]);
26118480Sralph 
26223723Sbloom 		if (ioctl(devfile, (int)TIOCNXCL, (char *)0) < 0)
26318480Sralph 			fprintf(stderr,
26418480Sralph 			    "Cannot clear Exclusive Use on %s: %s\n",
26518480Sralph 				device, sys_errlist[errno]);
26618480Sralph 
26723723Sbloom 		if (ioctl(devfile, (int)TIOCHPCL, (char *)0) < 0)
26818480Sralph 			fprintf(stderr,
26918480Sralph 			    "Cannot set hangup on close on %s: %s\n",
27018480Sralph 				device, sys_errlist[errno]);
27118480Sralph 
27233557Srick #endif !sequent
27318480Sralph 		i = resetmodem;
27418480Sralph 
27533557Srick #ifndef sequent
27623723Sbloom 		if (setmodem(device, ENABLE) < 0) {
27723723Sbloom 			fprintf(stderr, "Cannot Enable modem control\n");
27823723Sbloom 			(void)setmodem(device, i);
27918480Sralph 			exit(1);
28018480Sralph 		}
28133557Srick #endif sequent
28218480Sralph 		resetmodem=i;
28318480Sralph 
28418614Sralph 		if (settys(ENABLE)) {
28523723Sbloom 			fprintf(stderr, "%s already enabled\n", device);
28618614Sralph 		} else {
28723723Sbloom 			pokeinit(device, Uname, enable);
28818614Sralph 		}
28923723Sbloom 		post(device, "");
29018480Sralph 
29118480Sralph 	} else {
29218480Sralph #if defined(TIOCMGET) && defined(SENSECARRIER)
29318480Sralph 		if (uid!=0) {
29418480Sralph 			int linestat = 0;
29518480Sralph 
29618480Sralph 			/* check for presence of carrier */
29718480Sralph 			sleep(2); /* need time after modem control turnoff */
29818480Sralph 
29918480Sralph 			if((devfile = open(device, 1)) < 0) {
30023723Sbloom 				fprintf(stderr, "On open of %s: %s\n",
30118480Sralph 					device, sys_errlist[errno]);
30223723Sbloom 				(void)setmodem(device, resetmodem);
30318480Sralph 				exit(1);
30418480Sralph 			}
30518480Sralph 
30623723Sbloom 			(void)ioctl(devfile, TIOCMGET, &linestat);
30718480Sralph 
30818480Sralph 			if (linestat&TIOCM_CAR) {
30923723Sbloom 				fprintf(stderr, "%s is in use (Carrier On)\n",
31018480Sralph 					device);
31123723Sbloom 				(void)setmodem(device, resetmodem);
31218480Sralph 				exit(2);
31318480Sralph 			}
31418480Sralph 			(void)close(devfile);
31518480Sralph 		}
31618480Sralph #endif TIOCMGET
31718480Sralph 		/* chown device */
31818480Sralph 		if(chown(device, uid, gid) < 0)
31918480Sralph 			fprintf(stderr, "Cannot chown %s: %s\n",
32018480Sralph 				device, sys_errlist[errno]);
32118480Sralph 
32218480Sralph 
32318480Sralph 		/* poke init */
32418614Sralph 		if(settys(DISABLE)) {
32523723Sbloom 			fprintf(stderr, "%s already disabled\n", device);
32618614Sralph 		} else {
32723723Sbloom 			pokeinit(device, Uname, enable);
32818614Sralph 		}
32923723Sbloom 		post(device, Uname);
33033557Srick #ifdef sequent
33133557Srick 	/* Disable modem control */
33233557Srick 	if (setmodem(device, DISABLE) < 0) {
33333557Srick 		fprintf(stderr, "Unable to disable modem control\n");
33433557Srick 		exit(1);
33533557Srick 	}
33633557Srick #endif sequent
33725123Sbloom 		if((devfile = open(device, O_RDWR|O_NDELAY)) < 0) {
33818480Sralph 			fprintf(stderr, "On %s open: %s\n",
33918480Sralph 				device, sys_errlist[errno]);
34018480Sralph 		} else {
34118614Sralph 			if(ioctl(devfile, (int)TIOCSDTR, (char *)0) < 0)
34218480Sralph 				fprintf(stderr,
34318480Sralph 				    "Cannot set DTR on %s: %s\n",
34418480Sralph 					device, sys_errlist[errno]);
34518480Sralph 		}
34618480Sralph 	}
34718480Sralph 
34818480Sralph 	exit(0);
34918480Sralph }
35018480Sralph 
35118480Sralph /* return true if no lower case */
35223723Sbloom upcase(str, len)
35318480Sralph register char *str;
35418480Sralph register int len;
35518480Sralph {
35618480Sralph 	for (; *str, --len >= 0 ; str++)
35718480Sralph 		if (*str>='a' && *str<='z')
35818480Sralph 			return(0);
35918480Sralph 	return(1);
36018480Sralph }
36118480Sralph 
36218480Sralph /* Post name to public */
36323723Sbloom post(device, name)
36418480Sralph char *device, *name;
36518480Sralph {
36618614Sralph 	(void)time((time_t *)&utmp.ut_time);
36723583Sbloom 	strncpy(utmp.ut_line, device, LINSIZ);
36823583Sbloom 	strncpy(utmp.ut_name, name,  NAMSIZ);
36925123Sbloom 	if (lseek(etcutmp, utmploc, 0) < 0)
37023723Sbloom 		fprintf(stderr, "on lseek in /etc/utmp: %s",
37118480Sralph 			sys_errlist[errno]);
37225123Sbloom 	if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0)
37323723Sbloom 		fprintf(stderr, "on write in /etc/utmp: %s",
37418480Sralph 			sys_errlist[errno]);
37518480Sralph }
37618480Sralph 
37718480Sralph /* poke process 1 and wait for it to do its thing */
37823723Sbloom pokeinit(device, uname, enable)
37918480Sralph char *uname, *device; int enable;
38018480Sralph {
38118480Sralph 	struct utmp utmp;
38218614Sralph 	register int i;
38318480Sralph 
38418480Sralph 	post(device, uname);
38518480Sralph 
38618480Sralph 	/* poke init */
38718480Sralph 	if (kill(1, SIGHUP)) {
38818480Sralph 		fprintf(stderr,
38918480Sralph 		    "Cannot send hangup to init process: %s\n",
39018480Sralph 			sys_errlist[errno]);
39118614Sralph 		(void)settys(resettty);
39223723Sbloom 		(void)setmodem(device, resetmodem);
39318480Sralph 		exit(1);
39418480Sralph 	}
39518480Sralph 
39618480Sralph 	if (enable)
39718480Sralph 		return;
39818480Sralph 
39918480Sralph 	/* wait till init has responded, clearing the utmp entry */
40025123Sbloom 	i = 100;
40118480Sralph 	do {
40218480Sralph 		sleep(1);
40325123Sbloom 		if (lseek(etcutmp, utmploc, 0) < 0)
40423723Sbloom 			fprintf(stderr, "On lseek in /etc/utmp: %s",
40518480Sralph 				sys_errlist[errno]);
40625123Sbloom 		if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0)
40723723Sbloom 			fprintf(stderr, "On read from /etc/utmp: %s",
40818480Sralph 				sys_errlist[errno]);
40918614Sralph 	} while (utmp.ut_name[0] != '\0' && --i > 0);
41018480Sralph }
41118480Sralph 
41223723Sbloom #ifdef BSD4_3
41318480Sralph /* identify terminal line in ttys */
41418480Sralph opnttys(device)
41518480Sralph char *device;
41618480Sralph {
41723723Sbloom 	register int  ndevice;
41823723Sbloom 	register char *p;
41923723Sbloom 	char *index();
42023723Sbloom 	char linebuf[BUFSIZ];
42123723Sbloom 
42225123Sbloom 	ttysfile = NULL;
42325123Sbloom 	do {
42425123Sbloom 		if (ttysfile != NULL) {
42525123Sbloom 			fclose(ttysfile);
42625123Sbloom 			sleep(5);
42725123Sbloom 		}
42825123Sbloom 		ttysfile = fopen(Etcttys, "r");
42925123Sbloom 		if(ttysfile == NULL) {
43025123Sbloom 			fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
43125123Sbloom 				sys_errlist[errno]);
43225123Sbloom 			exit(1);
43325123Sbloom 		}
43425123Sbloom 	} while (flock(fileno(ttysfile), LOCK_NB|LOCK_EX) < 0);
43523723Sbloom 	nttysfile = fopen(NEtcttys, "w");
43623723Sbloom 	if(nttysfile == NULL) {
43723723Sbloom 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
43823723Sbloom 			sys_errlist[errno]);
43923723Sbloom 		exit(1);
44023723Sbloom 	}
44123723Sbloom 
44223723Sbloom 	ndevice = strlen(device);
44325962Sbloom #ifndef BRL4_2
44425123Sbloom 	utmploc = sizeof(utmp);
44525962Sbloom #else BRL4_2
44625962Sbloom 	utmploc = 0;
44725962Sbloom #endif BRL4_2
44823723Sbloom 
44923723Sbloom 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
45025123Sbloom 		if(strncmp(device, linebuf, ndevice) == 0)
45125123Sbloom 			return;
45225123Sbloom 		ttyslnbeg += strlen(linebuf);
45325962Sbloom 		if (linebuf[0] != '#' && linebuf[0] != '\0')
45425962Sbloom 			utmploc += sizeof(utmp);
45523723Sbloom 		if (fputs(linebuf, nttysfile) == NULL) {
45623723Sbloom 			fprintf(stderr, "On %s write: %s\n",
45723723Sbloom 				Etcttys, sys_errlist[errno]);
45823723Sbloom 			exit(1);
45923723Sbloom 		}
46023723Sbloom 
46123723Sbloom 	}
46223723Sbloom 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
46323723Sbloom 	exit(1);
46423723Sbloom }
46523723Sbloom 
46623723Sbloom /* modify appropriate line in /etc/ttys to turn on/off the device */
46723723Sbloom settys(enable)
46823723Sbloom int enable;
46923723Sbloom {
47023723Sbloom 	register char *cp, *cp2;
47123723Sbloom 	char lbuf[BUFSIZ];
47223723Sbloom 	int i;
47323723Sbloom 	char c1, c2;
47423723Sbloom 
47525123Sbloom 	(void) fseek(ttysfile, ttyslnbeg, 0);
47625123Sbloom 	if(fgets(lbuf, BUFSIZ, ttysfile) == NULL) {
47723723Sbloom 		fprintf(stderr, "On %s read: %s\n",
47823723Sbloom 			Etcttys, sys_errlist[errno]);
47923723Sbloom 		exit(1);
48023723Sbloom 	}
48123723Sbloom 	/* format is now */
48223723Sbloom 	/* ttyd0 std.100 dialup on secure # comment */
48325364Sbloom 	/* except, 2nd item may have embedded spaces inside quotes, Hubert */
48423723Sbloom 	cp = lbuf;
48523723Sbloom 	for (i=0;*cp && i<3;i++) {
48625364Sbloom 		if (*cp == '"') {
48723723Sbloom 			cp++;
48825364Sbloom 			while (*cp && *cp != '"')
48925364Sbloom 				cp++;
49025364Sbloom 			if (*cp != '\0')
49125364Sbloom 				cp++;
49225364Sbloom 		}else {
49325364Sbloom 			while (*cp && *cp != ' ' && *cp != '\t')
49425364Sbloom 				cp++;
49525364Sbloom 		}
49623723Sbloom 		while (*cp && (*cp == ' ' || *cp == '\t'))
49723723Sbloom 			cp++;
49823723Sbloom 	}
49923723Sbloom 	if (*cp == '\0') {
50023723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
50123723Sbloom 		exit(1);
50223723Sbloom 	}
50323723Sbloom 	c1 = *--cp;
50423723Sbloom 	*cp++ = '\0';
50523723Sbloom 	cp2 = cp;
50623723Sbloom 	while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
50723723Sbloom 		cp++;
50823723Sbloom 	if (*cp == '\0') {
50923723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
51023723Sbloom 		exit(1);
51123723Sbloom 	}
51223723Sbloom 	c2 = *cp;
51323723Sbloom 	*cp++ = '\0';
51423723Sbloom 	while (*cp && (*cp == ' ' || *cp == '\t'))
51523723Sbloom 		cp++;
51623723Sbloom 	resettty = strcmp("on", cp2) != 0;
51725123Sbloom 	fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
51825123Sbloom 	if (ferror(nttysfile)) {
51923723Sbloom 		fprintf(stderr, "On %s fprintf: %s\n",
52023723Sbloom 			NEtcttys, sys_errlist[errno]);
52123723Sbloom 		exit(1);
52223723Sbloom 	}
52325123Sbloom 	while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) {
52425123Sbloom 		if (fputs(lbuf, nttysfile) == NULL) {
52523723Sbloom 			fprintf(stderr, "On %s write: %s\n",
52623723Sbloom 				NEtcttys, sys_errlist[errno]);
52723723Sbloom 			exit(1);
52823723Sbloom 		}
52923723Sbloom 	}
53023723Sbloom 
53123723Sbloom 	if (enable^resettty)
53223723Sbloom 		(void) unlink(NEtcttys);
53323723Sbloom 	else {
53423723Sbloom 		struct stat statb;
53523723Sbloom 		if (stat(Etcttys, &statb) == 0) {
53625123Sbloom 			fchmod(fileno(nttysfile) ,statb.st_mode);
53725123Sbloom 			fchown(fileno(nttysfile), statb.st_uid, statb.st_gid);
53823723Sbloom 		}
53923723Sbloom 		(void) rename(NEtcttys, Etcttys);
54023723Sbloom 	}
54125123Sbloom 	(void) fclose(nttysfile);
54225123Sbloom 	(void) fclose(ttysfile);
54323723Sbloom 	return enable^resettty;
54423723Sbloom }
54523723Sbloom 
54623723Sbloom #else !BSD4_3
54723723Sbloom 
54823723Sbloom /* identify terminal line in ttys */
54923723Sbloom opnttys(device)
55023723Sbloom char *device;
55123723Sbloom {
55218480Sralph 	register FILE *ttysfile;
55318480Sralph 	register int  ndevice, lnsiz;
55418480Sralph 	register char *p;
55518480Sralph 	char *index();
55623723Sbloom 	char linebuf[BUFSIZ];
55718480Sralph 
55818480Sralph 	ttysfile = fopen(Etcttys, "r");
55918480Sralph 	if(ttysfile == NULL) {
56018480Sralph 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
56118480Sralph 			sys_errlist[errno]);
56218480Sralph 		exit(1);
56318480Sralph 	}
56418480Sralph 
56518480Sralph 	ndevice = strlen(device);
56618480Sralph 	ttyslnbeg = 0;
56718480Sralph 	utmploc = 0;
56818480Sralph 
56918480Sralph 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
57018480Sralph 		lnsiz = strlen(linebuf);
57123723Sbloom 		if ((p = index(linebuf, '\n')) != NULL)
57218480Sralph 			*p = '\0';
57318480Sralph 		if(strncmp(device, &linebuf[2], ndevice) == 0) {
57418480Sralph 			(void)fclose(ttysfile);
57533557Srick #ifdef sequent
57633557Srick 			/* Why is the sequent off by one? */
57733557Srick 			utmploc += sizeof(utmp);
57833557Srick #endif sequent
57918480Sralph 			return;
58018480Sralph 		}
58118480Sralph 		ttyslnbeg += lnsiz;
58225123Sbloom 		utmploc += sizeof(utmp);
58318480Sralph 	}
58418480Sralph 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
58518480Sralph 	exit(1);
58618480Sralph }
58718480Sralph 
58818480Sralph /* modify appropriate line in /etc/ttys to turn on/off the device */
58918480Sralph settys(enable)
59018480Sralph int enable;
59118480Sralph {
59218480Sralph 	int ittysfil;
59323723Sbloom 	char out, in;
59418480Sralph 
59518480Sralph 	ittysfil = open(Etcttys, 2);
59623723Sbloom 	if(ittysfil < 0) {
59718480Sralph 		fprintf(stderr, "Cannot open %s for output: %s\n",
59818480Sralph 			Etcttys, sys_errlist[errno]);
59918480Sralph 		exit(1);
60018480Sralph 	}
60123723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
60223723Sbloom 	if(read(ittysfil, &in, 1)<0) {
60323723Sbloom 		fprintf(stderr, "On %s write: %s\n",
60418480Sralph 			Etcttys, sys_errlist[errno]);
60518480Sralph 		exit(1);
60618480Sralph 	}
60718480Sralph 	resettty = (in == '1');
60818480Sralph 	out = enable ? '1' : '0';
60923723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
61023723Sbloom 	if(write(ittysfil, &out, 1)<0) {
61123723Sbloom 		fprintf(stderr, "On %s write: %s\n",
61218480Sralph 			Etcttys, sys_errlist[errno]);
61318480Sralph 		exit(1);
61418480Sralph 	}
61518480Sralph 	(void)close(ittysfil);
61618614Sralph 	return(in==out);
61718480Sralph }
61823723Sbloom #endif !BSD4_3
61918480Sralph 
62033557Srick #ifdef sequent
62133557Srick setmodem(ttyline, enable)
62233557Srick char *ttyline; int enable;
62333557Srick {
62433557Srick 	char *sysbuf[BUFSIZ];
62533557Srick 	sprintf(sysbuf,"/etc/ttyconfig /dev/%s -special %s", ttyline,
62633557Srick 		enable ? "-carrier" : "-nocarrier");
62733557Srick 	system(sysbuf);
62833557Srick }
62933557Srick #endif /* sequent */
63033557Srick #ifdef vax
63118480Sralph /*
63223723Sbloom  * Excerpted from (June 8, 1983 W.Sebok)
63318480Sralph  * > ttymodem.c - enable/disable modem control for tty lines.
63418480Sralph  * >
63518480Sralph  * > Knows about DZ11s and DH11/DM11s.
63618480Sralph  * > 23.3.83 - TS
63723723Sbloom  * > modified to know about DMF's  (hasn't been tested) Nov 8, 1984 - WLS
63818480Sralph  */
63918480Sralph 
64018480Sralph 
64123723Sbloom setmodem(ttyline, enable)
64218480Sralph char *ttyline; int enable;
64318480Sralph {
64418480Sralph 	dev_t dev;
64518480Sralph 	int kmem;
64623723Sbloom 	int unit, line, nlines, addr, tflags;
64718614Sralph 	int devtype=0;
64818614Sralph 	char cflags; short sflags;
64918614Sralph #ifdef BSD4_2
65018614Sralph 	int flags;
65118614Sralph #else
65218614Sralph 	short flags;
65318614Sralph #endif
65418480Sralph 	struct uba_device *ubinfo;
65518480Sralph 	struct stat statb;
65618480Sralph 	struct cdevsw cdevsw;
65718480Sralph 
65818480Sralph 	if(nl[CDEVSW].n_type == 0) {
65923723Sbloom 		fprintf(stderr, "No namelist.\n");
66018480Sralph 		return(-1);
66118480Sralph 	}
66218480Sralph 
66318480Sralph 	if((kmem = open("/dev/kmem", 2)) < 0) {
66423723Sbloom 		fprintf(stderr, "/dev/kmem open: %s\n", sys_errlist[errno]);
66518480Sralph 		return(-1);
66618480Sralph 	}
66718480Sralph 
66823723Sbloom 	if(stat(ttyline, &statb) < 0) {
66923723Sbloom 		fprintf(stderr, "%s stat: %s\n", ttyline, sys_errlist[errno]);
67018480Sralph 		return(-1);
67118480Sralph 	}
67218480Sralph 
67318614Sralph 	if((statb.st_mode&S_IFMT) != S_IFCHR) {
67423723Sbloom 		fprintf(stderr, "%s is not a character device.\n",ttyline);
67518480Sralph 		return(-1);
67618480Sralph 	}
67718480Sralph 
67818480Sralph 	dev = statb.st_rdev;
67918480Sralph 	(void)lseek(kmem,
68018614Sralph 		(off_t) &(((struct cdevsw *)NLVALUE(CDEVSW))[major(dev)]),0);
68123723Sbloom 	(void)read(kmem, (char *) &cdevsw, sizeof cdevsw);
68218480Sralph 
68318480Sralph 	if((int)(cdevsw.d_open) == NLVALUE(DZOPEN)) {
68418480Sralph 		devtype = DZ11;
68518480Sralph 		unit = minor(dev) / NDZLINE;
68618480Sralph 		line = minor(dev) % NDZLINE;
68718480Sralph 		addr = (int) &(((int *)NLVALUE(DZINFO))[unit]);
68823723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDZ11), 0);
68918480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DHOPEN)) {
69018480Sralph 		devtype = DH11;
69118480Sralph 		unit = minor(dev) / NDHLINE;
69218480Sralph 		line = minor(dev) % NDHLINE;
69318480Sralph 		addr = (int) &(((int *)NLVALUE(DHINFO))[unit]);
69423723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDH11), 0);
69518480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DMFOPEN)) {
69618480Sralph 		devtype = DMF;
69718480Sralph 		unit = minor(dev) / NDMFLINE;
69818480Sralph 		line = minor(dev) % NDMFLINE;
69918480Sralph 		addr = (int) &(((int *)NLVALUE(DMFINFO))[unit]);
70023723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDMF), 0);
70118480Sralph 	} else {
70223723Sbloom 		fprintf(stderr, "Device %s (%d/%d) unknown.\n", ttyline,
70323723Sbloom 		    major(dev), minor(dev));
70418480Sralph 		return(-1);
70518480Sralph 	}
70618480Sralph 
70723723Sbloom 	(void)read(kmem, (char *) &nlines, sizeof nlines);
70818480Sralph 	if(minor(dev) >= nlines) {
70923723Sbloom 		fprintf(stderr, "Sub-device %d does not exist (only %d).\n",
71023723Sbloom 		    minor(dev), nlines);
71118480Sralph 		return(-1);
71218480Sralph 	}
71318480Sralph 
71423723Sbloom 	(void)lseek(kmem, (off_t)addr, 0);
71523723Sbloom 	(void)read(kmem, (char *) &ubinfo, sizeof ubinfo);
71623723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
71723723Sbloom 	(void)read(kmem, (char *) &flags, sizeof flags);
71818480Sralph 
71918480Sralph 	tflags = 1<<line;
72018480Sralph 	resetmodem = ((flags&tflags) == 0);
72118480Sralph 	flags = enable ? (flags & ~tflags) : (flags | tflags);
72223723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
72323723Sbloom 	(void)write(kmem, (char *) &flags, sizeof flags);
72418480Sralph 	switch(devtype) {
72518480Sralph 		case DZ11:
72618480Sralph 			if((addr = NLVALUE(DZSCAR)) == 0) {
72723723Sbloom 				fprintf(stderr, "No dzsoftCAR.\n");
72818480Sralph 				return(-1);
72918480Sralph 			}
73018614Sralph 			cflags = flags;
73123723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
73223723Sbloom 			(void)write(kmem, (char *) &cflags, sizeof cflags);
73318480Sralph 			break;
73418480Sralph 		case DH11:
73518480Sralph 			if((addr = NLVALUE(DHSCAR)) == 0) {
73623723Sbloom 				fprintf(stderr, "No dhsoftCAR.\n");
73718480Sralph 				return(-1);
73818480Sralph 			}
73918614Sralph 			sflags = flags;
74023723Sbloom 			(void)lseek(kmem, (off_t) &(((short *)addr)[unit]), 0);
74123723Sbloom 			(void)write(kmem, (char *) &sflags, sizeof sflags);
74218480Sralph 			break;
74318480Sralph 		case DMF:
74418480Sralph 			if((addr = NLVALUE(DMFSCAR)) == 0) {
74523723Sbloom 				fprintf(stderr, "No dmfsoftCAR.\n");
74618480Sralph 				return(-1);
74718480Sralph 			}
74818614Sralph 			cflags = flags;
74923723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
75034008Sbostic 			(void)write(kmem, (char *) &cflags, sizeof cflags);
75118480Sralph 			break;
75218480Sralph 		default:
75323723Sbloom 			fprintf(stderr, "Unknown device type\n");
75418480Sralph 			return(-1);
75518480Sralph 	}
75618480Sralph 	return(0);
75718480Sralph }
75833557Srick #endif /* vax */
75918480Sralph 
76018480Sralph prefix(s1, s2)
76118480Sralph 	register char *s1, *s2;
76218480Sralph {
76318480Sralph 	register char c;
76418480Sralph 
76518480Sralph 	while ((c = *s1++) == *s2++)
76618480Sralph 		if (c == '\0')
76718480Sralph 			return (1);
76818480Sralph 	return (c == '\0');
76918480Sralph }
77033592Srick #else	/* !DIALINOUT */
77133592Srick main()
77233592Srick {
77333592Srick 	fprintf(stderr,"acucntrl is not supported on this system\n");
77433592Srick }
77533592Srick #endif /* !DIALINOUT */
776