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