slattach.c revision 1.9
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Adams.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/*
38 * Hacked to change from sgtty to POSIX termio style serial line control
39 * and added flag to enable cts/rts style flow control.
40 *
41 * blymn@awadi.com.au (Brett Lymn) 93.04.04
42 */
43
44#ifndef lint
45char copyright[] =
46"@(#) Copyright (c) 1988 Regents of the University of California.\n\
47 All rights reserved.\n";
48#endif /* not lint */
49
50#ifndef lint
51/*static char sccsid[] = "from: @(#)slattach.c	4.6 (Berkeley) 6/1/90";*/
52static char rcsid[] = "$Id: slattach.c,v 1.9 1993/12/10 13:20:12 cgd Exp $";
53#endif /* not lint */
54
55#include <sys/param.h>
56#include <sys/ioctl.h>
57#include <termios.h>
58#include <sys/socket.h>
59#include <netinet/in.h>
60#include <net/if.h>
61#include <net/if_slvar.h>
62#include <netdb.h>
63#include <fcntl.h>
64#include <stdio.h>
65#include <paths.h>
66
67#define DEFAULT_BAUD	9600
68
69static char usage_str[] = "\
70usage: %s [-h] [-m] [-s <speed>] <device>\n\
71	-h -- turn on CTS/RTS style flow control\n\
72	-m -- maintain DTR after last close (no HUPCL)\n\
73	-s -- baud rate (default 9600)\n";
74
75int main(int argc, char **argv)
76{
77	struct termios tty;
78	int option;
79	int fd;
80	char devname[32];
81	char *dev = (char *)0;
82	int slipdisc = SLIPDISC;
83	int speed = DEFAULT_BAUD;
84	int cflags = HUPCL;
85
86	extern char *optarg;
87	extern int optind;
88
89	while ((option = getopt(argc, argv, "achmns:")) != EOF) {
90		switch (option) {
91		case 'h':
92			cflags |= CRTSCTS;
93			break;
94		case 'm':
95			cflags &= ~HUPCL;
96			break;
97		case 's':
98			speed = atoi(optarg);
99			break;
100		case '?':
101		default:
102			fprintf(stderr, usage_str, argv[0]);
103			exit(1);
104		}
105	}
106
107	if (optind == argc - 1)
108		dev = argv[optind];
109
110	if (dev == (char *)0) {
111		fprintf(stderr, usage_str, argv[0]);
112		exit(2);
113	}
114
115	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
116		strcpy(devname, _PATH_DEV);
117		strcat(devname, "/");
118		strncat(devname, dev, 10);
119		dev = devname;
120	}
121
122	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
123		perror(dev);
124		exit(1);
125	}
126
127	if (tcgetattr(fd, &tty) < 0) {
128		perror("tcgetattr");
129		close(fd);
130		exit(1);
131	}
132	tty.c_iflag = 0;
133	tty.c_oflag = 0;
134	tty.c_cflag = CREAD | CS8 | cflags;
135	tty.c_lflag = 0;
136	tty.c_cc[VMIN] = 1; /* wait for one char */
137	tty.c_cc[VTIME] = 0; /* wait forever for a char */
138	cfsetispeed(&tty, speed);
139	cfsetospeed(&tty, speed);
140	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
141		perror("tcsetattr");
142		close(fd);
143		exit(1);
144	}
145
146	if (ioctl(fd, TIOCSDTR) < 0) {
147                perror("ioctl(TIOCSDTR)");
148                close(fd);
149                exit(1);
150        }
151
152	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
153		perror("ioctl(TIOCSETD)");
154		close(fd);
155		exit(1);
156	}
157
158	if (fork() > 0)
159		exit(0);
160
161	for (;;)
162		sigpause(0L);
163}
164