BSD 4_3_Net_1 release
[unix-history] / slattach / slattach.c
CommitLineData
dd1fdcfb
KB
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 are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21#ifndef lint
22char copyright[] =
23"@(#) Copyright (c) 1988 Regents of the University of California.\n\
24 All rights reserved.\n";
25#endif /* not lint */
26
00c2d469 27#ifndef lint
ca67e7b4 28static char sccsid[] = "@(#)slattach.c 4.3 (Berkeley) 6/30/88";
dd1fdcfb
KB
29#endif /* not lint */
30
43660e2e 31#include <stdio.h>
00c2d469 32#include <sys/param.h>
43660e2e
MK
33#include <sgtty.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <net/if.h>
37#include <netdb.h>
38#include <fcntl.h>
39
43660e2e 40#define DEFAULT_BAUD 9600
43660e2e
MK
41int slipdisc = SLIPDISC;
42
43char devname[32];
00c2d469 44char hostname[MAXHOSTNAMELEN];
43660e2e 45
43660e2e
MK
46main(argc, argv)
47 int argc;
48 char *argv[];
49{
43660e2e
MK
50 register int fd;
51 register char *dev = argv[1];
52 struct sgttyb sgtty;
6eae156c 53 int speed;
43660e2e 54
00c2d469
MK
55 if (argc < 2 || argc > 3) {
56 fprintf(stderr, "usage: %s ttyname [baudrate]\n", argv[0]);
57 exit(1);
58 }
59 speed = argc == 3 ? findspeed(atoi(argv[2])) : findspeed(DEFAULT_BAUD);
60 if (speed == 0) {
61 fprintf(stderr, "unknown speed %s", argv[2]);
62 exit(1);
63 }
43660e2e 64 if (strncmp("/dev/", dev, 5)) {
ca67e7b4 65 (void)sprintf(devname, "/dev/%s", dev);
43660e2e
MK
66 dev = devname;
67 }
00c2d469
MK
68 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
69 perror(dev);
70 exit(1);
71 }
43660e2e
MK
72 sgtty.sg_flags = RAW | ANYP;
73 sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
00c2d469
MK
74 if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
75 perror("ioctl(TIOCSETP)");
76 exit(1);
77 }
78 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
79 perror("ioctl(TIOCSETD)");
80 exit(1);
81 }
43660e2e 82
00c2d469
MK
83 if (fork() > 0)
84 exit(0);
43660e2e 85 for (;;)
6eae156c 86 sigpause(0L);
43660e2e
MK
87}
88
89struct sg_spds {
90 int sp_val, sp_name;
91} spds[] = {
92#ifdef B50
93 { 50, B50 },
94#endif
95#ifdef B75
96 { 75, B75 },
97#endif
98#ifdef B110
99 { 110, B110 },
100#endif
101#ifdef B150
102 { 150, B150 },
103#endif
104#ifdef B200
105 { 200, B200 },
106#endif
107#ifdef B300
108 { 300, B300 },
109#endif
110#ifdef B600
111 { 600, B600 },
112#endif
113#ifdef B1200
114 { 1200, B1200 },
115#endif
116#ifdef B1800
117 { 1800, B1800 },
118#endif
119#ifdef B2000
120 { 2000, B2000 },
121#endif
122#ifdef B2400
123 { 2400, B2400 },
124#endif
125#ifdef B3600
126 { 3600, B3600 },
127#endif
128#ifdef B4800
129 { 4800, B4800 },
130#endif
131#ifdef B7200
132 { 7200, B7200 },
133#endif
134#ifdef B9600
135 { 9600, B9600 },
136#endif
137#ifdef EXTA
138 { 19200, EXTA },
139#endif
140#ifdef EXTB
141 { 38400, EXTB },
142#endif
143 { 0, 0 }
144};
145
146findspeed(speed)
147 register int speed;
148{
149 register struct sg_spds *sp;
150
151 sp = spds;
152 while (sp->sp_val && sp->sp_val != speed)
153 sp++;
154 return (sp->sp_name);
155}