xref: /original-bsd/sbin/slattach/slattach.c (revision c3e32dec)
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.1 (Berkeley) 06/05/93";
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 
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)sprintf(devname, "%s/%s", _PATH_DEV, dev);
57 		dev = devname;
58 	}
59 	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
60 		perror(dev);
61 		exit(1);
62 	}
63 	sgtty.sg_flags = RAW | ANYP;
64 	sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
65 	if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
66 		perror("ioctl(TIOCSETP)");
67 		exit(1);
68 	}
69 	if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
70 		perror("ioctl(TIOCSETD)");
71 		exit(1);
72 	}
73 
74 	if (fork() > 0)
75 		exit(0);
76 	for (;;)
77 		sigpause(0L);
78 }
79 
80 struct sg_spds {
81 	int sp_val, sp_name;
82 }       spds[] = {
83 #ifdef B50
84 	{ 50, B50 },
85 #endif
86 #ifdef B75
87 	{ 75, B75 },
88 #endif
89 #ifdef B110
90 	{ 110, B110 },
91 #endif
92 #ifdef B150
93 	{ 150, B150 },
94 #endif
95 #ifdef B200
96 	{ 200, B200 },
97 #endif
98 #ifdef B300
99 	{ 300, B300 },
100 #endif
101 #ifdef B600
102 	{ 600, B600 },
103 #endif
104 #ifdef B1200
105 	{ 1200, B1200 },
106 #endif
107 #ifdef B1800
108 	{ 1800, B1800 },
109 #endif
110 #ifdef B2000
111 	{ 2000, B2000 },
112 #endif
113 #ifdef B2400
114 	{ 2400, B2400 },
115 #endif
116 #ifdef B3600
117 	{ 3600, B3600 },
118 #endif
119 #ifdef B4800
120 	{ 4800, B4800 },
121 #endif
122 #ifdef B7200
123 	{ 7200, B7200 },
124 #endif
125 #ifdef B9600
126 	{ 9600, B9600 },
127 #endif
128 #ifdef EXTA
129 	{ 19200, EXTA },
130 #endif
131 #ifdef EXTB
132 	{ 38400, EXTB },
133 #endif
134 	{ 0, 0 }
135 };
136 
137 findspeed(speed)
138 	register int speed;
139 {
140 	register struct sg_spds *sp;
141 
142 	sp = spds;
143 	while (sp->sp_val && sp->sp_val != speed)
144 		sp++;
145 	return (sp->sp_name);
146 }
147