xref: /csrg-svn/usr.bin/uucp/uucico/gnsys.c (revision 13654)
1*13654Ssam #ifndef lint
2*13654Ssam static char sccsid[] = "@(#)gnsys.c	5.1 (Berkeley) 07/02/83";
3*13654Ssam #endif
4*13654Ssam 
5*13654Ssam /*
6*13654Ssam  * Mods:
7*13654Ssam  * The "retry" code below prevents uucico from calling
8*13654Ssam  * a site which it has called earlier.
9*13654Ssam  * Also, uucico does callok() only once for each system.
10*13654Ssam  * Done by unc!smb
11*13654Ssam  */
12*13654Ssam 
13*13654Ssam #include "uucp.h"
14*13654Ssam #include <sys/types.h>
15*13654Ssam #ifdef	NDIR
16*13654Ssam #include "ndir.h"
17*13654Ssam #else
18*13654Ssam #include <dir.h>
19*13654Ssam #endif
20*13654Ssam 
21*13654Ssam 
22*13654Ssam #define LSIZE 100	/* number of systems to store */
23*13654Ssam #define WSUFSIZE 6	/* work file name suffix size */
24*13654Ssam 
25*13654Ssam /*******
26*13654Ssam  *	gnsys(sname, dir, pre)
27*13654Ssam  *	char *sname, *dir, pre;
28*13654Ssam  *
29*13654Ssam  *	gnsys  -  this routine will return the next
30*13654Ssam  *	system name which has work to be done.
31*13654Ssam  *	"pre" is the prefix for work files.
32*13654Ssam  *	"dir" is the directory to search.
33*13654Ssam  *	"sname" is a string of size DIRSIZ - WSUFSIZE.
34*13654Ssam  *
35*13654Ssam  *	return codes:
36*13654Ssam  *		0  -  no more names
37*13654Ssam  *		1  -  name returned in sname
38*13654Ssam  *		FAIL  -  bad directory
39*13654Ssam  */
40*13654Ssam 
41*13654Ssam gnsys(sname, dir, pre)
42*13654Ssam char *sname, *dir, pre;
43*13654Ssam {
44*13654Ssam 	register char *s, *p1, *p2;
45*13654Ssam 	char px[3];
46*13654Ssam 	static char *list[LSIZE];
47*13654Ssam 	static int nitem=0, n=0, base=0;
48*13654Ssam 	char systname[NAMESIZE], filename[NAMESIZE];
49*13654Ssam 	DIR *dirp;
50*13654Ssam 
51*13654Ssam retry:
52*13654Ssam 	px[0] = pre;
53*13654Ssam 	px[1] = '.';
54*13654Ssam 	px[2] = '\0';
55*13654Ssam 	if (nitem == base) {
56*13654Ssam 		/* get list of systems with work */
57*13654Ssam 		int i;
58*13654Ssam 		dirp = opendir(subdir(dir,pre), "r");
59*13654Ssam 		ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
60*13654Ssam 		for (i = base; i < LSIZE; i++)
61*13654Ssam 			list[i] = NULL;
62*13654Ssam 		while (gnamef(dirp, filename) != 0) {
63*13654Ssam 			if (!prefix(px, filename))
64*13654Ssam 				continue;
65*13654Ssam 			p2 = filename + strlen(filename)
66*13654Ssam 				- WSUFSIZE;
67*13654Ssam 			p1 = filename + strlen(px);
68*13654Ssam 			for(s = systname; p1 <= p2; p1++)
69*13654Ssam 				*s++ = *p1;
70*13654Ssam 			*s = '\0';
71*13654Ssam 			if (systname[0] == '\0')
72*13654Ssam 				continue;
73*13654Ssam 			nitem = srchst(systname, list, nitem);
74*13654Ssam 			if (LSIZE <= nitem) break;
75*13654Ssam 		}
76*13654Ssam 
77*13654Ssam 		closedir(dirp);
78*13654Ssam 	}
79*13654Ssam 
80*13654Ssam 	if (nitem == base) {
81*13654Ssam 		for (n = 0; n < nitem; n++)
82*13654Ssam 			if (list[n] != NULL)
83*13654Ssam 				free(list[n]);
84*13654Ssam 		return(0);
85*13654Ssam 	}
86*13654Ssam 	while(nitem > n) {
87*13654Ssam 		strcpy(sname, list[n++]);
88*13654Ssam 		if (callok(sname) == 0)
89*13654Ssam 			return(1);
90*13654Ssam 	}
91*13654Ssam 	base = n = nitem;
92*13654Ssam 	goto retry;
93*13654Ssam }
94*13654Ssam 
95*13654Ssam /***
96*13654Ssam  *	srchst(name, list, n)
97*13654Ssam  *	char *name, **list;
98*13654Ssam  *	int n;
99*13654Ssam  *
100*13654Ssam  *	srchst  -  this routine will do a linear search
101*13654Ssam  *	of list (list) to find name (name).
102*13654Ssam  *	If the name is not found, it is added to the
103*13654Ssam  *	list.
104*13654Ssam  *	The number of items in the list (n) is
105*13654Ssam  *	returned (incremented if a name is added).
106*13654Ssam  *
107*13654Ssam  *	return codes:
108*13654Ssam  *		n - the number of items in the list
109*13654Ssam  */
110*13654Ssam 
111*13654Ssam srchst(name, list, n)
112*13654Ssam char *name;
113*13654Ssam register char **list;
114*13654Ssam int n;
115*13654Ssam {
116*13654Ssam 	register int i;
117*13654Ssam 	register char *p;
118*13654Ssam 
119*13654Ssam 	for (i = 0; i < n; i++)
120*13654Ssam 		if (strcmp(name, list[i]) == 0)
121*13654Ssam 			break;
122*13654Ssam 	if (i >= n) {
123*13654Ssam 		if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char)))
124*13654Ssam 			== NULL)
125*13654Ssam 			return(n);
126*13654Ssam 		strcpy(p, name);
127*13654Ssam 		list[n++] = p;
128*13654Ssam 	}
129*13654Ssam 	return(n);
130*13654Ssam }
131