#include #include #include main(int argc, char *argv[]) { char arg[81], *tag; char buf[161], tnam[81], prog[2], *bp; char line[256], temp[13]; int k=0, l, n, m; FILE *fp, *ftmp; if (argc != 2) { fprintf(stderr, "Usage: job ctrl-file[:tag]\n"); exit(1); } if (strlen(argv[1]) > 80) { fprintf(stderr, " too long argument\n"); exit(1); } strcpy(arg, argv[1]); if ((tag=strchr(arg,':')) != NULL) { *tag = '\0'; tag++; } if ((fp=fopen(arg,"r")) == NULL) { fprintf(stderr, " file %s not found\n", arg); exit(1); } while (1) { if (fgets(buf,161,fp) == NULL) { fprintf(stderr, "*** Job not found ***\n"); exit(1); } if (buf[0] != ':') continue; sscanf(buf, "%80s", tnam); if ((tag==NULL) || (strcmp(&tnam[1],tag)== 0)) break; } fprintf(stderr, "=== Start of Job ===\n"); while (k == 0) { if ((bp=strstr(buf,";#")) != NULL) *bp = '\0'; l = strlen(buf); if (l > 0) if (buf[l-1] == '\n') { l--; buf[l]='\0'; } if (l > 0) if (buf[l-1] == '\r') { l--; buf[l]='\0'; } n = sscanf(buf, "%80s%1s%n", tnam,prog,&m); if (n == 1) { if (strcmp(tnam,"::") == 0) break; while (1) { if (fgets(buf,161,fp) == NULL) { k = 1; break; } if (buf[0] == ':') break; } continue; } bp = &buf[m-1]; strcpy(temp, "@@tmp.XXXXXX"); if ((ftmp=fdopen(mkstemp(temp),"w")) == NULL) { fprintf(stderr, "### @@tmp.file open fail ###\n"); exit(1); } strcpy(line, "cat "); strcat(line, temp); strcat(line, " | "); strcat(line, bp); fprintf(stderr, "\n%% %s\n", bp); while (1) { if (fgets(buf,161,fp) == NULL) { k = 1; break; } if (buf[0] == ':') break; fputs(buf, ftmp); } fclose(ftmp); if (system(line) != 0) { fprintf(stderr, "*** ERROR: Job terminated ***\n"); exit(1); } strcpy(line, "rm -f "); strcat(line, temp); if (system(line) != 0) { fprintf(stderr, "### @@tmp.file remove fail ###\n"); exit(1); } } fclose(fp); fprintf(stderr, "\n=== End of Job ===\n"); }