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