xref: /csrg-svn/usr.bin/uucp/uucico/gnsys.c (revision 17835)
113654Ssam #ifndef lint
2*17835Sralph static char sccsid[] = "@(#)gnsys.c	5.3 (Berkeley) 01/22/85";
313654Ssam #endif
413654Ssam 
513654Ssam #include "uucp.h"
613654Ssam #include <sys/types.h>
713654Ssam #ifdef	NDIR
813654Ssam #include "ndir.h"
913654Ssam #else
1013702Ssam #include <sys/dir.h>
1113654Ssam #endif
1213654Ssam 
1313654Ssam #define LSIZE 100	/* number of systems to store */
1413654Ssam #define WSUFSIZE 6	/* work file name suffix size */
1513654Ssam 
16*17835Sralph /*
17*17835Sralph  *	this routine will return the next system name which has work to be done.
18*17835Sralph  *	"sname" is a string of size DIRSIZ - WSUFSIZE.
1913654Ssam  *	"pre" is the prefix for work files.
2013654Ssam  *	"dir" is the directory to search.
2113654Ssam  *
2213654Ssam  *	return codes:
2313654Ssam  *		0  -  no more names
2413654Ssam  *		1  -  name returned in sname
2513654Ssam  *		FAIL  -  bad directory
2613654Ssam  */
2713654Ssam 
2813654Ssam gnsys(sname, dir, pre)
2913654Ssam char *sname, *dir, pre;
3013654Ssam {
3113654Ssam 	register char *s, *p1, *p2;
3213654Ssam 	char px[3];
3313654Ssam 	static char *list[LSIZE];
3413654Ssam 	static int nitem=0, n=0, base=0;
3513654Ssam 	char systname[NAMESIZE], filename[NAMESIZE];
3613654Ssam 	DIR *dirp;
3713654Ssam 
3813654Ssam retry:
3913654Ssam 	px[0] = pre;
4013654Ssam 	px[1] = '.';
4113654Ssam 	px[2] = '\0';
4213654Ssam 	if (nitem == base) {
4313654Ssam 		/* get list of systems with work */
4413654Ssam 		int i;
45*17835Sralph 		dirp = opendir(subdir(dir,pre));
4613654Ssam 		ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
4713654Ssam 		for (i = base; i < LSIZE; i++)
4813654Ssam 			list[i] = NULL;
4913654Ssam 		while (gnamef(dirp, filename) != 0) {
5013654Ssam 			if (!prefix(px, filename))
5113654Ssam 				continue;
5213654Ssam 			p2 = filename + strlen(filename)
5313654Ssam 				- WSUFSIZE;
5413654Ssam 			p1 = filename + strlen(px);
5513654Ssam 			for(s = systname; p1 <= p2; p1++)
5613654Ssam 				*s++ = *p1;
5713654Ssam 			*s = '\0';
5813654Ssam 			if (systname[0] == '\0')
5913654Ssam 				continue;
6013654Ssam 			nitem = srchst(systname, list, nitem);
6113654Ssam 			if (LSIZE <= nitem) break;
6213654Ssam 		}
6313654Ssam 
6413654Ssam 		closedir(dirp);
6513654Ssam 	}
6613654Ssam 
6713654Ssam 	if (nitem == base) {
6813654Ssam 		for (n = 0; n < nitem; n++)
6913654Ssam 			if (list[n] != NULL)
7013654Ssam 				free(list[n]);
71*17835Sralph 		return 0;
7213654Ssam 	}
7313654Ssam 	while(nitem > n) {
7413654Ssam 		strcpy(sname, list[n++]);
7513654Ssam 		if (callok(sname) == 0)
76*17835Sralph 			return 1;
7713654Ssam 	}
7813654Ssam 	base = n = nitem;
7913654Ssam 	goto retry;
8013654Ssam }
8113654Ssam 
82*17835Sralph /*
83*17835Sralph  *	this routine will do a linear search of list (list) to find name (name).
84*17835Sralph  *	If the name is not found, it is added to the list.
85*17835Sralph  *	The number of items in the list (n) is returned (incremented if a
86*17835Sralph  *	name is added).
8713654Ssam  *
8813654Ssam  *	return codes:
8913654Ssam  *		n - the number of items in the list
9013654Ssam  */
9113654Ssam 
9213654Ssam srchst(name, list, n)
9313654Ssam char *name;
9413654Ssam register char **list;
9513654Ssam int n;
9613654Ssam {
9713654Ssam 	register int i;
9813654Ssam 	register char *p;
9913654Ssam 
10013654Ssam 	for (i = 0; i < n; i++)
10113654Ssam 		if (strcmp(name, list[i]) == 0)
10213654Ssam 			break;
10313654Ssam 	if (i >= n) {
10413654Ssam 		if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char)))
10513654Ssam 			== NULL)
106*17835Sralph 			return n;
10713654Ssam 		strcpy(p, name);
10813654Ssam 		list[n++] = p;
10913654Ssam 	}
110*17835Sralph 	return n;
11113654Ssam }
112