Initial import, 0.1 + pk 0.2.4-B1
[unix-history] / sbin / slattach / slattach.c
CommitLineData
15637ed4
RG
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#ifndef lint
38char copyright[] =
39"@(#) Copyright (c) 1988 Regents of the University of California.\n\
40 All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static char sccsid[] = "@(#)slattach.c 4.6 (Berkeley) 6/1/90";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sgtty.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <net/if.h>
52#include <netdb.h>
53#include <fcntl.h>
54#include <stdio.h>
55#include <paths.h>
56
57#define DEFAULT_BAUD 9600
58int slipdisc = SLIPDISC;
59
60char devname[32];
61char hostname[MAXHOSTNAMELEN];
62
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67 register int fd;
68 register char *dev = argv[1];
69 struct sgttyb sgtty;
70 int speed;
71
72 if (argc < 2 || argc > 3) {
73 fprintf(stderr, "usage: %s ttyname [baudrate]\n", argv[0]);
74 exit(1);
75 }
76 speed = argc == 3 ? findspeed(atoi(argv[2])) : findspeed(DEFAULT_BAUD);
77 if (speed == 0) {
78 fprintf(stderr, "unknown speed %s", argv[2]);
79 exit(1);
80 }
81 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
82 (void)sprintf(devname, "%s/%s", _PATH_DEV, dev);
83 dev = devname;
84 }
85 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
86 perror(dev);
87 exit(1);
88 }
89 sgtty.sg_flags = RAW | ANYP;
90 sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
91 if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
92 perror("ioctl(TIOCSETP)");
93 exit(1);
94 }
95 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
96 perror("ioctl(TIOCSETD)");
97 exit(1);
98 }
99
100 if (fork() > 0)
101 exit(0);
102 for (;;)
103 sigpause(0L);
104}
105
106struct sg_spds {
107 int sp_val, sp_name;
108} spds[] = {
109#ifdef B50
110 { 50, B50 },
111#endif
112#ifdef B75
113 { 75, B75 },
114#endif
115#ifdef B110
116 { 110, B110 },
117#endif
118#ifdef B150
119 { 150, B150 },
120#endif
121#ifdef B200
122 { 200, B200 },
123#endif
124#ifdef B300
125 { 300, B300 },
126#endif
127#ifdef B600
128 { 600, B600 },
129#endif
130#ifdef B1200
131 { 1200, B1200 },
132#endif
133#ifdef B1800
134 { 1800, B1800 },
135#endif
136#ifdef B2000
137 { 2000, B2000 },
138#endif
139#ifdef B2400
140 { 2400, B2400 },
141#endif
142#ifdef B3600
143 { 3600, B3600 },
144#endif
145#ifdef B4800
146 { 4800, B4800 },
147#endif
148#ifdef B7200
149 { 7200, B7200 },
150#endif
151#ifdef B9600
152 { 9600, B9600 },
153#endif
154#ifdef EXTA
155 { 19200, EXTA },
156#endif
157#ifdef EXTB
158 { 38400, EXTB },
159#endif
160 { 0, 0 }
161};
162
163findspeed(speed)
164 register int speed;
165{
166 register struct sg_spds *sp;
167
168 sp = spds;
169 while (sp->sp_val && sp->sp_val != speed)
170 sp++;
171 return (sp->sp_name);
172}