xref: /original-bsd/usr.bin/tip/hunt.c (revision 6cca134b)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)hunt.c	5.4 (Berkeley) 09/02/88";
20 #endif /* not lint */
21 
22 #include "tip.h"
23 
24 extern char *getremote();
25 extern char *rindex();
26 
27 static	jmp_buf deadline;
28 static	int deadfl;
29 
30 dead()
31 {
32 
33 	deadfl = 1;
34 	longjmp(deadline, 1);
35 }
36 
37 hunt(name)
38 	char *name;
39 {
40 	register char *cp;
41 	int (*f)();
42 
43 	f = signal(SIGALRM, dead);
44 	while (cp = getremote(name)) {
45 		deadfl = 0;
46 		uucplock = rindex(cp, '/')+1;
47 		if (uu_lock(uucplock) < 0)
48 			continue;
49 		/*
50 		 * Straight through call units, such as the BIZCOMP,
51 		 * VADIC and the DF, must indicate they're hardwired in
52 		 *  order to get an open file descriptor placed in FD.
53 		 * Otherwise, as for a DN-11, the open will have to
54 		 *  be done in the "open" routine.
55 		 */
56 		if (!HW)
57 			break;
58 		if (setjmp(deadline) == 0) {
59 			alarm(10);
60 			FD = open(cp, O_RDWR);
61 		}
62 		alarm(0);
63 		if (FD < 0) {
64 			perror(cp);
65 			deadfl = 1;
66 		}
67 		if (!deadfl) {
68 			ioctl(FD, TIOCEXCL, 0);
69 			ioctl(FD, TIOCHPCL, 0);
70 			signal(SIGALRM, SIG_DFL);
71 			return ((int)cp);
72 		}
73 		(void)uu_unlock(uucplock);
74 	}
75 	signal(SIGALRM, f);
76 	return (deadfl ? -1 : (int)cp);
77 }
78