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