#include "uucp.h"
#define BOOKMARK_PRE 'A'
#define CLEAN_RETURN(fp) {\
if (fp != NULL) \
(void) fclose(fp); \
fp = NULL; \
return(0); \
\
}
#define SUFSIZE (MAXBASENAME - 2 - SYSNSIZE)
#define LLEN 50
#define MAXRQST 250
static void insert();
static int anlwrk(), bldflst();
extern int iswrk(), gtwvec(), gnamef();
static char Filent[LLEN][NAMESIZE];
static char *Fptr[LLEN];
static short Nnext;
static short Nfiles = 0;
static int
anlwrk(char *file, char **wvec, int wcount)
{
int i;
FILE *p_bookmark;
static FILE *fp = NULL;
static char afile[NAMESIZE];
static char str[MAXRQST];
static short acount;
struct stat stbuf;
int nargs;
if (file[0] == '\0') {
if (fp != NULL)
errent("anlwrk",
"attempt made to use old workfile was thwarted", 0,
__FILE__, __LINE__);
CLEAN_RETURN(fp);
}
if (fp == NULL) {
fp = fopen(file, "r");
if (fp == NULL){
errent(Ct_OPEN,file,errno, __FILE__, __LINE__);
toCorrupt(file);
return(0);
}
(void) fstat(fileno(fp), &stbuf);
Nstat.t_qtime = stbuf.st_mtime;
(void) strncpy(afile, BASENAME(file, '/'), NAMESIZE);
afile[NAMESIZE-1] = NULLCHAR;
*afile = BOOKMARK_PRE;
acount = 0;
p_bookmark = fopen(afile, "r");
if (p_bookmark != NULL) {
i = fscanf(p_bookmark, "%hd", &acount);
(void) fclose(p_bookmark);
if (i <= 0)
acount = 0;
for (i = 0; i < acount; i++) {
if (fgets(str, MAXRQST, fp) == NULL)
break;
}
}
}
if (fgets(str, MAXRQST, fp) == NULL) {
ASSERT(unlink(file) == 0, Ct_UNLINK, file, errno);
(void) unlink(afile);
DEBUG(4,"Finished Processing file: %s\n",file);
*file = '\0';
CLEAN_RETURN(fp);
}
nargs = getargs(str, wvec, wcount);
if ((str[0] != 'R' && str[0] != 'S')
|| (str[0] == 'R' && nargs < 6)
|| (str[0] == 'S' && nargs < 7)) {
toCorrupt(file);
(void) unlink(afile);
*file = '\0';
CLEAN_RETURN(fp);
}
p_bookmark = fopen(afile, "w");
if (p_bookmark == NULL)
errent(Ct_OPEN, afile, errno, __FILE__, __LINE__);
else {
chmod(afile, CFILEMODE);
(void) fprintf(p_bookmark, "%d", acount);
(void) fclose(p_bookmark);
}
acount++;
return(nargs);
}
extern int
iswrk(file)
char *file;
{
char newspool[MAXFULLNAME];
char lockname[MAXFULLNAME];
char gradedir[2*MAXBASENAME];
if (Nfiles == 0) {
if (Role == MASTER) {
if (*JobGrade == NULLCHAR)
return(0);
if (bldflst() != 0) {
(void) sprintf(file, "%s/%s", RemSpool, Fptr[Nnext]);
Nfiles--;
Nnext++;
return(1);
}
(void) sprintf(lockname, "%.*s.%s", SYSNSIZE, Rmtname, JobGrade);
delock(LOCKPRE, lockname);
} else {
(void) sprintf(lockname, "%ld", (long) getpid());
delock(LOCKPRE, lockname);
}
(void) sprintf(newspool, "%s/%s", SPOOL, Rmtname);
ASSERT(chdir(newspool) == 0, Ct_CHDIR, newspool, errno);
findgrade(newspool, JobGrade);
DEBUG(4, "Job grade to process - %s\n", JobGrade);
if (*JobGrade == NULLCHAR)
return(0);
(void) sprintf(lockname, "%.*s.%s", SYSNSIZE, Rmtname, JobGrade);
(void) umlock(LOCKPRE, lockname);
(void) sprintf(gradedir, "%s/%s", Rmtname, JobGrade);
chremdir(gradedir);
bldflst();
}
(void) sprintf(file, "%s/%s", RemSpool, Fptr[Nnext]);
Nfiles--;
Nnext++;
return(1);
}
static int
bldflst()
{
DIR *pdir;
char filename[NAMESIZE];
char prefix[SYSNSIZE+3];
Nnext = Nfiles = 0;
if ((pdir = opendir(RemSpool)) == NULL)
return(0);
(void) sprintf(prefix, "C.%.*s", SYSNSIZE, Rmtname);
while (gnamef(pdir, filename) ) {
if (!PREFIX(prefix, filename))
continue;
if ((strlen(filename)-strlen(prefix)) != SUFSIZE) {
errent("bldflst: Funny filename", filename, 0,
__FILE__, __LINE__);
continue;
}
insert(filename);
}
closedir(pdir);
return(Nfiles);
}
extern int
gtwvec(char *file, char **wrkvec, int wcount)
{
int nargs;
DEBUG(7, "gtwvec: dir %s\n", RemSpool);
while ((nargs = anlwrk(file, wrkvec, wcount)) == 0) {
if (!iswrk(file))
return(0);
}
DEBUG(7, " return - %d\n", nargs);
return(nargs);
}
static void
insert(file)
char *file;
{
int i, j;
char *p;
DEBUG(7, "insert(%s) ", file);
for (i = Nfiles; i>0; i--) {
if (strcmp(file, Fptr[i-1]) > 0)
break;
}
if (i == LLEN)
return;
if (Nfiles == LLEN)
p = strcpy(Fptr[--Nfiles], file);
else
p = strcpy(Filent[Nfiles], file);
for (j = Nfiles; j >i; j--)
Fptr[j] = Fptr[j-1];
DEBUG(7, "insert %s ", p);
DEBUG(7, "at %d\n", i);
Fptr[i] = p;
Nfiles++;
return;
}