1*48664Sbostic /*- 2*48664Sbostic * Copyright (c) 1985 The Regents of the University of California. 3*48664Sbostic * All rights reserved. 4*48664Sbostic * 5*48664Sbostic * %sccs.include.proprietary.c% 6*48664Sbostic */ 7*48664Sbostic 813654Ssam #ifndef lint 9*48664Sbostic static char sccsid[] = "@(#)gnsys.c 5.7 (Berkeley) 04/24/91"; 10*48664Sbostic #endif /* not lint */ 1113654Ssam 1213654Ssam #include "uucp.h" 1313654Ssam #ifdef NDIR 1413654Ssam #include "ndir.h" 1513654Ssam #else 1613702Ssam #include <sys/dir.h> 1713654Ssam #endif 1813654Ssam 1933951Srick #define LSIZE 512 /* number of systems to store */ 2013654Ssam #define WSUFSIZE 6 /* work file name suffix size */ 2113654Ssam 2223608Sbloom /*LINTLIBRARY*/ 2323608Sbloom 2417835Sralph /* 2517835Sralph * this routine will return the next system name which has work to be done. 2617835Sralph * "sname" is a string of size DIRSIZ - WSUFSIZE. 2713654Ssam * "pre" is the prefix for work files. 2813654Ssam * "dir" is the directory to search. 2913654Ssam * 3013654Ssam * return codes: 3113654Ssam * 1 - name returned in sname 3223608Sbloom * SUCCESS - no more names 3313654Ssam * FAIL - bad directory 3413654Ssam */ 3513654Ssam gnsys(sname, dir, pre) 3613654Ssam char *sname, *dir, pre; 3713654Ssam { 3833568Srick register DIR *dirp; 3933568Srick register struct direct *dentp; 4013654Ssam static char *list[LSIZE]; 4133568Srick static int nitem = 0, n = 0, base = 0; 4233568Srick char systname[NAMESIZE]; 4313654Ssam 4413654Ssam retry: 4513654Ssam if (nitem == base) { 4613654Ssam /* get list of systems with work */ 4713654Ssam int i; 4817835Sralph dirp = opendir(subdir(dir,pre)); 4933951Srick if (dirp == NULL) { 5033951Srick syslog(LOG_ERR, "opendir(%s) failed: %m", 5133951Srick subdir(dir,pre)); 5233951Srick cleanup(FAIL); 5333951Srick } 5413654Ssam for (i = base; i < LSIZE; i++) 5513654Ssam list[i] = NULL; 5633568Srick while (dentp = readdir(dirp)) { 5733568Srick register char *s, *p1, *p2; 5833568Srick if (dentp->d_name[0] != pre || dentp->d_name[1] != '.') 5913654Ssam continue; 6033568Srick p2 = dentp->d_name + dentp->d_namlen - WSUFSIZE; 6133568Srick p1 = dentp->d_name + 2; 6213654Ssam for(s = systname; p1 <= p2; p1++) 6313654Ssam *s++ = *p1; 6413654Ssam *s = '\0'; 6513654Ssam if (systname[0] == '\0') 6613654Ssam continue; 6713654Ssam nitem = srchst(systname, list, nitem); 6833951Srick if (LSIZE <= nitem) { 6933951Srick syslog(LOG_WARNING, 7033951Srick "%s: Increase LSIZE in gnsys.c", 7133951Srick systname); 7233568Srick break; 7333951Srick } 7413654Ssam } 7513654Ssam closedir(dirp); 7613654Ssam } 7713654Ssam 7813654Ssam if (nitem == base) { 7913654Ssam for (n = 0; n < nitem; n++) 8013654Ssam if (list[n] != NULL) 8113654Ssam free(list[n]); 8223608Sbloom return SUCCESS; 8313654Ssam } 8433568Srick while (nitem > n) { 8523608Sbloom /* We only have at most a SYSNSIZE character site name encoded 8623608Sbloom * in the file. However, we would like to use the full sitename 8723608Sbloom * if possible. If the number of chars in list[n] is < SYSNSIZE 8823608Sbloom * then the sitename could not have been truncated and 8923608Sbloom * we don't bother to check. Otherwise, we scan SYSFILE 9023608Sbloom * looking for the fullname and return it if we find it 9123608Sbloom */ 9213654Ssam strcpy(sname, list[n++]); 9323608Sbloom if (strlen(sname) >= SYSNSIZE) { 9423608Sbloom register FILE *fp; 9523608Sbloom register char *p; 9623608Sbloom char line[MAXFULLNAME]; 9723608Sbloom fp = fopen(SYSFILE, "r"); 9833951Srick if (fp == NULL) { 9933951Srick syslog(LOG_ERR, "fopen(%s) failed: %m", 10033951Srick SYSFILE); 10133951Srick cleanup(FAIL); 10233951Srick } 10323608Sbloom while (cfgets(line, sizeof(line), fp) != NULL) { 10433951Srick p = strpbrk(line, " \t"); 10523608Sbloom if (p) 10623608Sbloom *p = '\0'; 10723608Sbloom if (strncmp(sname, line, SYSNSIZE) == SAME) { 10823608Sbloom strncpy(sname, line, MAXBASENAME); 10923608Sbloom break; 11023608Sbloom } 11123608Sbloom } 11223608Sbloom fclose(fp); 11323608Sbloom } 11413654Ssam if (callok(sname) == 0) 11517835Sralph return 1; 11613654Ssam } 11713654Ssam base = n = nitem; 11813654Ssam goto retry; 11913654Ssam } 12013654Ssam 12117835Sralph /* 12217835Sralph * this routine will do a linear search of list (list) to find name (name). 12317835Sralph * If the name is not found, it is added to the list. 12417835Sralph * The number of items in the list (n) is returned (incremented if a 12517835Sralph * name is added). 12613654Ssam * 12713654Ssam * return codes: 12813654Ssam * n - the number of items in the list 12913654Ssam */ 13013654Ssam srchst(name, list, n) 13113654Ssam char *name; 13213654Ssam register char **list; 13313654Ssam int n; 13413654Ssam { 13513654Ssam register int i; 13613654Ssam register char *p; 13713654Ssam 13813654Ssam for (i = 0; i < n; i++) 13913654Ssam if (strcmp(name, list[i]) == 0) 14013654Ssam break; 14113654Ssam if (i >= n) { 14213654Ssam if ((p = calloc((unsigned)strlen(name) + 1, sizeof (char))) 14313654Ssam == NULL) 14417835Sralph return n; 14513654Ssam strcpy(p, name); 14613654Ssam list[n++] = p; 14713654Ssam } 14817835Sralph return n; 14913654Ssam } 150