slattach.c revision 1.20
1/*	$NetBSD: slattach.c,v 1.20 1997/11/17 23:15:12 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Adams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40#ifndef lint
41__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
42	The Regents of the University of California.  All rights reserved.\n");
43#endif /* not lint */
44
45#ifndef lint
46#if 0
47static char sccsid[] = "@(#)slattach.c	8.2 (Berkeley) 1/7/94";
48#else
49__RCSID("$NetBSD: slattach.c,v 1.20 1997/11/17 23:15:12 thorpej Exp $");
50#endif
51#endif /* not lint */
52
53#include <sys/param.h>
54#include <sys/socket.h>
55#include <sys/types.h>
56#include <sys/ioctl.h>
57
58#include <net/if.h>
59#include <netinet/in.h>
60
61#include <err.h>
62#include <fcntl.h>
63#include <netdb.h>
64#include <paths.h>
65#include <signal.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <termios.h>
70#include <unistd.h>
71
72int	speed = 9600;
73int	slipdisc = SLIPDISC;
74
75char	devicename[32];
76
77int	main __P((int, char *[]));
78int	ttydisc __P((char *));
79void	usage __P((void));
80
81
82int
83main(argc, argv)
84	int argc;
85	char *argv[];
86{
87	int fd;
88	char *dev = argv[1];
89	struct termios tty;
90	tcflag_t cflag = HUPCL;
91	int ch;
92	sigset_t sigset;
93
94	while ((ch = getopt(argc, argv, "hHms:t:")) != -1) {
95		switch (ch) {
96		case 'h':
97			cflag |= CRTSCTS;
98			break;
99		case 'H':
100			cflag |= CDTRCTS;
101			break;
102		case 'm':
103			cflag &= ~HUPCL;
104			break;
105		case 's':
106			speed = atoi(optarg);
107			break;
108		case 'r': case 't':
109			slipdisc = ttydisc(optarg);
110			break;
111		case '?':
112		default:
113			usage();
114		}
115	}
116	argc -= optind;
117	argv += optind;
118
119	if (argc != 1)
120		usage();
121
122	dev = *argv;
123	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
124		(void)snprintf(devicename, sizeof(devicename),
125		    "%s%s", _PATH_DEV, dev);
126		dev = devicename;
127	}
128	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
129		perror(dev);
130		exit(1);
131	}
132	tty.c_cflag = CREAD | CS8 | cflag;
133	tty.c_iflag = 0;
134	tty.c_lflag = 0;
135	tty.c_oflag = 0;
136	tty.c_cc[VMIN] = 1;
137	tty.c_cc[VTIME] = 0;
138	cfsetspeed(&tty, speed);
139	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
140		err(1, "tcsetattr");
141	if (ioctl(fd, TIOCSDTR, 0) < 0)
142		err(1, "TIOCSDTR");
143	if (ioctl(fd, TIOCSETD, &slipdisc) < 0)
144		err(1, "TIOCSETD");
145
146	if (fork() > 0)
147		exit(0);
148	sigemptyset(&sigset);
149	for (;;)
150		sigsuspend(&sigset);
151}
152
153int
154ttydisc(name)
155     char *name;
156{
157	if (strcmp(name, "slip") == 0)
158		return(SLIPDISC);
159#ifdef STRIPDISC
160	else if (strcmp(name, "strip") == 0)
161  		return(STRIPDISC);
162#endif
163	else
164		usage();
165	/* NOTREACHED */
166	return -1;
167}
168
169void
170usage()
171{
172	extern char *__progname;
173
174	(void)fprintf(stderr,
175	    "usage: %s [-t ldisc] [-hm] [-s baudrate] ttyname\n",
176		__progname);
177	exit(1);
178}
179