xref: /csrg-svn/usr.bin/uucp/uucico/gnsys.c (revision 13702)
113654Ssam #ifndef lint
2*13702Ssam static char sccsid[] = "@(#)gnsys.c	5.2 (Berkeley) 07/02/83";
313654Ssam #endif
413654Ssam 
513654Ssam /*
613654Ssam  * Mods:
713654Ssam  * The "retry" code below prevents uucico from calling
813654Ssam  * a site which it has called earlier.
913654Ssam  * Also, uucico does callok() only once for each system.
1013654Ssam  * Done by unc!smb
1113654Ssam  */
1213654Ssam 
1313654Ssam #include "uucp.h"
1413654Ssam #include <sys/types.h>
1513654Ssam #ifdef	NDIR
1613654Ssam #include "ndir.h"
1713654Ssam #else
18*13702Ssam #include <sys/dir.h>
1913654Ssam #endif
2013654Ssam 
2113654Ssam 
2213654Ssam #define LSIZE 100	/* number of systems to store */
2313654Ssam #define WSUFSIZE 6	/* work file name suffix size */
2413654Ssam 
2513654Ssam /*******
2613654Ssam  *	gnsys(sname, dir, pre)
2713654Ssam  *	char *sname, *dir, pre;
2813654Ssam  *
2913654Ssam  *	gnsys  -  this routine will return the next
3013654Ssam  *	system name which has work to be done.
3113654Ssam  *	"pre" is the prefix for work files.
3213654Ssam  *	"dir" is the directory to search.
3313654Ssam  *	"sname" is a string of size DIRSIZ - WSUFSIZE.
3413654Ssam  *
3513654Ssam  *	return codes:
3613654Ssam  *		0  -  no more names
3713654Ssam  *		1  -  name returned in sname
3813654Ssam  *		FAIL  -  bad directory
3913654Ssam  */
4013654Ssam 
4113654Ssam gnsys(sname, dir, pre)
4213654Ssam char *sname, *dir, pre;
4313654Ssam {
4413654Ssam 	register char *s, *p1, *p2;
4513654Ssam 	char px[3];
4613654Ssam 	static char *list[LSIZE];
4713654Ssam 	static int nitem=0, n=0, base=0;
4813654Ssam 	char systname[NAMESIZE], filename[NAMESIZE];
4913654Ssam 	DIR *dirp;
5013654Ssam 
5113654Ssam retry:
5213654Ssam 	px[0] = pre;
5313654Ssam 	px[1] = '.';
5413654Ssam 	px[2] = '\0';
5513654Ssam 	if (nitem == base) {
5613654Ssam 		/* get list of systems with work */
5713654Ssam 		int i;
5813654Ssam 		dirp = opendir(subdir(dir,pre), "r");
5913654Ssam 		ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
6013654Ssam 		for (i = base; i < LSIZE; i++)
6113654Ssam 			list[i] = NULL;
6213654Ssam 		while (gnamef(dirp, filename) != 0) {
6313654Ssam 			if (!prefix(px, filename))
6413654Ssam 				continue;
6513654Ssam 			p2 = filename + strlen(filename)
6613654Ssam 				- WSUFSIZE;
6713654Ssam 			p1 = filename + strlen(px);
6813654Ssam 			for(s = systname; p1 <= p2; p1++)
6913654Ssam 				*s++ = *p1;
7013654Ssam 			*s = '\0';
7113654Ssam 			if (systname[0] == '\0')
7213654Ssam 				continue;
7313654Ssam 			nitem = srchst(systname, list, nitem);
7413654Ssam 			if (LSIZE <= nitem) break;
7513654Ssam 		}
7613654Ssam 
7713654Ssam 		closedir(dirp);
7813654Ssam 	}
7913654Ssam 
8013654Ssam 	if (nitem == base) {
8113654Ssam 		for (n = 0; n < nitem; n++)
8213654Ssam 			if (list[n] != NULL)
8313654Ssam 				free(list[n]);
8413654Ssam 		return(0);
8513654Ssam 	}
8613654Ssam 	while(nitem > n) {
8713654Ssam 		strcpy(sname, list[n++]);
8813654Ssam 		if (callok(sname) == 0)
8913654Ssam 			return(1);
9013654Ssam 	}
9113654Ssam 	base = n = nitem;
9213654Ssam 	goto retry;
9313654Ssam }
9413654Ssam 
9513654Ssam /***
9613654Ssam  *	srchst(name, list, n)
9713654Ssam  *	char *name, **list;
9813654Ssam  *	int n;
9913654Ssam  *
10013654Ssam  *	srchst  -  this routine will do a linear search
10113654Ssam  *	of list (list) to find name (name).
10213654Ssam  *	If the name is not found, it is added to the
10313654Ssam  *	list.
10413654Ssam  *	The number of items in the list (n) is
10513654Ssam  *	returned (incremented if a name is added).
10613654Ssam  *
10713654Ssam  *	return codes:
10813654Ssam  *		n - the number of items in the list
10913654Ssam  */
11013654Ssam 
11113654Ssam srchst(name, list, n)
11213654Ssam char *name;
11313654Ssam register char **list;
11413654Ssam int n;
11513654Ssam {
11613654Ssam 	register int i;
11713654Ssam 	register char *p;
11813654Ssam 
11913654Ssam 	for (i = 0; i < n; i++)
12013654Ssam 		if (strcmp(name, list[i]) == 0)
12113654Ssam 			break;
12213654Ssam 	if (i >= n) {
12313654Ssam 		if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char)))
12413654Ssam 			== NULL)
12513654Ssam 			return(n);
12613654Ssam 		strcpy(p, name);
12713654Ssam 		list[n++] = p;
12813654Ssam 	}
12913654Ssam 	return(n);
13013654Ssam }
131