|
|
1.1 root 1: /* /sccs/src/cmd/uucp/s.anlwrk.c
2: anlwrk.c 1.3 8/30/84 17:36:58
3:
4: This module contains routines that find C. files
5: in a system spool directory, return the next C. file
6: to process, and break up the C. line into arguments
7: for processing.
8: */
9: #include "uucp.h"
10: VERSION(@(#)anlwrk.c 1.3);
11:
12: #define BOOKMARK_PRE 'A'
13: #define CLEAN_RETURN(fp) {\
14: if (fp != NULL) \
15: (void) fclose(fp); \
16: fp = NULL; \
17: return(0); \
18: }
19:
20: /* C.princetN0026 - ('C' + '.') - "princet" */
21: #define SUFSIZE (MAXBASENAME - 2 - SYSNSIZE)
22: #define LLEN 50
23: #define MAXRQST 250
24:
25: static void insert();
26: static int bldflst();
27:
28: static char Filent[LLEN][NAMESIZE]; /* array of C. file names (text) */
29: static char *Fptr[LLEN]; /* pointers to names in Filent */
30: static short Nnext; /* index of next C. file in Fptr list */
31: static short Nfiles = 0; /* Number of files in Filent */
32:
33: /*
34: * read a line from the workfile (C.file)
35: * file -> work file (Input/Output) made '\0' after work completion
36: * wvec -> address of array to return arguments (Output)
37: * wcount -> maximum # of arguments to return in wvec
38: * NOTE: wvec should be large enough to accept wcount + 1 pointers
39: * since NULL is inserted after last item.
40: * returns:
41: * 0 -> no more work in this file
42: * positive # -> number of arguments
43: */
44: anlwrk(file, wvec, wcount)
45: char *file, **wvec;
46: {
47: register i;
48: register FILE *p_bookmark; /* pointer to afile */
49: static FILE *fp = NULL; /* currently opened C. file pointer */
50: static char afile[NAMESIZE+1]; /* file with line count for book marks */
51: static char str[MAXRQST]; /* the string which wvec points to */
52: static short acount;
53: struct stat stbuf;
54: int nargs; /* return value == # args in the line */
55:
56: if (file[0] == '\0') {
57: if (fp != NULL)
58: errent("anlwrk",
59: "attempt made to use old workfile was thwarted", 0,
60: sccsid, __FILE__, __LINE__);
61: CLEAN_RETURN(fp);
62: }
63: if (fp == NULL) {
64: fp = fopen(file, "r");
65:
66: if (fp == NULL){ /* can't open C. file! */
67: errent(Ct_OPEN,file,errno, sccsid, __FILE__, __LINE__);
68: /* this may not work, but we'll try it */
69: /* It will fail if the C. name is more than */
70: /* the standard 14 characters - if this is the */
71: /* tocorrupt will exit with ASSERT */
72: toCorrupt(file);
73: return(0);
74: }
75: (void) fstat(fileno(fp), &stbuf);
76: Nstat.t_qtime = stbuf.st_mtime;
77:
78: (void) strncpy(afile, BASENAME(file, '/'), NAMESIZE);
79: afile[NAMESIZE] = NULLCHAR;
80: *afile = BOOKMARK_PRE; /* make up name by replacing C with A */
81: acount = 0;
82: p_bookmark = fopen(afile, "r");
83: if (p_bookmark != NULL) {
84: /* get count of already completed work */
85: i = fscanf(p_bookmark, "%hd", &acount);
86: (void) fclose(p_bookmark);
87: if (i <= 0)
88: acount = 0;
89:
90: /* skip lines which have already been processed */
91: for (i = 0; i < acount; i++) {
92: if (fgets(str, MAXRQST, fp) == NULL)
93: break;
94: }
95: }
96:
97: }
98:
99: if (fgets(str, MAXRQST, fp) == NULL) {
100: ASSERT(unlink(file) == 0, Ct_UNLINK, file, errno);
101: (void) unlink(afile);
102: DEBUG(4,"Finished Processing file: %s\n",file);
103: *file = '\0';
104: CLEAN_RETURN(fp);
105: }
106:
107: nargs = getargs(str, wvec, wcount);
108:
109: /* sanity checks for C. file */
110: if ((str[0] != 'R' && str[0] != 'S') /* legal wrktypes are R and S */
111: || (str[0] == 'R' && nargs < 6) /* R lines need >= 6 entries */
112: || (str[0] == 'S' && nargs < 7)) { /* S lines need >= 7 entries */
113: /* bad C. file - stash it */
114: toCorrupt(file);
115: (void) unlink(afile);
116: *file = '\0';
117: CLEAN_RETURN(fp);
118: }
119:
120: p_bookmark = fopen(afile, "w"); /* update bookmark file */
121: if (p_bookmark == NULL)
122: errent(Ct_OPEN, afile, errno, sccsid, __FILE__, __LINE__);
123: else {
124: chmod(afile, CFILEMODE);
125: (void) fprintf(p_bookmark, "%d", acount);
126: (void) fclose(p_bookmark);
127: }
128: acount++;
129: return(nargs);
130: }
131:
132: /*
133: * Check the list of work files (C.sys).
134: * If it is empty or the present work is exhausted, it
135: * will call gtwrk to generate a new list.
136: *
137: * file -> address of array to return full pathname in
138: * returns:
139: * 0 -> no more work (or some error)
140: * 1 -> there is work
141: */
142: iswrk(file)
143: char *file;
144: {
145: if (Nfiles == 0 && (bldflst() == 0) )
146: return(0);
147:
148: (void) sprintf(file, "%s/%s", RemSpool, Fptr[Nnext]);
149: Nfiles--;
150: Nnext++;
151: return(1);
152: }
153:
154:
155: /*
156: * build list of work files for given system using an insertion sort
157: * Nfiles, Nnext, RemSpool and Rmtname are global
158: *
159: * return:
160: * number of C. files in list - (Nfiles)
161: */
162: static
163: bldflst()
164: {
165: register DIR *pdir;
166: char filename[NAMESIZE];
167: char prefix[SYSNSIZE+3];
168:
169: Nnext = Nfiles = 0;
170: if ((pdir = opendir(RemSpool)) == NULL)
171: return(0);
172:
173: (void) sprintf(prefix, "C.%.*s", SYSNSIZE, Rmtname);
174: while (gnamef(pdir, filename) ) {
175: if (!PREFIX(prefix, filename))
176: continue;
177: if ((strlen(filename)-strlen(prefix)) != SUFSIZE) {
178: errent("bldflst: Funny filename", filename, 0,
179: sccsid, __FILE__, __LINE__);
180: continue;
181: }
182: insert(filename);
183: }
184: closedir(pdir);
185: return(Nfiles);
186: }
187:
188: /*
189: * get work return
190: * file -> place to deposit file name
191: * wrkvec -> array to return arguments
192: * wcount -> max number of args for wrkvec
193: * returns:
194: * nargs -> number of arguments
195: * 0 -> no arguments - fail
196: */
197: gtwvec(file, wrkvec, wcount)
198: char *file, *wrkvec;
199: {
200: register int nargs;
201:
202: DEBUG(7, "gtwvec: dir %s\n", RemSpool);
203: while ((nargs = anlwrk(file, wrkvec, wcount)) == 0) {
204: if (!iswrk(file))
205: return(0);
206: }
207: DEBUG(7, " return - %d\n", nargs);
208: return(nargs);
209: }
210:
211:
212: /*
213: * insert - insert file name in sorted list
214: * return - none
215: */
216: static
217: void
218: insert(file)
219: char *file;
220: {
221: register i, j;
222: register char *p;
223:
224: DEBUG(7, "insert(%s) ", file);
225: for (i = Nfiles; i>0; i--) {
226: if (strcmp(file, Fptr[i-1]) > 0)
227: break;
228: }
229: if (i == LLEN) /* if this is off the end get out */
230: return;
231:
232: /* get p (pointer) to where the text of name will go */
233: if (Nfiles == LLEN) /* last possible entry */
234: /* put in text of last and decrement Nfiles for make hole */
235: p = strcpy(Fptr[--Nfiles], file);
236: else
237: p = strcpy(Filent[Nfiles], file); /* copy to next free */
238:
239: /* make a hole for new entry */
240: for (j = Nfiles; j >i; j--)
241: Fptr[j] = Fptr[j-1];
242:
243: DEBUG(7, "insert %s ", p);
244: DEBUG(7, "at %d\n", i);
245: Fptr[i] = p;
246: Nfiles++;
247: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.