cu.c revision 88276
1/*	$OpenBSD: cu.c,v 1.10 2001/09/26 06:07:28 pvalchev 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. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/usr.bin/tip/tip/cu.c 88276 2001-12-20 14:25:46Z markm $");
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)cu.c	8.1 (Berkeley) 6/6/93";
43static char rcsid[] = "$OpenBSD: cu.c,v 1.10 2001/09/26 06:07:28 pvalchev Exp $";
44#endif
45#endif /* not lint */
46
47#include "tip.h"
48
49void	cleanup();
50void	cuusage();
51
52/*
53 * Botch the interface to look like cu's
54 */
55void
56cumain(argc, argv)
57	int argc;
58	char *argv[];
59{
60	int ch, i;
61	long l;
62	char *cp;
63	static char sbuf[12];
64
65	if (argc < 2)
66		cuusage();
67	CU = DV = NOSTR;
68	BR = DEFBR;
69	while ((ch = getopt(argc, argv, "a:l:s:htoe0123456789")) != -1) {
70		switch(ch) {
71		case 'a':
72			CU = optarg;
73			break;
74		case 'l':
75			if (DV != NULL) {
76				fprintf(stderr,
77				    "%s: cannot specificy multiple -l options\n",
78				    __progname);
79				exit(3);
80			}
81			if (strchr(optarg, '/'))
82				DV = optarg;
83			else
84				asprintf(&DV, "/dev/%s", optarg);
85			break;
86		case 's':
87			l = strtol(optarg, &cp, 10);
88			if (*cp != '\0' || l < 0 || l >= INT_MAX ||
89			    speed((int)l) == 0) {
90				fprintf(stderr, "%s: unsupported speed %s\n",
91				    __progname, optarg);
92				exit(3);
93			}
94			BR = (int)l;
95			break;
96		case 'h':
97			setboolean(value(LECHO), TRUE);
98			HD = TRUE;
99			break;
100		case 't':
101			HW = 1, DU = -1;
102			break;
103		case 'o':
104			setparity("odd");
105			break;
106		case 'e':
107			setparity("even");
108			break;
109		case '0': case '1': case '2': case '3': case '4':
110		case '5': case '6': case '7': case '8': case '9':
111			if (CU)
112				CU[strlen(CU)-1] = ch;
113			if (DV)
114				DV[strlen(DV)-1] = ch;
115			break;
116		default:
117			cuusage();
118			break;
119		}
120	}
121	argc -= optind;
122	argv += optind;
123
124	switch (argc) {
125	case 1:
126		PN = argv[0];
127		break;
128	case 0:
129		break;
130	default:
131		cuusage();
132		break;
133	}
134
135	signal(SIGINT, cleanup);
136	signal(SIGQUIT, cleanup);
137	signal(SIGHUP, cleanup);
138	signal(SIGTERM, cleanup);
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	setparity("none");
159	setboolean(value(VERBOSE), FALSE);
160	if (HW)
161		ttysetup(speed(BR));
162	if (connect()) {
163		printf("Connect failed\n");
164		daemon_uid();
165		(void)uu_unlock(uucplock);
166		exit(1);
167	}
168	if (!HW)
169		ttysetup(speed(BR));
170}
171
172void
173cuusage()
174{
175	fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] [-s speed] [-#] "
176	    "[phone-number]\n");
177	exit(8);
178}
179