check for error on socket()
[unix-history] / usr / src / usr.bin / uucp / uucico / gnsys.c
CommitLineData
54d4ccd7 1#ifndef lint
44c9962f 2static char sccsid[] = "@(#)gnsys.c 5.4 (Berkeley) %G%";
54d4ccd7
SL
3#endif
4
54d4ccd7 5#include "uucp.h"
54d4ccd7
SL
6#ifdef NDIR
7#include "ndir.h"
8#else
ed412ce0 9#include <sys/dir.h>
54d4ccd7
SL
10#endif
11
44c9962f 12#define LSIZE 128 /* number of systems to store */
54d4ccd7
SL
13#define WSUFSIZE 6 /* work file name suffix size */
14
44c9962f
JB
15/*LINTLIBRARY*/
16
46b15d8a
RC
17/*
18 * this routine will return the next system name which has work to be done.
19 * "sname" is a string of size DIRSIZ - WSUFSIZE.
54d4ccd7
SL
20 * "pre" is the prefix for work files.
21 * "dir" is the directory to search.
54d4ccd7
SL
22 *
23 * return codes:
54d4ccd7 24 * 1 - name returned in sname
44c9962f 25 * SUCCESS - no more names
54d4ccd7
SL
26 * FAIL - bad directory
27 */
28
29gnsys(sname, dir, pre)
30char *sname, *dir, pre;
31{
32 register char *s, *p1, *p2;
33 char px[3];
34 static char *list[LSIZE];
35 static int nitem=0, n=0, base=0;
36 char systname[NAMESIZE], filename[NAMESIZE];
37 DIR *dirp;
38
39retry:
40 px[0] = pre;
41 px[1] = '.';
42 px[2] = '\0';
43 if (nitem == base) {
44 /* get list of systems with work */
45 int i;
46b15d8a 46 dirp = opendir(subdir(dir,pre));
54d4ccd7
SL
47 ASSERT(dirp != NULL, "BAD DIRECTORY", dir, 0);
48 for (i = base; i < LSIZE; i++)
49 list[i] = NULL;
50 while (gnamef(dirp, filename) != 0) {
51 if (!prefix(px, filename))
52 continue;
53 p2 = filename + strlen(filename)
54 - WSUFSIZE;
55 p1 = filename + strlen(px);
56 for(s = systname; p1 <= p2; p1++)
57 *s++ = *p1;
58 *s = '\0';
59 if (systname[0] == '\0')
60 continue;
61 nitem = srchst(systname, list, nitem);
62 if (LSIZE <= nitem) break;
63 }
64
65 closedir(dirp);
66 }
67
68 if (nitem == base) {
69 for (n = 0; n < nitem; n++)
70 if (list[n] != NULL)
71 free(list[n]);
44c9962f 72 return SUCCESS;
54d4ccd7
SL
73 }
74 while(nitem > n) {
44c9962f
JB
75 /* We only have at most a SYSNSIZE character site name encoded
76 * in the file. However, we would like to use the full sitename
77 * if possible. If the number of chars in list[n] is < SYSNSIZE
78 * then the sitename could not have been truncated and
79 * we don't bother to check. Otherwise, we scan SYSFILE
80 * looking for the fullname and return it if we find it
81 */
54d4ccd7 82 strcpy(sname, list[n++]);
44c9962f
JB
83 if (strlen(sname) >= SYSNSIZE) {
84 register FILE *fp;
85 register char *p;
86 char line[MAXFULLNAME];
87 fp = fopen(SYSFILE, "r");
88 ASSERT(fp != NULL, CANTOPEN, SYSFILE, 0);
89 while (cfgets(line, sizeof(line), fp) != NULL) {
90 p = index(line, ' ');
91 if (p)
92 *p = '\0';
93 if (strncmp(sname, line, SYSNSIZE) == SAME) {
94 strncpy(sname, line, MAXBASENAME);
95 break;
96 }
97 }
98 fclose(fp);
99 }
54d4ccd7 100 if (callok(sname) == 0)
46b15d8a 101 return 1;
54d4ccd7
SL
102 }
103 base = n = nitem;
104 goto retry;
105}
106
46b15d8a
RC
107/*
108 * this routine will do a linear search of list (list) to find name (name).
109 * If the name is not found, it is added to the list.
110 * The number of items in the list (n) is returned (incremented if a
111 * name is added).
54d4ccd7
SL
112 *
113 * return codes:
114 * n - the number of items in the list
115 */
116
117srchst(name, list, n)
118char *name;
119register char **list;
120int n;
121{
122 register int i;
123 register char *p;
124
125 for (i = 0; i < n; i++)
126 if (strcmp(name, list[i]) == 0)
127 break;
128 if (i >= n) {
129 if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char)))
130 == NULL)
46b15d8a 131 return n;
54d4ccd7
SL
132 strcpy(p, name);
133 list[n++] = p;
134 }
46b15d8a 135 return n;
54d4ccd7 136}