1 /*	$OpenBSD: cu.c,v 1.16 2004/11/07 09:48:08 otto Exp $	*/
2 /*	$NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)cu.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 static const char rcsid[] = "$OpenBSD: cu.c,v 1.16 2004/11/07 09:48:08 otto Exp $";
38 #endif /* not lint */
39 
40 #include "tip.h"
41 
42 void	cleanup();
43 void	cuusage();
44 
45 /*
46  * Botch the interface to look like cu's
47  */
48 void
cumain(argc,argv)49 cumain(argc, argv)
50 	int argc;
51 	char *argv[];
52 {
53 	int ch, i, parity;
54 	long l;
55 	char *cp;
56 	static char sbuf[12];
57 
58 	if (argc < 2)
59 		cuusage();
60 	CU = DV = NOSTR;
61 	BR = DEFBR;
62 	parity = 0;	/* none */
63 	while ((ch = getopt(argc, argv, "a:l:s:htoe0123456789")) != -1) {
64 		switch(ch) {
65 		case 'a':
66 			CU = optarg;
67 			break;
68 		case 'l':
69 			if (DV != NULL) {
70 				fprintf(stderr,
71 				    "%s: cannot specificy multiple -l options\n",
72 				    __progname);
73 				exit(3);
74 			}
75 			if (strchr(optarg, '/'))
76 				DV = optarg;
77 			else
78 				asprintf(&DV, "/dev/%s", optarg);
79 			break;
80 		case 's':
81 			l = strtol(optarg, &cp, 10);
82 			if (*cp != '\0' || l < 0 || l >= INT_MAX) {
83 				fprintf(stderr, "%s: unsupported speed %s\n",
84 				    __progname, optarg);
85 				exit(3);
86 			}
87 			BR = (int)l;
88 			break;
89 		case 'h':
90 			setboolean(value(LECHO), TRUE);
91 			HD = TRUE;
92 			break;
93 		case 't':
94 			HW = 1, DU = -1;
95 			break;
96 		case 'o':
97 			if (parity != 0)
98 				parity = 0;	/* -e -o */
99 			else
100 				parity = 1;	/* odd */
101 			break;
102 		case 'e':
103 			if (parity != 0)
104 				parity = 0;	/* -o -e */
105 			else
106 				parity = -1;	/* even */
107 			break;
108 		case '0': case '1': case '2': case '3': case '4':
109 		case '5': case '6': case '7': case '8': case '9':
110 			if (CU)
111 				CU[strlen(CU)-1] = ch;
112 			if (DV)
113 				DV[strlen(DV)-1] = ch;
114 			break;
115 		default:
116 			cuusage();
117 			break;
118 		}
119 	}
120 	argc -= optind;
121 	argv += optind;
122 
123 	switch (argc) {
124 	case 1:
125 		PN = argv[0];
126 		break;
127 	case 0:
128 		break;
129 	default:
130 		cuusage();
131 		break;
132 	}
133 
134 	signal(SIGINT, cleanup);
135 	signal(SIGQUIT, cleanup);
136 	signal(SIGHUP, cleanup);
137 	signal(SIGTERM, cleanup);
138 	signal(SIGCHLD, SIG_DFL);
139 
140 	/*
141 	 * The "cu" host name is used to define the
142 	 * attributes of the generic dialer.
143 	 */
144 	(void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
145 	if ((i = hunt(sbuf)) == 0) {
146 		printf("all ports busy\n");
147 		exit(3);
148 	}
149 	if (i == -1) {
150 		printf("link down\n");
151 		(void)uu_unlock(uucplock);
152 		exit(3);
153 	}
154 	setbuf(stdout, NULL);
155 	loginit();
156 	user_uid();
157 	vinit();
158 	switch (parity) {
159 	case -1:
160 		setparity("even");
161 		break;
162 	case 1:
163 		setparity("odd");
164 		break;
165 	default:
166 		setparity("none");
167 		break;
168 	}
169 	setboolean(value(VERBOSE), FALSE);
170 	if (HW && ttysetup(BR)) {
171 		fprintf(stderr, "%s: unsupported speed %ld\n",
172 		    __progname, BR);
173 		daemon_uid();
174 		(void)uu_unlock(uucplock);
175 		exit(3);
176 	}
177 	if (connect()) {
178 		printf("Connect failed\n");
179 		daemon_uid();
180 		(void)uu_unlock(uucplock);
181 		exit(1);
182 	}
183 	if (!HW && ttysetup(BR)) {
184 		fprintf(stderr, "%s: unsupported speed %ld\n",
185 		    __progname, BR);
186 		daemon_uid();
187 		(void)uu_unlock(uucplock);
188 		exit(3);
189 	}
190 }
191 
192 void
cuusage()193 cuusage()
194 {
195 	fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] [-s speed] [-#] "
196 	    "[phone-number]\n");
197 	exit(8);
198 }
199