slattach.c revision 1.10
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.10 1994/02/10 05:39:15 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 <net/slip.h>
60#include <netdb.h>
61#include <fcntl.h>
62#include <stdio.h>
63#include <paths.h>
64
65#define DEFAULT_BAUD	9600
66
67static char usage_str[] = "\
68usage: %s [-h] [-m] [-s <speed>] <device>\n\
69	-h -- turn on CTS/RTS style flow control\n\
70	-m -- maintain DTR after last close (no HUPCL)\n\
71	-s -- baud rate (default 9600)\n";
72
73int main(int argc, char **argv)
74{
75	struct termios tty;
76	int option;
77	int fd;
78	char devname[32];
79	char *dev = (char *)0;
80	int slipdisc = SLIPDISC;
81	int speed = DEFAULT_BAUD;
82	int cflags = HUPCL;
83
84	extern char *optarg;
85	extern int optind;
86
87	while ((option = getopt(argc, argv, "achmns:")) != EOF) {
88		switch (option) {
89		case 'h':
90			cflags |= CRTSCTS;
91			break;
92		case 'm':
93			cflags &= ~HUPCL;
94			break;
95		case 's':
96			speed = atoi(optarg);
97			break;
98		case '?':
99		default:
100			fprintf(stderr, usage_str, argv[0]);
101			exit(1);
102		}
103	}
104
105	if (optind == argc - 1)
106		dev = argv[optind];
107
108	if (dev == (char *)0) {
109		fprintf(stderr, usage_str, argv[0]);
110		exit(2);
111	}
112
113	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
114		strcpy(devname, _PATH_DEV);
115		strcat(devname, "/");
116		strncat(devname, dev, 10);
117		dev = devname;
118	}
119
120	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
121		perror(dev);
122		exit(1);
123	}
124
125	if (tcgetattr(fd, &tty) < 0) {
126		perror("tcgetattr");
127		close(fd);
128		exit(1);
129	}
130	tty.c_iflag = 0;
131	tty.c_oflag = 0;
132	tty.c_cflag = CREAD | CS8 | cflags;
133	tty.c_lflag = 0;
134	tty.c_cc[VMIN] = 1; /* wait for one char */
135	tty.c_cc[VTIME] = 0; /* wait forever for a char */
136	cfsetispeed(&tty, speed);
137	cfsetospeed(&tty, speed);
138	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
139		perror("tcsetattr");
140		close(fd);
141		exit(1);
142	}
143
144	if (ioctl(fd, TIOCSDTR) < 0) {
145                perror("ioctl(TIOCSDTR)");
146                close(fd);
147                exit(1);
148        }
149
150	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
151		perror("ioctl(TIOCSETD)");
152		close(fd);
153		exit(1);
154	}
155
156	if (fork() > 0)
157		exit(0);
158
159	for (;;)
160		sigpause(0L);
161}
162