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