1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Adams.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1988, 1993\n\
14 The Regents of the University of California. All rights reserved.\n";
15 #endif /* not lint */
16
17 #ifndef lint
18 static char sccsid[] = "@(#)slattach.c 8.2 (Berkeley) 01/07/94";
19 #endif /* not lint */
20
21 #include <sys/param.h>
22 #include <sgtty.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <net/if.h>
26 #include <netdb.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <paths.h>
30
31 #define DEFAULT_BAUD 9600
32 int slipdisc = SLIPDISC;
33
34 char devname[32];
35 char hostname[MAXHOSTNAMELEN];
36
main(argc,argv)37 main(argc, argv)
38 int argc;
39 char *argv[];
40 {
41 register int fd;
42 register char *dev = argv[1];
43 struct sgttyb sgtty;
44 int speed;
45
46 if (argc < 2 || argc > 3) {
47 fprintf(stderr, "usage: %s ttyname [baudrate]\n", argv[0]);
48 exit(1);
49 }
50 speed = argc == 3 ? findspeed(atoi(argv[2])) : findspeed(DEFAULT_BAUD);
51 if (speed == 0) {
52 fprintf(stderr, "unknown speed %s", argv[2]);
53 exit(1);
54 }
55 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
56 (void)snprintf(devname, sizeof(devname),
57 "%s%s", _PATH_DEV, dev);
58 dev = devname;
59 }
60 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
61 perror(dev);
62 exit(1);
63 }
64 sgtty.sg_flags = RAW | ANYP;
65 sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
66 if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
67 perror("ioctl(TIOCSETP)");
68 exit(1);
69 }
70 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
71 perror("ioctl(TIOCSETD)");
72 exit(1);
73 }
74
75 if (fork() > 0)
76 exit(0);
77 for (;;)
78 sigpause(0L);
79 }
80
81 struct sg_spds {
82 int sp_val, sp_name;
83 } spds[] = {
84 #ifdef B50
85 { 50, B50 },
86 #endif
87 #ifdef B75
88 { 75, B75 },
89 #endif
90 #ifdef B110
91 { 110, B110 },
92 #endif
93 #ifdef B150
94 { 150, B150 },
95 #endif
96 #ifdef B200
97 { 200, B200 },
98 #endif
99 #ifdef B300
100 { 300, B300 },
101 #endif
102 #ifdef B600
103 { 600, B600 },
104 #endif
105 #ifdef B1200
106 { 1200, B1200 },
107 #endif
108 #ifdef B1800
109 { 1800, B1800 },
110 #endif
111 #ifdef B2000
112 { 2000, B2000 },
113 #endif
114 #ifdef B2400
115 { 2400, B2400 },
116 #endif
117 #ifdef B3600
118 { 3600, B3600 },
119 #endif
120 #ifdef B4800
121 { 4800, B4800 },
122 #endif
123 #ifdef B7200
124 { 7200, B7200 },
125 #endif
126 #ifdef B9600
127 { 9600, B9600 },
128 #endif
129 #ifdef EXTA
130 { 19200, EXTA },
131 #endif
132 #ifdef EXTB
133 { 38400, EXTB },
134 #endif
135 { 0, 0 }
136 };
137
findspeed(speed)138 findspeed(speed)
139 register int speed;
140 {
141 register struct sg_spds *sp;
142
143 sp = spds;
144 while (sp->sp_val && sp->sp_val != speed)
145 sp++;
146 return (sp->sp_name);
147 }
148