xref: /csrg-svn/usr.bin/uucp/uucico/gnsys.c (revision 33568)
113654Ssam #ifndef lint
2*33568Srick static char sccsid[] = "@(#)gnsys.c	5.5	02/24/88";
313654Ssam #endif
413654Ssam 
513654Ssam #include "uucp.h"
613654Ssam #ifdef	NDIR
713654Ssam #include "ndir.h"
813654Ssam #else
913702Ssam #include <sys/dir.h>
1013654Ssam #endif
1113654Ssam 
1223608Sbloom #define LSIZE 128	/* number of systems to store */
1313654Ssam #define WSUFSIZE 6	/* work file name suffix size */
1413654Ssam 
1523608Sbloom /*LINTLIBRARY*/
1623608Sbloom 
1717835Sralph /*
1817835Sralph  *	this routine will return the next system name which has work to be done.
1917835Sralph  *	"sname" is a string of size DIRSIZ - WSUFSIZE.
2013654Ssam  *	"pre" is the prefix for work files.
2113654Ssam  *	"dir" is the directory to search.
2213654Ssam  *
2313654Ssam  *	return codes:
2413654Ssam  *		1  -  name returned in sname
2523608Sbloom  *		SUCCESS  -  no more names
2613654Ssam  *		FAIL  -  bad directory
2713654Ssam  */
2813654Ssam gnsys(sname, dir, pre)
2913654Ssam char *sname, *dir, pre;
3013654Ssam {
31*33568Srick 	register DIR *dirp;
32*33568Srick 	register struct direct *dentp;
3313654Ssam 	static char *list[LSIZE];
34*33568Srick 	static int nitem = 0, n = 0, base = 0;
35*33568Srick 	char systname[NAMESIZE];
3613654Ssam 
3713654Ssam retry:
3813654Ssam 	if (nitem == base) {
3913654Ssam 		/* get list of systems with work */
4013654Ssam 		int i;
4117835Sralph 		dirp = opendir(subdir(dir,pre));
4213654Ssam 		ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
4313654Ssam 		for (i = base; i < LSIZE; i++)
4413654Ssam 			list[i] = NULL;
45*33568Srick 		while (dentp = readdir(dirp)) {
46*33568Srick 			register char *s, *p1, *p2;
47*33568Srick 			if (dentp->d_name[0] != pre || dentp->d_name[1] != '.')
4813654Ssam 				continue;
49*33568Srick 			p2 = dentp->d_name + dentp->d_namlen - WSUFSIZE;
50*33568Srick 			p1 = dentp->d_name + 2;
5113654Ssam 			for(s = systname; p1 <= p2; p1++)
5213654Ssam 				*s++ = *p1;
5313654Ssam 			*s = '\0';
5413654Ssam 			if (systname[0] == '\0')
5513654Ssam 				continue;
5613654Ssam 			nitem = srchst(systname, list, nitem);
57*33568Srick 			if (LSIZE <= nitem)
58*33568Srick 				break;
5913654Ssam 		}
6013654Ssam 		closedir(dirp);
6113654Ssam 	}
6213654Ssam 
6313654Ssam 	if (nitem == base) {
6413654Ssam 		for (n = 0; n < nitem; n++)
6513654Ssam 			if (list[n] != NULL)
6613654Ssam 				free(list[n]);
6723608Sbloom 		return SUCCESS;
6813654Ssam 	}
69*33568Srick 	while (nitem > n) {
7023608Sbloom 		/* We only have at most a SYSNSIZE character site name encoded
7123608Sbloom 		 * in the file. However, we would like to use the full sitename
7223608Sbloom 		 * if possible. If the number of chars in list[n] is < SYSNSIZE
7323608Sbloom 		 * then the sitename could not have been truncated and
7423608Sbloom 		 * we don't bother to check. Otherwise, we scan SYSFILE
7523608Sbloom 		 * looking for the fullname and return it if we find it
7623608Sbloom 		 */
7713654Ssam 		strcpy(sname, list[n++]);
7823608Sbloom 		if (strlen(sname) >= SYSNSIZE) {
7923608Sbloom 			register FILE *fp;
8023608Sbloom 			register char *p;
8123608Sbloom 			char line[MAXFULLNAME];
8223608Sbloom 			fp = fopen(SYSFILE, "r");
8323608Sbloom 			ASSERT(fp != NULL, CANTOPEN, SYSFILE, 0);
8423608Sbloom 			while (cfgets(line, sizeof(line), fp) != NULL) {
8523608Sbloom 				p = index(line, ' ');
8623608Sbloom 				if (p)
8723608Sbloom 					*p = '\0';
8823608Sbloom 				if (strncmp(sname, line, SYSNSIZE) == SAME) {
8923608Sbloom 					strncpy(sname, line, MAXBASENAME);
9023608Sbloom 					break;
9123608Sbloom 				}
9223608Sbloom 			}
9323608Sbloom 			fclose(fp);
9423608Sbloom 		}
9513654Ssam 		if (callok(sname) == 0)
9617835Sralph 			return 1;
9713654Ssam 	}
9813654Ssam 	base = n = nitem;
9913654Ssam 	goto retry;
10013654Ssam }
10113654Ssam 
10217835Sralph /*
10317835Sralph  *	this routine will do a linear search of list (list) to find name (name).
10417835Sralph  *	If the name is not found, it is added to the list.
10517835Sralph  *	The number of items in the list (n) is returned (incremented if a
10617835Sralph  *	name is added).
10713654Ssam  *
10813654Ssam  *	return codes:
10913654Ssam  *		n - the number of items in the list
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)
12517835Sralph 			return n;
12613654Ssam 		strcpy(p, name);
12713654Ssam 		list[n++] = p;
12813654Ssam 	}
12917835Sralph 	return n;
13013654Ssam }
131