slattach.c revision 1.1
11.1Scgd/*
21.1Scgd * Copyright (c) 1988 Regents of the University of California.
31.1Scgd * All rights reserved.
41.1Scgd *
51.1Scgd * This code is derived from software contributed to Berkeley by
61.1Scgd * Rick Adams.
71.1Scgd *
81.1Scgd * Redistribution and use in source and binary forms, with or without
91.1Scgd * modification, are permitted provided that the following conditions
101.1Scgd * are met:
111.1Scgd * 1. Redistributions of source code must retain the above copyright
121.1Scgd *    notice, this list of conditions and the following disclaimer.
131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer in the
151.1Scgd *    documentation and/or other materials provided with the distribution.
161.1Scgd * 3. All advertising materials mentioning features or use of this software
171.1Scgd *    must display the following acknowledgement:
181.1Scgd *	This product includes software developed by the University of
191.1Scgd *	California, Berkeley and its contributors.
201.1Scgd * 4. Neither the name of the University nor the names of its contributors
211.1Scgd *    may be used to endorse or promote products derived from this software
221.1Scgd *    without specific prior written permission.
231.1Scgd *
241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Scgd * SUCH DAMAGE.
351.1Scgd */
361.1Scgd
371.1Scgd#ifndef lint
381.1Scgdchar copyright[] =
391.1Scgd"@(#) Copyright (c) 1988 Regents of the University of California.\n\
401.1Scgd All rights reserved.\n";
411.1Scgd#endif /* not lint */
421.1Scgd
431.1Scgd#ifndef lint
441.1Scgdstatic char sccsid[] = "@(#)slattach.c	4.6 (Berkeley) 6/1/90";
451.1Scgd#endif /* not lint */
461.1Scgd
471.1Scgd#include <sys/param.h>
481.1Scgd#include <sgtty.h>
491.1Scgd#include <sys/socket.h>
501.1Scgd#include <netinet/in.h>
511.1Scgd#include <net/if.h>
521.1Scgd#include <netdb.h>
531.1Scgd#include <fcntl.h>
541.1Scgd#include <stdio.h>
551.1Scgd#include <paths.h>
561.1Scgd
571.1Scgd#define DEFAULT_BAUD	9600
581.1Scgdint	slipdisc = SLIPDISC;
591.1Scgd
601.1Scgdchar	devname[32];
611.1Scgdchar	hostname[MAXHOSTNAMELEN];
621.1Scgd
631.1Scgdmain(argc, argv)
641.1Scgd	int argc;
651.1Scgd	char *argv[];
661.1Scgd{
671.1Scgd	register int fd;
681.1Scgd	register char *dev = argv[1];
691.1Scgd	struct sgttyb sgtty;
701.1Scgd	int	speed;
711.1Scgd
721.1Scgd	if (argc < 2 || argc > 3) {
731.1Scgd		fprintf(stderr, "usage: %s ttyname [baudrate]\n", argv[0]);
741.1Scgd		exit(1);
751.1Scgd	}
761.1Scgd	speed = argc == 3 ? findspeed(atoi(argv[2])) : findspeed(DEFAULT_BAUD);
771.1Scgd	if (speed == 0) {
781.1Scgd		fprintf(stderr, "unknown speed %s", argv[2]);
791.1Scgd		exit(1);
801.1Scgd	}
811.1Scgd	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
821.1Scgd		(void)sprintf(devname, "%s/%s", _PATH_DEV, dev);
831.1Scgd		dev = devname;
841.1Scgd	}
851.1Scgd	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
861.1Scgd		perror(dev);
871.1Scgd		exit(1);
881.1Scgd	}
891.1Scgd	sgtty.sg_flags = RAW | ANYP;
901.1Scgd	sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
911.1Scgd	if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
921.1Scgd		perror("ioctl(TIOCSETP)");
931.1Scgd		exit(1);
941.1Scgd	}
951.1Scgd	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
961.1Scgd		perror("ioctl(TIOCSETD)");
971.1Scgd		exit(1);
981.1Scgd	}
991.1Scgd
1001.1Scgd	if (fork() > 0)
1011.1Scgd		exit(0);
1021.1Scgd	for (;;)
1031.1Scgd		sigpause(0L);
1041.1Scgd}
1051.1Scgd
1061.1Scgdstruct sg_spds {
1071.1Scgd	int sp_val, sp_name;
1081.1Scgd}       spds[] = {
1091.1Scgd#ifdef B50
1101.1Scgd	{ 50, B50 },
1111.1Scgd#endif
1121.1Scgd#ifdef B75
1131.1Scgd	{ 75, B75 },
1141.1Scgd#endif
1151.1Scgd#ifdef B110
1161.1Scgd	{ 110, B110 },
1171.1Scgd#endif
1181.1Scgd#ifdef B150
1191.1Scgd	{ 150, B150 },
1201.1Scgd#endif
1211.1Scgd#ifdef B200
1221.1Scgd	{ 200, B200 },
1231.1Scgd#endif
1241.1Scgd#ifdef B300
1251.1Scgd	{ 300, B300 },
1261.1Scgd#endif
1271.1Scgd#ifdef B600
1281.1Scgd	{ 600, B600 },
1291.1Scgd#endif
1301.1Scgd#ifdef B1200
1311.1Scgd	{ 1200, B1200 },
1321.1Scgd#endif
1331.1Scgd#ifdef B1800
1341.1Scgd	{ 1800, B1800 },
1351.1Scgd#endif
1361.1Scgd#ifdef B2000
1371.1Scgd	{ 2000, B2000 },
1381.1Scgd#endif
1391.1Scgd#ifdef B2400
1401.1Scgd	{ 2400, B2400 },
1411.1Scgd#endif
1421.1Scgd#ifdef B3600
1431.1Scgd	{ 3600, B3600 },
1441.1Scgd#endif
1451.1Scgd#ifdef B4800
1461.1Scgd	{ 4800, B4800 },
1471.1Scgd#endif
1481.1Scgd#ifdef B7200
1491.1Scgd	{ 7200, B7200 },
1501.1Scgd#endif
1511.1Scgd#ifdef B9600
1521.1Scgd	{ 9600, B9600 },
1531.1Scgd#endif
1541.1Scgd#ifdef EXTA
1551.1Scgd	{ 19200, EXTA },
1561.1Scgd#endif
1571.1Scgd#ifdef EXTB
1581.1Scgd	{ 38400, EXTB },
1591.1Scgd#endif
1601.1Scgd	{ 0, 0 }
1611.1Scgd};
1621.1Scgd
1631.1Scgdfindspeed(speed)
1641.1Scgd	register int speed;
1651.1Scgd{
1661.1Scgd	register struct sg_spds *sp;
1671.1Scgd
1681.1Scgd	sp = spds;
1691.1Scgd	while (sp->sp_val && sp->sp_val != speed)
1701.1Scgd		sp++;
1711.1Scgd	return (sp->sp_name);
1721.1Scgd}
173