Annotation of src/usr.bin/tip/cu.c, Revision 1.21
1.21 ! ray 1: /* $OpenBSD: cu.c,v 1.20 2006/11/06 19:37:21 millert Exp $ */
1.3 millert 2: /* $NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $ */
1.1 deraadt 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.
1.14 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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
1.21 ! ray 37: static const char rcsid[] = "$OpenBSD: cu.c,v 1.20 2006/11/06 19:37:21 millert Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
1.21 ! ray 40: #include <err.h>
! 41: #include <paths.h>
! 42:
1.1 deraadt 43: #include "tip.h"
44:
1.18 moritz 45: static void cuusage(void);
1.1 deraadt 46:
47: /*
48: * Botch the interface to look like cu's
49: */
1.4 deraadt 50: void
1.17 deraadt 51: cumain(int argc, char *argv[])
1.1 deraadt 52: {
1.11 miod 53: int ch, i, parity;
1.20 millert 54: const char *errstr;
1.1 deraadt 55: static char sbuf[12];
56:
1.6 millert 57: if (argc < 2)
58: cuusage();
1.1 deraadt 59: CU = DV = NOSTR;
60: BR = DEFBR;
1.11 miod 61: parity = 0; /* none */
1.20 millert 62:
63: /*
64: * Convert obsolecent -### speed to modern -s### syntax which
65: * getopt() can handle.
66: */
67: for (i = 1; i < argc; i++) {
68: if (argv[i][0] == '-') {
69: switch (argv[i][1]) {
70: case '0': case '1': case '2': case '3': case '4':
71: case '5': case '6': case '7': case '8': case '9':
72: ch = snprintf(sbuf, sizeof(sbuf), "-s%s",
73: &argv[i][1]);
74: if (ch <= 0 || ch >= sizeof(sbuf)) {
75: errx(3, "invalid speed: %s",
76: &argv[i][1]);
77: }
78: argv[i] = sbuf;
79: break;
80: case '-':
81: /* if we get "--" stop processing args */
82: if (argv[i][2] == '\0')
83: goto getopt;
84: break;
85: }
86: }
87: }
88:
89: getopt:
90: while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
1.17 deraadt 91: switch (ch) {
1.1 deraadt 92: case 'a':
1.21 ! ray 93: if (optarg[0] == '\0')
! 94: errx(3, "invalid acu: \"\"");
1.6 millert 95: CU = optarg;
96: break;
97: case 'l':
1.8 deraadt 98: if (DV != NULL) {
99: fprintf(stderr,
100: "%s: cannot specificy multiple -l options\n",
101: __progname);
102: exit(3);
103: }
104: if (strchr(optarg, '/'))
105: DV = optarg;
106: else
1.21 ! ray 107: if (asprintf(&DV, "%s%s", _PATH_DEV, optarg) == -1)
! 108: err(3, "asprintf");
1.1 deraadt 109: break;
110: case 's':
1.20 millert 111: BR = (int)strtonum(optarg, 0, INT_MAX, &errstr);
112: if (errstr)
113: errx(3, "speed is %s: %s", errstr, optarg);
1.6 millert 114: break;
115: case 'h':
1.9 millert 116: setboolean(value(LECHO), TRUE);
1.6 millert 117: HD = TRUE;
118: break;
119: case 't':
120: HW = 1, DU = -1;
1.1 deraadt 121: break;
1.6 millert 122: case 'o':
1.11 miod 123: if (parity != 0)
124: parity = 0; /* -e -o */
125: else
126: parity = 1; /* odd */
1.6 millert 127: break;
128: case 'e':
1.11 miod 129: if (parity != 0)
130: parity = 0; /* -o -e */
131: else
132: parity = -1; /* even */
1.1 deraadt 133: break;
134: default:
1.10 pvalchev 135: cuusage();
1.1 deraadt 136: break;
137: }
138: }
1.6 millert 139: argc -= optind;
140: argv += optind;
141:
142: switch (argc) {
143: case 1:
144: PN = argv[0];
145: break;
146: case 0:
147: break;
148: default:
149: cuusage();
150: break;
151: }
152:
1.1 deraadt 153: signal(SIGINT, cleanup);
154: signal(SIGQUIT, cleanup);
155: signal(SIGHUP, cleanup);
156: signal(SIGTERM, cleanup);
1.16 otto 157: signal(SIGCHLD, SIG_DFL);
1.1 deraadt 158:
159: /*
160: * The "cu" host name is used to define the
161: * attributes of the generic dialer.
162: */
1.4 deraadt 163: (void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
1.1 deraadt 164: if ((i = hunt(sbuf)) == 0) {
165: printf("all ports busy\n");
166: exit(3);
167: }
168: if (i == -1) {
169: printf("link down\n");
170: (void)uu_unlock(uucplock);
171: exit(3);
172: }
173: setbuf(stdout, NULL);
174: loginit();
175: user_uid();
176: vinit();
1.11 miod 177: switch (parity) {
178: case -1:
179: setparity("even");
180: break;
181: case 1:
182: setparity("odd");
183: break;
184: default:
185: setparity("none");
186: break;
187: }
1.9 millert 188: setboolean(value(VERBOSE), FALSE);
1.13 millert 189: if (HW && ttysetup(BR)) {
190: fprintf(stderr, "%s: unsupported speed %ld\n",
191: __progname, BR);
192: daemon_uid();
193: (void)uu_unlock(uucplock);
194: exit(3);
195: }
1.17 deraadt 196: if (con()) {
1.1 deraadt 197: printf("Connect failed\n");
198: daemon_uid();
199: (void)uu_unlock(uucplock);
200: exit(1);
201: }
1.13 millert 202: if (!HW && ttysetup(BR)) {
203: fprintf(stderr, "%s: unsupported speed %ld\n",
204: __progname, BR);
205: daemon_uid();
206: (void)uu_unlock(uucplock);
207: exit(3);
208: }
1.6 millert 209: }
210:
1.18 moritz 211: static void
1.17 deraadt 212: cuusage(void)
1.6 millert 213: {
1.19 jmc 214: fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
215: "[-s speed | -speed] [phone-number]\n");
1.6 millert 216: exit(8);
1.1 deraadt 217: }