118480Sralph #ifndef lint
2*37930Sbostic static char sccsid[] = "@(#)acucntrl.c	5.16	(Berkeley) 05/11/89";
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
22*37930Sbostic  *	(2) look in _PATH_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
26*37930Sbostic  *	(6) edit _PATH_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
29*37930Sbostic  *	(8) post uid name in capitals in _PATH_UTMP to let world know device
30*37930Sbostic  *	    has been grabbed
3118480Sralph  *	(9) make sure that DTR is on
3218480Sralph  *
3318480Sralph  *   enable (i.e.) restore for dialin
3418480Sralph  *	(1) check input arguments
35*37930Sbostic  *	(2) look in _PATH_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
40*37930Sbostic  *	(7) edit _PATH_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
43*37930Sbostic  *	(9) clear uid name for _PATH_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>
67*37930Sbostic #include "pathnames.h"
6818480Sralph 
6918480Sralph #define NDZLINE	8	/* lines/dz */
7018480Sralph #define NDHLINE	16	/* lines/dh */
7118480Sralph #define NDMFLINE 8	/* lines/dmf */
7218480Sralph 
7318480Sralph #define DZ11	1
7418480Sralph #define DH11	2
7518480Sralph #define DMF	3
7618480Sralph 
7718480Sralph #define NLVALUE(val)	(nl[val].n_value)
7818480Sralph 
7918480Sralph struct nlist nl[] = {
8018480Sralph #define CDEVSW	0
8118480Sralph 	{ "_cdevsw" },
8218480Sralph 
8318480Sralph #define DZOPEN	1
8418480Sralph 	{ "_dzopen" },
8518480Sralph #define DZINFO	2
8618480Sralph 	{ "_dzinfo" },
8718480Sralph #define NDZ11	3
8818480Sralph 	{ "_dz_cnt" },
8918480Sralph #define DZSCAR	4
9018480Sralph 	{ "_dzsoftCAR" },
9118480Sralph 
9218480Sralph #define DHOPEN	5
9318480Sralph 	{ "_dhopen" },
9418480Sralph #define DHINFO	6
9518480Sralph 	{ "_dhinfo" },
9618480Sralph #define NDH11	7
9718480Sralph 	{ "_ndh11" },
9818480Sralph #define DHSCAR	8
9918480Sralph 	{ "_dhsoftCAR" },
10018480Sralph 
10118480Sralph #define DMFOPEN	9
10218480Sralph 	{ "_dmfopen" },
10318480Sralph #define DMFINFO	10
10418480Sralph 	{ "_dmfinfo" },
10518480Sralph #define NDMF	11
10618480Sralph 	{ "_ndmf" },
10718480Sralph #define DMFSCAR	12
10818480Sralph 	{ "_dmfsoftCAR" },
10918480Sralph 
11018480Sralph 	{ "\0" }
11118480Sralph };
11218480Sralph 
11318480Sralph #define ENABLE	1
11418480Sralph #define DISABLE	0
11518480Sralph 
116*37930Sbostic char Etcttys[] = _PATH_TTYS;
11723723Sbloom #ifdef BSD4_3
11825123Sbloom FILE *ttysfile, *nttysfile;
119*37930Sbostic char NEtcttys[] = _PATH_NEWTTYS;
12023723Sbloom extern long ftell();
12123723Sbloom #endif BSD4_3
122*37930Sbostic char Devhome[] = _PATH_DEV;
12318480Sralph 
12418480Sralph char usage[] = "Usage: acucntrl {dis|en}able ttydX\n";
12518480Sralph 
12618480Sralph struct utmp utmp;
12718480Sralph char resettty, resetmodem;
12818480Sralph int etcutmp;
12934163Srick off_t utmploc;
13034163Srick 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 */
15234163Srick 	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 */
174*37930Sbostic 	nlist(_PATH_UNIX, 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 
19434163Srick 	if (strcmp(p, "uucp") == 0 && argc == 4)
19534163Srick 		p = argv[3];
19634163Srick 
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 */
205*37930Sbostic 	if( (etcutmp = open(_PATH_UTMP, 2)) < 0) {
20623723Sbloom 		fprintf(stderr, "On open %s open: %s\n",
207*37930Sbostic 			_PATH_UTMP, 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)
367*37930Sbostic 		fprintf(stderr, "on lseek in %s: %s",
368*37930Sbostic 			_PATH_UTMP, sys_errlist[errno]);
36925123Sbloom 	if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0)
370*37930Sbostic 		fprintf(stderr, "on write in %s: %s",
371*37930Sbostic 			_PATH_UTMP, 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)
401*37930Sbostic 			fprintf(stderr, "On lseek in %s: %s",
402*37930Sbostic 				_PATH_UTMP, sys_errlist[errno]);
40325123Sbloom 		if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0)
404*37930Sbostic 			fprintf(stderr, "On read from %s: %s",
405*37930Sbostic 				_PATH_UTMP, 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 
463*37930Sbostic /* modify appropriate line in _PATH_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') {
497*37930Sbostic 		fprintf(stderr,"Badly formatted line in %s:\n%s",
498*37930Sbostic 		    _PATH_TTYS, lbuf);
49923723Sbloom 		exit(1);
50023723Sbloom 	}
50123723Sbloom 	c1 = *--cp;
50223723Sbloom 	*cp++ = '\0';
50323723Sbloom 	cp2 = cp;
50423723Sbloom 	while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
50523723Sbloom 		cp++;
50623723Sbloom 	if (*cp == '\0') {
507*37930Sbostic 		fprintf(stderr,"Badly formatted line in %s:\n%s",
508*37930Sbostic 		    _PATH_TTYS, lbuf);
50923723Sbloom 		exit(1);
51023723Sbloom 	}
51123723Sbloom 	c2 = *cp;
51223723Sbloom 	*cp++ = '\0';
51323723Sbloom 	while (*cp && (*cp == ' ' || *cp == '\t'))
51423723Sbloom 		cp++;
51523723Sbloom 	resettty = strcmp("on", cp2) != 0;
51625123Sbloom 	fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
51725123Sbloom 	if (ferror(nttysfile)) {
51823723Sbloom 		fprintf(stderr, "On %s fprintf: %s\n",
51923723Sbloom 			NEtcttys, sys_errlist[errno]);
52023723Sbloom 		exit(1);
52123723Sbloom 	}
52225123Sbloom 	while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) {
52325123Sbloom 		if (fputs(lbuf, nttysfile) == NULL) {
52423723Sbloom 			fprintf(stderr, "On %s write: %s\n",
52523723Sbloom 				NEtcttys, sys_errlist[errno]);
52623723Sbloom 			exit(1);
52723723Sbloom 		}
52823723Sbloom 	}
52923723Sbloom 
53023723Sbloom 	if (enable^resettty)
53123723Sbloom 		(void) unlink(NEtcttys);
53223723Sbloom 	else {
53323723Sbloom 		struct stat statb;
53423723Sbloom 		if (stat(Etcttys, &statb) == 0) {
53525123Sbloom 			fchmod(fileno(nttysfile) ,statb.st_mode);
53625123Sbloom 			fchown(fileno(nttysfile), statb.st_uid, statb.st_gid);
53723723Sbloom 		}
53823723Sbloom 		(void) rename(NEtcttys, Etcttys);
53923723Sbloom 	}
54025123Sbloom 	(void) fclose(nttysfile);
54125123Sbloom 	(void) fclose(ttysfile);
54223723Sbloom 	return enable^resettty;
54323723Sbloom }
54423723Sbloom 
54523723Sbloom #else !BSD4_3
54623723Sbloom 
54723723Sbloom /* identify terminal line in ttys */
54823723Sbloom opnttys(device)
54923723Sbloom char *device;
55023723Sbloom {
55118480Sralph 	register FILE *ttysfile;
55218480Sralph 	register int  ndevice, lnsiz;
55318480Sralph 	register char *p;
55418480Sralph 	char *index();
55523723Sbloom 	char linebuf[BUFSIZ];
55618480Sralph 
55718480Sralph 	ttysfile = fopen(Etcttys, "r");
55818480Sralph 	if(ttysfile == NULL) {
55918480Sralph 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
56018480Sralph 			sys_errlist[errno]);
56118480Sralph 		exit(1);
56218480Sralph 	}
56318480Sralph 
56418480Sralph 	ndevice = strlen(device);
56518480Sralph 	ttyslnbeg = 0;
56618480Sralph 	utmploc = 0;
56718480Sralph 
56818480Sralph 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
56918480Sralph 		lnsiz = strlen(linebuf);
57023723Sbloom 		if ((p = index(linebuf, '\n')) != NULL)
57118480Sralph 			*p = '\0';
57218480Sralph 		if(strncmp(device, &linebuf[2], ndevice) == 0) {
57318480Sralph 			(void)fclose(ttysfile);
57433557Srick #ifdef sequent
57533557Srick 			/* Why is the sequent off by one? */
57633557Srick 			utmploc += sizeof(utmp);
57733557Srick #endif sequent
57818480Sralph 			return;
57918480Sralph 		}
58018480Sralph 		ttyslnbeg += lnsiz;
58125123Sbloom 		utmploc += sizeof(utmp);
58218480Sralph 	}
58318480Sralph 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
58418480Sralph 	exit(1);
58518480Sralph }
58618480Sralph 
587*37930Sbostic /* modify appropriate line in _PATH_TTYS to turn on/off the device */
58818480Sralph settys(enable)
58918480Sralph int enable;
59018480Sralph {
59118480Sralph 	int ittysfil;
59223723Sbloom 	char out, in;
59318480Sralph 
59418480Sralph 	ittysfil = open(Etcttys, 2);
59523723Sbloom 	if(ittysfil < 0) {
59618480Sralph 		fprintf(stderr, "Cannot open %s for output: %s\n",
59718480Sralph 			Etcttys, sys_errlist[errno]);
59818480Sralph 		exit(1);
59918480Sralph 	}
60023723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
60123723Sbloom 	if(read(ittysfil, &in, 1)<0) {
60223723Sbloom 		fprintf(stderr, "On %s write: %s\n",
60318480Sralph 			Etcttys, sys_errlist[errno]);
60418480Sralph 		exit(1);
60518480Sralph 	}
60618480Sralph 	resettty = (in == '1');
60718480Sralph 	out = enable ? '1' : '0';
60823723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
60923723Sbloom 	if(write(ittysfil, &out, 1)<0) {
61023723Sbloom 		fprintf(stderr, "On %s write: %s\n",
61118480Sralph 			Etcttys, sys_errlist[errno]);
61218480Sralph 		exit(1);
61318480Sralph 	}
61418480Sralph 	(void)close(ittysfil);
61518614Sralph 	return(in==out);
61618480Sralph }
61723723Sbloom #endif !BSD4_3
61818480Sralph 
61933557Srick #ifdef sequent
62033557Srick setmodem(ttyline, enable)
62133557Srick char *ttyline; int enable;
62233557Srick {
62333557Srick 	char *sysbuf[BUFSIZ];
62433557Srick 	sprintf(sysbuf,"/etc/ttyconfig /dev/%s -special %s", ttyline,
62533557Srick 		enable ? "-carrier" : "-nocarrier");
62633557Srick 	system(sysbuf);
62733557Srick }
62833557Srick #endif /* sequent */
62933557Srick #ifdef vax
63018480Sralph /*
63123723Sbloom  * Excerpted from (June 8, 1983 W.Sebok)
63218480Sralph  * > ttymodem.c - enable/disable modem control for tty lines.
63318480Sralph  * >
63418480Sralph  * > Knows about DZ11s and DH11/DM11s.
63518480Sralph  * > 23.3.83 - TS
63623723Sbloom  * > modified to know about DMF's  (hasn't been tested) Nov 8, 1984 - WLS
63718480Sralph  */
63818480Sralph 
63918480Sralph 
64023723Sbloom setmodem(ttyline, enable)
64118480Sralph char *ttyline; int enable;
64218480Sralph {
64318480Sralph 	dev_t dev;
64418480Sralph 	int kmem;
64523723Sbloom 	int unit, line, nlines, addr, tflags;
64618614Sralph 	int devtype=0;
64718614Sralph 	char cflags; short sflags;
64818614Sralph #ifdef BSD4_2
64918614Sralph 	int flags;
65018614Sralph #else
65118614Sralph 	short flags;
65218614Sralph #endif
65318480Sralph 	struct uba_device *ubinfo;
65418480Sralph 	struct stat statb;
65518480Sralph 	struct cdevsw cdevsw;
65618480Sralph 
65718480Sralph 	if(nl[CDEVSW].n_type == 0) {
65823723Sbloom 		fprintf(stderr, "No namelist.\n");
65918480Sralph 		return(-1);
66018480Sralph 	}
66118480Sralph 
662*37930Sbostic 	if((kmem = open(_PATH_KMEM, 2)) < 0) {
663*37930Sbostic 		fprintf(stderr, "%s open: %s\n", _PATH_KMEM,
664*37930Sbostic 		    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