118480Sralph #ifndef lint
2*34163Srick static char sccsid[] = "@(#)acucntrl.c	5.15	(Berkeley) 05/04/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*34163Srick off_t utmploc;
130*34163Srick off_t ttyslnbeg;
13134093Sbostic extern int errno;
13234093Sbostic extern char *sys_errlist[];
13334093Sbostic 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 */
152*34163Srick 	if (argc!=3 && argc != 4) {
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 	opnttys(device);
17118480Sralph 
17233557Srick #ifdef vax
17318480Sralph 	/* Get nlist info */
17423723Sbloom 	nlist("/vmunix", nl);
17533557Srick #endif vax
17618480Sralph 
17718480Sralph 	/* Chdir to /dev */
17818480Sralph 	if(chdir(Devhome) < 0) {
17918480Sralph 		fprintf(stderr, "Cannot chdir to %s: %s\r\n",
18018480Sralph 			Devhome, sys_errlist[errno]);
18118480Sralph 		exit(1);
18218480Sralph 	}
18318480Sralph 
18418480Sralph 	/* Get uid information */
18518480Sralph 	uid = getuid();
18618480Sralph 	gid = getgid();
18718480Sralph 
18818480Sralph 	p = getpwuid(uid)->pw_name;
18918480Sralph 	if (p==NULL) {
19023723Sbloom 		fprintf(stderr, "cannot get uid name\n");
19118480Sralph 		exit(1);
19218480Sralph 	}
19318480Sralph 
194*34163Srick 	if (strcmp(p, "uucp") == 0 && argc == 4)
195*34163Srick 		p = argv[3];
196*34163Srick 
19718480Sralph 	/*  to upper case */
19818480Sralph 	i = 0;
19918480Sralph 	do {
20018480Sralph 		uname[i] = *p;
20133968Srick 		Uname[i++] = (*p>='a' && *p<='z') ? (*p - ('a'-'A')) : *p;
20233968Srick 	} while (*p++ && i<NAMSIZ);
20318480Sralph 
20418480Sralph 	/* check to see if line is being used */
20518480Sralph 	if( (etcutmp = open(Etcutmp, 2)) < 0) {
20623723Sbloom 		fprintf(stderr, "On open %s open: %s\n",
20723723Sbloom 			Etcutmp, sys_errlist[errno]);
20818480Sralph 		exit(1);
20918480Sralph 	}
21018480Sralph 
21123723Sbloom 	(void)lseek(etcutmp, utmploc, 0);
21218480Sralph 
21323723Sbloom 	i = read(etcutmp, (char *)&utmp, sizeof(struct utmp));
21418480Sralph 
21518480Sralph 	if(
21618480Sralph 		i == sizeof(struct utmp) &&
21718480Sralph 		utmp.ut_line[0] != '\0'  &&
21818480Sralph 		utmp.ut_name[0] != '\0'  &&
21918480Sralph 		(
22023723Sbloom 			!upcase(utmp.ut_name, NAMSIZ) ||
22118480Sralph 			(
22218480Sralph 				uid != 0 &&
22323723Sbloom 				strncmp(utmp.ut_name, Uname, NAMSIZ) != 0
22418480Sralph 			)
22518480Sralph 		)
22618480Sralph 	) {
22718480Sralph 		fprintf(stderr, "%s in use by %s\n", device, utmp.ut_name);
22818480Sralph 		exit(2);
22918480Sralph 	}
23018480Sralph 
23133557Srick #ifndef sequent
23218480Sralph 	/* Disable modem control */
23323723Sbloom 	if (setmodem(device, DISABLE) < 0) {
23423723Sbloom 		fprintf(stderr, "Unable to disable modem control\n");
23518480Sralph 		exit(1);
23618480Sralph 	}
23733557Srick #endif !sequent
23818480Sralph 
23918480Sralph 	if (enable) {
24033557Srick #ifdef sequent
24133557Srick 		if (setmodem(device, ENABLE) < 0) {
24233557Srick 			fprintf(stderr, "Cannot Enable modem control\n");
24333557Srick 			(void)setmodem(device, i);
24433557Srick 			exit(1);
24533557Srick 		}
24633557Srick #endif sequent
24733557Srick #ifndef sequent
24818480Sralph 		if((devfile = open(device, 1)) < 0) {
24923723Sbloom 			fprintf(stderr, "On open of %s: %s\n",
25018480Sralph 				device, sys_errlist[errno]);
25123723Sbloom 			(void)setmodem(device, resetmodem);
25218480Sralph 			exit(1);
25318480Sralph 		}
25418480Sralph 		/* Try one last time to hang up */
25523723Sbloom 		if (ioctl(devfile, (int)TIOCCDTR, (char *)0) < 0)
25623723Sbloom 			fprintf(stderr, "On TIOCCDTR ioctl: %s\n",
25718480Sralph 				sys_errlist[errno]);
25818480Sralph 
25923723Sbloom 		if (ioctl(devfile, (int)TIOCNXCL, (char *)0) < 0)
26018480Sralph 			fprintf(stderr,
26118480Sralph 			    "Cannot clear Exclusive Use on %s: %s\n",
26218480Sralph 				device, sys_errlist[errno]);
26318480Sralph 
26423723Sbloom 		if (ioctl(devfile, (int)TIOCHPCL, (char *)0) < 0)
26518480Sralph 			fprintf(stderr,
26618480Sralph 			    "Cannot set hangup on close on %s: %s\n",
26718480Sralph 				device, sys_errlist[errno]);
26818480Sralph 
26933557Srick #endif !sequent
27018480Sralph 		i = resetmodem;
27118480Sralph 
27233557Srick #ifndef sequent
27323723Sbloom 		if (setmodem(device, ENABLE) < 0) {
27423723Sbloom 			fprintf(stderr, "Cannot Enable modem control\n");
27523723Sbloom 			(void)setmodem(device, i);
27618480Sralph 			exit(1);
27718480Sralph 		}
27833557Srick #endif sequent
27918480Sralph 		resetmodem=i;
28018480Sralph 
28118614Sralph 		if (settys(ENABLE)) {
28223723Sbloom 			fprintf(stderr, "%s already enabled\n", device);
28318614Sralph 		} else {
28423723Sbloom 			pokeinit(device, Uname, enable);
28518614Sralph 		}
28623723Sbloom 		post(device, "");
28718480Sralph 
28818480Sralph 	} else {
28918480Sralph #if defined(TIOCMGET) && defined(SENSECARRIER)
29018480Sralph 		if (uid!=0) {
29118480Sralph 			int linestat = 0;
29218480Sralph 
29318480Sralph 			/* check for presence of carrier */
29418480Sralph 			sleep(2); /* need time after modem control turnoff */
29518480Sralph 
29618480Sralph 			if((devfile = open(device, 1)) < 0) {
29723723Sbloom 				fprintf(stderr, "On open of %s: %s\n",
29818480Sralph 					device, sys_errlist[errno]);
29923723Sbloom 				(void)setmodem(device, resetmodem);
30018480Sralph 				exit(1);
30118480Sralph 			}
30218480Sralph 
30323723Sbloom 			(void)ioctl(devfile, TIOCMGET, &linestat);
30418480Sralph 
30518480Sralph 			if (linestat&TIOCM_CAR) {
30623723Sbloom 				fprintf(stderr, "%s is in use (Carrier On)\n",
30718480Sralph 					device);
30823723Sbloom 				(void)setmodem(device, resetmodem);
30918480Sralph 				exit(2);
31018480Sralph 			}
31118480Sralph 			(void)close(devfile);
31218480Sralph 		}
31318480Sralph #endif TIOCMGET
31418480Sralph 		/* chown device */
31518480Sralph 		if(chown(device, uid, gid) < 0)
31618480Sralph 			fprintf(stderr, "Cannot chown %s: %s\n",
31718480Sralph 				device, sys_errlist[errno]);
31818480Sralph 
31918480Sralph 
32018480Sralph 		/* poke init */
32118614Sralph 		if(settys(DISABLE)) {
32223723Sbloom 			fprintf(stderr, "%s already disabled\n", device);
32318614Sralph 		} else {
32423723Sbloom 			pokeinit(device, Uname, enable);
32518614Sralph 		}
32623723Sbloom 		post(device, Uname);
32733557Srick #ifdef sequent
32833557Srick 	/* Disable modem control */
32933557Srick 	if (setmodem(device, DISABLE) < 0) {
33033557Srick 		fprintf(stderr, "Unable to disable modem control\n");
33133557Srick 		exit(1);
33233557Srick 	}
33333557Srick #endif sequent
33425123Sbloom 		if((devfile = open(device, O_RDWR|O_NDELAY)) < 0) {
33518480Sralph 			fprintf(stderr, "On %s open: %s\n",
33618480Sralph 				device, sys_errlist[errno]);
33718480Sralph 		} else {
33818614Sralph 			if(ioctl(devfile, (int)TIOCSDTR, (char *)0) < 0)
33918480Sralph 				fprintf(stderr,
34018480Sralph 				    "Cannot set DTR on %s: %s\n",
34118480Sralph 					device, sys_errlist[errno]);
34218480Sralph 		}
34318480Sralph 	}
34418480Sralph 
34518480Sralph 	exit(0);
34618480Sralph }
34718480Sralph 
34818480Sralph /* return true if no lower case */
34923723Sbloom upcase(str, len)
35018480Sralph register char *str;
35118480Sralph register int len;
35218480Sralph {
35318480Sralph 	for (; *str, --len >= 0 ; str++)
35418480Sralph 		if (*str>='a' && *str<='z')
35518480Sralph 			return(0);
35618480Sralph 	return(1);
35718480Sralph }
35818480Sralph 
35918480Sralph /* Post name to public */
36023723Sbloom post(device, name)
36118480Sralph char *device, *name;
36218480Sralph {
36318614Sralph 	(void)time((time_t *)&utmp.ut_time);
36423583Sbloom 	strncpy(utmp.ut_line, device, LINSIZ);
36523583Sbloom 	strncpy(utmp.ut_name, name,  NAMSIZ);
36625123Sbloom 	if (lseek(etcutmp, utmploc, 0) < 0)
36723723Sbloom 		fprintf(stderr, "on lseek in /etc/utmp: %s",
36818480Sralph 			sys_errlist[errno]);
36925123Sbloom 	if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0)
37023723Sbloom 		fprintf(stderr, "on write in /etc/utmp: %s",
37118480Sralph 			sys_errlist[errno]);
37218480Sralph }
37318480Sralph 
37418480Sralph /* poke process 1 and wait for it to do its thing */
37523723Sbloom pokeinit(device, uname, enable)
37618480Sralph char *uname, *device; int enable;
37718480Sralph {
37818480Sralph 	struct utmp utmp;
37918614Sralph 	register int i;
38018480Sralph 
38118480Sralph 	post(device, uname);
38218480Sralph 
38318480Sralph 	/* poke init */
38418480Sralph 	if (kill(1, SIGHUP)) {
38518480Sralph 		fprintf(stderr,
38618480Sralph 		    "Cannot send hangup to init process: %s\n",
38718480Sralph 			sys_errlist[errno]);
38818614Sralph 		(void)settys(resettty);
38923723Sbloom 		(void)setmodem(device, resetmodem);
39018480Sralph 		exit(1);
39118480Sralph 	}
39218480Sralph 
39318480Sralph 	if (enable)
39418480Sralph 		return;
39518480Sralph 
39618480Sralph 	/* wait till init has responded, clearing the utmp entry */
39725123Sbloom 	i = 100;
39818480Sralph 	do {
39918480Sralph 		sleep(1);
40025123Sbloom 		if (lseek(etcutmp, utmploc, 0) < 0)
40123723Sbloom 			fprintf(stderr, "On lseek in /etc/utmp: %s",
40218480Sralph 				sys_errlist[errno]);
40325123Sbloom 		if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0)
40423723Sbloom 			fprintf(stderr, "On read from /etc/utmp: %s",
40518480Sralph 				sys_errlist[errno]);
40618614Sralph 	} while (utmp.ut_name[0] != '\0' && --i > 0);
40718480Sralph }
40818480Sralph 
40923723Sbloom #ifdef BSD4_3
41018480Sralph /* identify terminal line in ttys */
41118480Sralph opnttys(device)
41218480Sralph char *device;
41318480Sralph {
41423723Sbloom 	register int  ndevice;
41523723Sbloom 	register char *p;
41623723Sbloom 	char *index();
41723723Sbloom 	char linebuf[BUFSIZ];
41823723Sbloom 
41925123Sbloom 	ttysfile = NULL;
42025123Sbloom 	do {
42125123Sbloom 		if (ttysfile != NULL) {
42225123Sbloom 			fclose(ttysfile);
42325123Sbloom 			sleep(5);
42425123Sbloom 		}
42525123Sbloom 		ttysfile = fopen(Etcttys, "r");
42625123Sbloom 		if(ttysfile == NULL) {
42725123Sbloom 			fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
42825123Sbloom 				sys_errlist[errno]);
42925123Sbloom 			exit(1);
43025123Sbloom 		}
43125123Sbloom 	} while (flock(fileno(ttysfile), LOCK_NB|LOCK_EX) < 0);
43223723Sbloom 	nttysfile = fopen(NEtcttys, "w");
43323723Sbloom 	if(nttysfile == NULL) {
43423723Sbloom 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
43523723Sbloom 			sys_errlist[errno]);
43623723Sbloom 		exit(1);
43723723Sbloom 	}
43823723Sbloom 
43923723Sbloom 	ndevice = strlen(device);
44025962Sbloom #ifndef BRL4_2
44125123Sbloom 	utmploc = sizeof(utmp);
44225962Sbloom #else BRL4_2
44325962Sbloom 	utmploc = 0;
44425962Sbloom #endif BRL4_2
44523723Sbloom 
44623723Sbloom 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
44725123Sbloom 		if(strncmp(device, linebuf, ndevice) == 0)
44825123Sbloom 			return;
44925123Sbloom 		ttyslnbeg += strlen(linebuf);
45025962Sbloom 		if (linebuf[0] != '#' && linebuf[0] != '\0')
45125962Sbloom 			utmploc += sizeof(utmp);
45223723Sbloom 		if (fputs(linebuf, nttysfile) == NULL) {
45323723Sbloom 			fprintf(stderr, "On %s write: %s\n",
45423723Sbloom 				Etcttys, sys_errlist[errno]);
45523723Sbloom 			exit(1);
45623723Sbloom 		}
45723723Sbloom 
45823723Sbloom 	}
45923723Sbloom 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
46023723Sbloom 	exit(1);
46123723Sbloom }
46223723Sbloom 
46323723Sbloom /* modify appropriate line in /etc/ttys to turn on/off the device */
46423723Sbloom settys(enable)
46523723Sbloom int enable;
46623723Sbloom {
46723723Sbloom 	register char *cp, *cp2;
46823723Sbloom 	char lbuf[BUFSIZ];
46923723Sbloom 	int i;
47023723Sbloom 	char c1, c2;
47123723Sbloom 
47225123Sbloom 	(void) fseek(ttysfile, ttyslnbeg, 0);
47325123Sbloom 	if(fgets(lbuf, BUFSIZ, ttysfile) == NULL) {
47423723Sbloom 		fprintf(stderr, "On %s read: %s\n",
47523723Sbloom 			Etcttys, sys_errlist[errno]);
47623723Sbloom 		exit(1);
47723723Sbloom 	}
47823723Sbloom 	/* format is now */
47923723Sbloom 	/* ttyd0 std.100 dialup on secure # comment */
48025364Sbloom 	/* except, 2nd item may have embedded spaces inside quotes, Hubert */
48123723Sbloom 	cp = lbuf;
48223723Sbloom 	for (i=0;*cp && i<3;i++) {
48325364Sbloom 		if (*cp == '"') {
48423723Sbloom 			cp++;
48525364Sbloom 			while (*cp && *cp != '"')
48625364Sbloom 				cp++;
48725364Sbloom 			if (*cp != '\0')
48825364Sbloom 				cp++;
48925364Sbloom 		}else {
49025364Sbloom 			while (*cp && *cp != ' ' && *cp != '\t')
49125364Sbloom 				cp++;
49225364Sbloom 		}
49323723Sbloom 		while (*cp && (*cp == ' ' || *cp == '\t'))
49423723Sbloom 			cp++;
49523723Sbloom 	}
49623723Sbloom 	if (*cp == '\0') {
49723723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
49823723Sbloom 		exit(1);
49923723Sbloom 	}
50023723Sbloom 	c1 = *--cp;
50123723Sbloom 	*cp++ = '\0';
50223723Sbloom 	cp2 = cp;
50323723Sbloom 	while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
50423723Sbloom 		cp++;
50523723Sbloom 	if (*cp == '\0') {
50623723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
50723723Sbloom 		exit(1);
50823723Sbloom 	}
50923723Sbloom 	c2 = *cp;
51023723Sbloom 	*cp++ = '\0';
51123723Sbloom 	while (*cp && (*cp == ' ' || *cp == '\t'))
51223723Sbloom 		cp++;
51323723Sbloom 	resettty = strcmp("on", cp2) != 0;
51425123Sbloom 	fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
51525123Sbloom 	if (ferror(nttysfile)) {
51623723Sbloom 		fprintf(stderr, "On %s fprintf: %s\n",
51723723Sbloom 			NEtcttys, sys_errlist[errno]);
51823723Sbloom 		exit(1);
51923723Sbloom 	}
52025123Sbloom 	while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) {
52125123Sbloom 		if (fputs(lbuf, nttysfile) == NULL) {
52223723Sbloom 			fprintf(stderr, "On %s write: %s\n",
52323723Sbloom 				NEtcttys, sys_errlist[errno]);
52423723Sbloom 			exit(1);
52523723Sbloom 		}
52623723Sbloom 	}
52723723Sbloom 
52823723Sbloom 	if (enable^resettty)
52923723Sbloom 		(void) unlink(NEtcttys);
53023723Sbloom 	else {
53123723Sbloom 		struct stat statb;
53223723Sbloom 		if (stat(Etcttys, &statb) == 0) {
53325123Sbloom 			fchmod(fileno(nttysfile) ,statb.st_mode);
53425123Sbloom 			fchown(fileno(nttysfile), statb.st_uid, statb.st_gid);
53523723Sbloom 		}
53623723Sbloom 		(void) rename(NEtcttys, Etcttys);
53723723Sbloom 	}
53825123Sbloom 	(void) fclose(nttysfile);
53925123Sbloom 	(void) fclose(ttysfile);
54023723Sbloom 	return enable^resettty;
54123723Sbloom }
54223723Sbloom 
54323723Sbloom #else !BSD4_3
54423723Sbloom 
54523723Sbloom /* identify terminal line in ttys */
54623723Sbloom opnttys(device)
54723723Sbloom char *device;
54823723Sbloom {
54918480Sralph 	register FILE *ttysfile;
55018480Sralph 	register int  ndevice, lnsiz;
55118480Sralph 	register char *p;
55218480Sralph 	char *index();
55323723Sbloom 	char linebuf[BUFSIZ];
55418480Sralph 
55518480Sralph 	ttysfile = fopen(Etcttys, "r");
55618480Sralph 	if(ttysfile == NULL) {
55718480Sralph 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
55818480Sralph 			sys_errlist[errno]);
55918480Sralph 		exit(1);
56018480Sralph 	}
56118480Sralph 
56218480Sralph 	ndevice = strlen(device);
56318480Sralph 	ttyslnbeg = 0;
56418480Sralph 	utmploc = 0;
56518480Sralph 
56618480Sralph 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
56718480Sralph 		lnsiz = strlen(linebuf);
56823723Sbloom 		if ((p = index(linebuf, '\n')) != NULL)
56918480Sralph 			*p = '\0';
57018480Sralph 		if(strncmp(device, &linebuf[2], ndevice) == 0) {
57118480Sralph 			(void)fclose(ttysfile);
57233557Srick #ifdef sequent
57333557Srick 			/* Why is the sequent off by one? */
57433557Srick 			utmploc += sizeof(utmp);
57533557Srick #endif sequent
57618480Sralph 			return;
57718480Sralph 		}
57818480Sralph 		ttyslnbeg += lnsiz;
57925123Sbloom 		utmploc += sizeof(utmp);
58018480Sralph 	}
58118480Sralph 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
58218480Sralph 	exit(1);
58318480Sralph }
58418480Sralph 
58518480Sralph /* modify appropriate line in /etc/ttys to turn on/off the device */
58618480Sralph settys(enable)
58718480Sralph int enable;
58818480Sralph {
58918480Sralph 	int ittysfil;
59023723Sbloom 	char out, in;
59118480Sralph 
59218480Sralph 	ittysfil = open(Etcttys, 2);
59323723Sbloom 	if(ittysfil < 0) {
59418480Sralph 		fprintf(stderr, "Cannot open %s for output: %s\n",
59518480Sralph 			Etcttys, sys_errlist[errno]);
59618480Sralph 		exit(1);
59718480Sralph 	}
59823723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
59923723Sbloom 	if(read(ittysfil, &in, 1)<0) {
60023723Sbloom 		fprintf(stderr, "On %s write: %s\n",
60118480Sralph 			Etcttys, sys_errlist[errno]);
60218480Sralph 		exit(1);
60318480Sralph 	}
60418480Sralph 	resettty = (in == '1');
60518480Sralph 	out = enable ? '1' : '0';
60623723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
60723723Sbloom 	if(write(ittysfil, &out, 1)<0) {
60823723Sbloom 		fprintf(stderr, "On %s write: %s\n",
60918480Sralph 			Etcttys, sys_errlist[errno]);
61018480Sralph 		exit(1);
61118480Sralph 	}
61218480Sralph 	(void)close(ittysfil);
61318614Sralph 	return(in==out);
61418480Sralph }
61523723Sbloom #endif !BSD4_3
61618480Sralph 
61733557Srick #ifdef sequent
61833557Srick setmodem(ttyline, enable)
61933557Srick char *ttyline; int enable;
62033557Srick {
62133557Srick 	char *sysbuf[BUFSIZ];
62233557Srick 	sprintf(sysbuf,"/etc/ttyconfig /dev/%s -special %s", ttyline,
62333557Srick 		enable ? "-carrier" : "-nocarrier");
62433557Srick 	system(sysbuf);
62533557Srick }
62633557Srick #endif /* sequent */
62733557Srick #ifdef vax
62818480Sralph /*
62923723Sbloom  * Excerpted from (June 8, 1983 W.Sebok)
63018480Sralph  * > ttymodem.c - enable/disable modem control for tty lines.
63118480Sralph  * >
63218480Sralph  * > Knows about DZ11s and DH11/DM11s.
63318480Sralph  * > 23.3.83 - TS
63423723Sbloom  * > modified to know about DMF's  (hasn't been tested) Nov 8, 1984 - WLS
63518480Sralph  */
63618480Sralph 
63718480Sralph 
63823723Sbloom setmodem(ttyline, enable)
63918480Sralph char *ttyline; int enable;
64018480Sralph {
64118480Sralph 	dev_t dev;
64218480Sralph 	int kmem;
64323723Sbloom 	int unit, line, nlines, addr, tflags;
64418614Sralph 	int devtype=0;
64518614Sralph 	char cflags; short sflags;
64618614Sralph #ifdef BSD4_2
64718614Sralph 	int flags;
64818614Sralph #else
64918614Sralph 	short flags;
65018614Sralph #endif
65118480Sralph 	struct uba_device *ubinfo;
65218480Sralph 	struct stat statb;
65318480Sralph 	struct cdevsw cdevsw;
65418480Sralph 
65518480Sralph 	if(nl[CDEVSW].n_type == 0) {
65623723Sbloom 		fprintf(stderr, "No namelist.\n");
65718480Sralph 		return(-1);
65818480Sralph 	}
65918480Sralph 
66018480Sralph 	if((kmem = open("/dev/kmem", 2)) < 0) {
66123723Sbloom 		fprintf(stderr, "/dev/kmem open: %s\n", sys_errlist[errno]);
66218480Sralph 		return(-1);
66318480Sralph 	}
66418480Sralph 
66523723Sbloom 	if(stat(ttyline, &statb) < 0) {
66623723Sbloom 		fprintf(stderr, "%s stat: %s\n", ttyline, sys_errlist[errno]);
66718480Sralph 		return(-1);
66818480Sralph 	}
66918480Sralph 
67018614Sralph 	if((statb.st_mode&S_IFMT) != S_IFCHR) {
67123723Sbloom 		fprintf(stderr, "%s is not a character device.\n",ttyline);
67218480Sralph 		return(-1);
67318480Sralph 	}
67418480Sralph 
67518480Sralph 	dev = statb.st_rdev;
67618480Sralph 	(void)lseek(kmem,
67718614Sralph 		(off_t) &(((struct cdevsw *)NLVALUE(CDEVSW))[major(dev)]),0);
67823723Sbloom 	(void)read(kmem, (char *) &cdevsw, sizeof cdevsw);
67918480Sralph 
68018480Sralph 	if((int)(cdevsw.d_open) == NLVALUE(DZOPEN)) {
68118480Sralph 		devtype = DZ11;
68218480Sralph 		unit = minor(dev) / NDZLINE;
68318480Sralph 		line = minor(dev) % NDZLINE;
68418480Sralph 		addr = (int) &(((int *)NLVALUE(DZINFO))[unit]);
68523723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDZ11), 0);
68618480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DHOPEN)) {
68718480Sralph 		devtype = DH11;
68818480Sralph 		unit = minor(dev) / NDHLINE;
68918480Sralph 		line = minor(dev) % NDHLINE;
69018480Sralph 		addr = (int) &(((int *)NLVALUE(DHINFO))[unit]);
69123723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDH11), 0);
69218480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DMFOPEN)) {
69318480Sralph 		devtype = DMF;
69418480Sralph 		unit = minor(dev) / NDMFLINE;
69518480Sralph 		line = minor(dev) % NDMFLINE;
69618480Sralph 		addr = (int) &(((int *)NLVALUE(DMFINFO))[unit]);
69723723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDMF), 0);
69818480Sralph 	} else {
69923723Sbloom 		fprintf(stderr, "Device %s (%d/%d) unknown.\n", ttyline,
70023723Sbloom 		    major(dev), minor(dev));
70118480Sralph 		return(-1);
70218480Sralph 	}
70318480Sralph 
70423723Sbloom 	(void)read(kmem, (char *) &nlines, sizeof nlines);
70518480Sralph 	if(minor(dev) >= nlines) {
70623723Sbloom 		fprintf(stderr, "Sub-device %d does not exist (only %d).\n",
70723723Sbloom 		    minor(dev), nlines);
70818480Sralph 		return(-1);
70918480Sralph 	}
71018480Sralph 
71123723Sbloom 	(void)lseek(kmem, (off_t)addr, 0);
71223723Sbloom 	(void)read(kmem, (char *) &ubinfo, sizeof ubinfo);
71323723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
71423723Sbloom 	(void)read(kmem, (char *) &flags, sizeof flags);
71518480Sralph 
71618480Sralph 	tflags = 1<<line;
71718480Sralph 	resetmodem = ((flags&tflags) == 0);
71818480Sralph 	flags = enable ? (flags & ~tflags) : (flags | tflags);
71923723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
72023723Sbloom 	(void)write(kmem, (char *) &flags, sizeof flags);
72118480Sralph 	switch(devtype) {
72218480Sralph 		case DZ11:
72318480Sralph 			if((addr = NLVALUE(DZSCAR)) == 0) {
72423723Sbloom 				fprintf(stderr, "No dzsoftCAR.\n");
72518480Sralph 				return(-1);
72618480Sralph 			}
72718614Sralph 			cflags = flags;
72823723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
72923723Sbloom 			(void)write(kmem, (char *) &cflags, sizeof cflags);
73018480Sralph 			break;
73118480Sralph 		case DH11:
73218480Sralph 			if((addr = NLVALUE(DHSCAR)) == 0) {
73323723Sbloom 				fprintf(stderr, "No dhsoftCAR.\n");
73418480Sralph 				return(-1);
73518480Sralph 			}
73618614Sralph 			sflags = flags;
73723723Sbloom 			(void)lseek(kmem, (off_t) &(((short *)addr)[unit]), 0);
73823723Sbloom 			(void)write(kmem, (char *) &sflags, sizeof sflags);
73918480Sralph 			break;
74018480Sralph 		case DMF:
74118480Sralph 			if((addr = NLVALUE(DMFSCAR)) == 0) {
74223723Sbloom 				fprintf(stderr, "No dmfsoftCAR.\n");
74318480Sralph 				return(-1);
74418480Sralph 			}
74518614Sralph 			cflags = flags;
74623723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
74734008Sbostic 			(void)write(kmem, (char *) &cflags, sizeof cflags);
74818480Sralph 			break;
74918480Sralph 		default:
75023723Sbloom 			fprintf(stderr, "Unknown device type\n");
75118480Sralph 			return(-1);
75218480Sralph 	}
75318480Sralph 	return(0);
75418480Sralph }
75533557Srick #endif /* vax */
75618480Sralph 
75718480Sralph prefix(s1, s2)
75818480Sralph 	register char *s1, *s2;
75918480Sralph {
76018480Sralph 	register char c;
76118480Sralph 
76218480Sralph 	while ((c = *s1++) == *s2++)
76318480Sralph 		if (c == '\0')
76418480Sralph 			return (1);
76518480Sralph 	return (c == '\0');
76618480Sralph }
76733592Srick #else	/* !DIALINOUT */
76833592Srick main()
76933592Srick {
77033592Srick 	fprintf(stderr,"acucntrl is not supported on this system\n");
77133592Srick }
77233592Srick #endif /* !DIALINOUT */
773