Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / sbin / slattach / slattach.c
blobba9d461ad8037e8f4db2277da103428ad78df2ed
1 /* $NetBSD: slattach.c,v 1.29 2005/02/05 12:38:58 xtraeme Exp $ */
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Adams.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)slattach.c 8.2 (Berkeley) 1/7/94";
44 #else
45 __RCSID("$NetBSD: slattach.c,v 1.29 2005/02/05 12:38:58 xtraeme Exp $");
46 #endif
47 #endif /* not lint */
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
54 #include <net/if.h>
55 #include <netinet/in.h>
57 #include <err.h>
58 #include <fcntl.h>
59 #include <netdb.h>
60 #include <paths.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <termios.h>
66 #include <unistd.h>
68 int speed = 9600;
69 int slipdisc = SLIPDISC;
71 char devicename[32];
73 int ttydisc(char *);
74 void usage(void);
76 int
77 main(int argc, char *argv[])
79 int fd;
80 char *dev = argv[1];
81 struct termios tty;
82 tcflag_t cflag = HUPCL;
83 int ch;
84 sigset_t nsigset;
85 int opt_detach = 1;
87 while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
88 switch (ch) {
89 case 'h':
90 cflag |= CRTSCTS;
91 break;
92 case 'H':
93 cflag |= CDTRCTS;
94 break;
95 case 'l':
96 cflag |= CLOCAL;
97 break;
98 case 'm':
99 cflag &= ~HUPCL;
100 break;
101 case 'n':
102 opt_detach = 0;
103 break;
104 case 's':
105 speed = atoi(optarg);
106 break;
107 case 'r': case 't':
108 slipdisc = ttydisc(optarg);
109 break;
110 case '?':
111 default:
112 usage();
115 argc -= optind;
116 argv += optind;
118 if (argc != 1)
119 usage();
121 dev = *argv;
122 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
123 (void)snprintf(devicename, sizeof(devicename),
124 "%s%s", _PATH_DEV, dev);
125 dev = devicename;
127 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
128 err(1, "%s", dev);
129 tty.c_cflag = CREAD | CS8 | cflag;
130 tty.c_iflag = 0;
131 tty.c_lflag = 0;
132 tty.c_oflag = 0;
133 tty.c_cc[VMIN] = 1;
134 tty.c_cc[VTIME] = 0;
135 cfsetspeed(&tty, speed);
136 if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
137 err(1, "tcsetattr");
138 if (ioctl(fd, TIOCSDTR, 0) < 0)
139 err(1, "TIOCSDTR");
140 if (ioctl(fd, TIOCSETD, &slipdisc) < 0)
141 err(1, "TIOCSETD");
142 if (opt_detach && daemon(0, 0) != 0)
143 err(1, "couldn't detach");
144 sigemptyset(&nsigset);
145 for (;;)
146 sigsuspend(&nsigset);
150 ttydisc(char *name)
152 if (strcmp(name, "slip") == 0)
153 return(SLIPDISC);
154 #ifdef STRIPDISC
155 else if (strcmp(name, "strip") == 0)
156 return(STRIPDISC);
157 #endif
158 else
159 usage();
160 /* NOTREACHED */
161 return -1;
164 void
165 usage(void)
168 (void)fprintf(stderr,
169 "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n",
170 getprogname());
171 exit(1);