150276Speter/*	$OpenBSD: hunt.c,v 1.13 2006/03/17 19:39:46 deraadt Exp $	*/
2166124Srafan/*	$NetBSD: hunt.c,v 1.6 1997/04/20 00:02:10 mellon Exp $	*/
350276Speter
450276Speter/*
550276Speter * Copyright (c) 1983, 1993
650276Speter *	The Regents of the University of California.  All rights reserved.
750276Speter *
850276Speter * Redistribution and use in source and binary forms, with or without
950276Speter * modification, are permitted provided that the following conditions
1050276Speter * are met:
1150276Speter * 1. Redistributions of source code must retain the above copyright
1250276Speter *    notice, this list of conditions and the following disclaimer.
1350276Speter * 2. Redistributions in binary form must reproduce the above copyright
1450276Speter *    notice, this list of conditions and the following disclaimer in the
1550276Speter *    documentation and/or other materials provided with the distribution.
1650276Speter * 3. Neither the name of the University nor the names of its contributors
1750276Speter *    may be used to endorse or promote products derived from this software
1850276Speter *    without specific prior written permission.
1950276Speter *
2050276Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2150276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2250276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2350276Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2450276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2550276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2650276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2750276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2850276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2950276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30166124Srafan * SUCH DAMAGE.
3150276Speter */
3250276Speter
33166124Srafan#include <sys/cdefs.h>
34166124Srafan__FBSDID("$FreeBSD: releng/10.3/usr.bin/tip/tip/hunt.c 161754 2006-08-31 14:14:30Z ru $");
3550276Speter
3650276Speter#ifndef lint
3750276Speter#if 0
3850276Speterstatic char sccsid[] = "@(#)hunt.c	8.1 (Berkeley) 6/6/93";
3950276Speterstatic const char rcsid[] = "$OpenBSD: hunt.c,v 1.13 2006/03/17 19:39:46 deraadt Exp $";
4050276Speter#endif
4150276Speter#endif /* not lint */
4250276Speter
4350276Speter#include "tip.h"
4450276Speter
45166124Srafanstatic	jmp_buf deadline;
46166124Srafanstatic	int deadfl;
47166124Srafan
48166124Srafanstatic void	dead(int);
49166124Srafan
50166124Srafan/*ARGSUSED*/
51166124Srafanstatic void
52166124Srafandead(int signo)
5350276Speter{
5450276Speter	deadfl = 1;
5550276Speter	longjmp(deadline, 1);
5650276Speter}
5750276Speter
5850276Speterlong
5950276Speterhunt(char *name)
6050276Speter{
61166124Srafan	char *cp;
62166124Srafan	sig_t f;
63166124Srafan
64166124Srafan	f = signal(SIGALRM, dead);
6550276Speter	while ((cp = getremote(name))) {
6650276Speter		deadfl = 0;
6750276Speter		uucplock = strrchr(cp, '/');
6850276Speter		if (uucplock == NULL)
6950276Speter			uucplock = cp;
7050276Speter		else
7150276Speter			uucplock++;
72166124Srafan
73166124Srafan		if (uu_lock(uucplock) < 0)
74166124Srafan			continue;
75166124Srafan		/*
76166124Srafan		 * Straight through call units, such as the BIZCOMP,
77166124Srafan		 * VADIC and the DF, must indicate they're hardwired in
78166124Srafan		 *  order to get an open file descriptor placed in FD.
79166124Srafan		 * Otherwise, as for a DN-11, the open will have to
80166124Srafan		 *  be done in the "open" routine.
81166124Srafan		 */
82166124Srafan		if (!HW)
83166124Srafan			break;
84166124Srafan		if (setjmp(deadline) == 0) {
85166124Srafan			alarm(10);
86166124Srafan			FD = open(cp, (O_RDWR |
87166124Srafan			    (boolean(value(DC)) ? O_NONBLOCK : 0)));
88166124Srafan		}
89166124Srafan		alarm(0);
90166124Srafan		if (FD < 0) {
91166124Srafan			perror(cp);
92166124Srafan			deadfl = 1;
93166124Srafan		}
94166124Srafan		if (!deadfl) {
95166124Srafan			struct termios cntrl;
96166124Srafan
97166124Srafan			tcgetattr(FD, &cntrl);
98166124Srafan			if (!boolean(value(DC)))
99166124Srafan				cntrl.c_cflag |= HUPCL;
100166124Srafan			tcsetattr(FD, TCSAFLUSH, &cntrl);
101166124Srafan			ioctl(FD, TIOCEXCL, 0);
102166124Srafan			signal(SIGALRM, SIG_DFL);
103166124Srafan			return ((long)cp);
104166124Srafan		}
105166124Srafan		(void)uu_unlock(uucplock);
10650276Speter	}
10750276Speter	signal(SIGALRM, f);
10850276Speter	return (deadfl ? -1 : (long)cp);
10950276Speter}
11050276Speter