BSD 3 development
[unix-history] / usr / src / cmd / uucp / anlwrk.c
CommitLineData
7d511ee1
BJ
1 /* anlwrk 2.1 5/22/79 09:59:09 */
2#include "uucp.h"
3#include <sys/types.h>
4#include <sys/stat.h>
5
6static char SiD[] = "@(#)anlwrk 2.1";
7
8
9#define LLEN 20
10#define SAME 0
11
12/*******
13 * anlwrk(file, wvec) create a vector of command arguments
14 * char *file, **wvec;
15 *
16 * return codes:
17 * 0 - no more work in this file
18 * positive number - number of arguments
19 */
20
21anlwrk(file, wvec)
22char *file, **wvec;
23{
24 static char str[BUFSIZ];
25 static FILE *fp = NULL;
26
27 if (file[0] == '\0')
28 return(0);
29 if (fp == NULL) {
30 fp = fopen(file, "r");
31 if (fp == NULL)
32 return(0);
33 }
34
35 if (fgets(str, BUFSIZ, fp) == NULL) {
36 fclose(fp);
37 unlink(file);
38 file[0] = '\0';
39 fp = NULL;
40 return(0);
41 }
42
43 return(getargs(str, wvec));
44}
45
46
47/***
48 * iswrk(file, reqst, dir, pre)
49 * char *file, *reqst, *dir, *pre;
50 *
51 * iswrk - this routine will check the work list (list).
52 * If it is empty or the present work is exhausted, it
53 * will call gtwrk to generate a new list.
54 * The "reqst" field will be the string "chk" or "get" to
55 * check for work, or get the next work file respectively.
56 *
57 * return codes:
58 * 0 - no more work (or some error)
59 * 1 - there is work
60 */
61
62iswrk(file, reqst, dir, pre)
63char *file, *reqst, *dir, *pre;
64{
65 static char **listp, *list[LLEN];
66
67 if (listp == NULL || *listp == NULL || listp > (list + LLEN)
68 || !prefix(pre, *listp)) {
69 int i;
70 for (i = 0, listp = list; i < LLEN; i++) {
71 if (*listp != NULL)
72 free(*listp);
73 *listp++ = NULL;
74 }
75 if (gtwrk(dir, pre, listp = list, LLEN) != 0)
76 /* alloc error */
77 return(0);
78 }
79
80 if (*listp == NULL)
81 return(0);
82
83 if (strcmp(reqst, "get") == SAME)
84 sprintf(file, "%s/%s", dir, *listp++);
85 return(1);
86}
87
88
89/***
90 * gtwvec(file, dir, wkpre, wrkvec) get work vector
91 * char *file, *dir, *wkpre, **wrkvec;
92 *
93 * return codes:
94 * positive number - number of arguments
95 * 0 - no arguments - fail
96 */
97
98gtwvec(file, dir, wkpre, wrkvec)
99char *file, *dir, *wkpre, **wrkvec;
100{
101 int nargs;
102
103 while ((nargs = anlwrk(file, wrkvec)) == 0) {
104 if (!iswrk(file, "get", dir, wkpre))
105 return(0);
106 }
107 return(nargs);
108}
109
110
111/***
112 * gtwrk(dir, pre, list, llen)
113 * char *dir, *pre, **list;
114 * int llen;
115 *
116 * gtwrk - this routine will build a sorted list
117 * of files in a directory.
118 * "dir" is the directory name to search for file names
119 * beginning with the prefix (pre).
120 * "list" is the pointer to the list and "llen" is the
121 * length of the list.
122 *
123 * return codes: 0 | FAIL
124 */
125
126gtwrk(dir, pre, list, llen)
127char *dir, *pre, **list;
128int llen;
129{
130 struct stat s;
131 char filename[NAMESIZE], *p;
132 char **first, **last;
133 FILE *pdir;
134 extern int compar();
135 extern char *calloc();
136
137 first = last = list;
138 if ((pdir = fopen(dir, "r")) == NULL)
139 return(FAIL);
140 while (gnamef(pdir, filename) && (last - first) < llen) {
141 if (!prefix(pre, filename))
142 continue;
143 if (stat(filename, &s) == -1)
144 continue;
145 if ((s.st_mode & ANYREAD) == 0)
146 continue;
147 if ((p = calloc(strlen(filename) + 1, sizeof (char))) == NULL)
148 return(FAIL);
149
150 strcpy(p, filename);
151 *last++ = p;
152 }
153
154 fclose(pdir);
155 qsort(first, last - first, sizeof *last, compar);
156 return(0);
157}
158
159
160/***
161 * compar(p1, p2)
162 * char **p1, **p2;
163 *
164 * compar - this routine is used by qsort.
165 *
166 */
167
168compar(p1, p2)
169char **p1, **p2;
170{
171 return(strcmp(*p1, *p2));
172}