xref: /csrg-svn/usr.bin/uucp/uucico/gnsys.c (revision 23608)
113654Ssam #ifndef lint
2*23608Sbloom static char sccsid[] = "@(#)gnsys.c	5.4 (Berkeley) 06/20/85";
313654Ssam #endif
413654Ssam 
513654Ssam #include "uucp.h"
613654Ssam #ifdef	NDIR
713654Ssam #include "ndir.h"
813654Ssam #else
913702Ssam #include <sys/dir.h>
1013654Ssam #endif
1113654Ssam 
12*23608Sbloom #define LSIZE 128	/* number of systems to store */
1313654Ssam #define WSUFSIZE 6	/* work file name suffix size */
1413654Ssam 
15*23608Sbloom /*LINTLIBRARY*/
16*23608Sbloom 
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
25*23608Sbloom  *		SUCCESS  -  no more names
2613654Ssam  *		FAIL  -  bad directory
2713654Ssam  */
2813654Ssam 
2913654Ssam gnsys(sname, dir, pre)
3013654Ssam char *sname, *dir, pre;
3113654Ssam {
3213654Ssam 	register char *s, *p1, *p2;
3313654Ssam 	char px[3];
3413654Ssam 	static char *list[LSIZE];
3513654Ssam 	static int nitem=0, n=0, base=0;
3613654Ssam 	char systname[NAMESIZE], filename[NAMESIZE];
3713654Ssam 	DIR *dirp;
3813654Ssam 
3913654Ssam retry:
4013654Ssam 	px[0] = pre;
4113654Ssam 	px[1] = '.';
4213654Ssam 	px[2] = '\0';
4313654Ssam 	if (nitem == base) {
4413654Ssam 		/* get list of systems with work */
4513654Ssam 		int i;
4617835Sralph 		dirp = opendir(subdir(dir,pre));
4713654Ssam 		ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
4813654Ssam 		for (i = base; i < LSIZE; i++)
4913654Ssam 			list[i] = NULL;
5013654Ssam 		while (gnamef(dirp, filename) != 0) {
5113654Ssam 			if (!prefix(px, filename))
5213654Ssam 				continue;
5313654Ssam 			p2 = filename + strlen(filename)
5413654Ssam 				- WSUFSIZE;
5513654Ssam 			p1 = filename + strlen(px);
5613654Ssam 			for(s = systname; p1 <= p2; p1++)
5713654Ssam 				*s++ = *p1;
5813654Ssam 			*s = '\0';
5913654Ssam 			if (systname[0] == '\0')
6013654Ssam 				continue;
6113654Ssam 			nitem = srchst(systname, list, nitem);
6213654Ssam 			if (LSIZE <= nitem) break;
6313654Ssam 		}
6413654Ssam 
6513654Ssam 		closedir(dirp);
6613654Ssam 	}
6713654Ssam 
6813654Ssam 	if (nitem == base) {
6913654Ssam 		for (n = 0; n < nitem; n++)
7013654Ssam 			if (list[n] != NULL)
7113654Ssam 				free(list[n]);
72*23608Sbloom 		return SUCCESS;
7313654Ssam 	}
7413654Ssam 	while(nitem > n) {
75*23608Sbloom 		/* We only have at most a SYSNSIZE character site name encoded
76*23608Sbloom 		 * in the file. However, we would like to use the full sitename
77*23608Sbloom 		 * if possible. If the number of chars in list[n] is < SYSNSIZE
78*23608Sbloom 		 * then the sitename could not have been truncated and
79*23608Sbloom 		 * we don't bother to check. Otherwise, we scan SYSFILE
80*23608Sbloom 		 * looking for the fullname and return it if we find it
81*23608Sbloom 		 */
8213654Ssam 		strcpy(sname, list[n++]);
83*23608Sbloom 		if (strlen(sname) >= SYSNSIZE) {
84*23608Sbloom 			register FILE *fp;
85*23608Sbloom 			register char *p;
86*23608Sbloom 			char line[MAXFULLNAME];
87*23608Sbloom 			fp = fopen(SYSFILE, "r");
88*23608Sbloom 			ASSERT(fp != NULL, CANTOPEN, SYSFILE, 0);
89*23608Sbloom 			while (cfgets(line, sizeof(line), fp) != NULL) {
90*23608Sbloom 				p = index(line, ' ');
91*23608Sbloom 				if (p)
92*23608Sbloom 					*p = '\0';
93*23608Sbloom 				if (strncmp(sname, line, SYSNSIZE) == SAME) {
94*23608Sbloom 					strncpy(sname, line, MAXBASENAME);
95*23608Sbloom 					break;
96*23608Sbloom 				}
97*23608Sbloom 			}
98*23608Sbloom 			fclose(fp);
99*23608Sbloom 		}
10013654Ssam 		if (callok(sname) == 0)
10117835Sralph 			return 1;
10213654Ssam 	}
10313654Ssam 	base = n = nitem;
10413654Ssam 	goto retry;
10513654Ssam }
10613654Ssam 
10717835Sralph /*
10817835Sralph  *	this routine will do a linear search of list (list) to find name (name).
10917835Sralph  *	If the name is not found, it is added to the list.
11017835Sralph  *	The number of items in the list (n) is returned (incremented if a
11117835Sralph  *	name is added).
11213654Ssam  *
11313654Ssam  *	return codes:
11413654Ssam  *		n - the number of items in the list
11513654Ssam  */
11613654Ssam 
11713654Ssam srchst(name, list, n)
11813654Ssam char *name;
11913654Ssam register char **list;
12013654Ssam int n;
12113654Ssam {
12213654Ssam 	register int i;
12313654Ssam 	register char *p;
12413654Ssam 
12513654Ssam 	for (i = 0; i < n; i++)
12613654Ssam 		if (strcmp(name, list[i]) == 0)
12713654Ssam 			break;
12813654Ssam 	if (i >= n) {
12913654Ssam 		if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char)))
13013654Ssam 			== NULL)
13117835Sralph 			return n;
13213654Ssam 		strcpy(p, name);
13313654Ssam 		list[n++] = p;
13413654Ssam 	}
13517835Sralph 	return n;
13613654Ssam }
137