#include #include #include main(int argc, char *argv[]) { char arg[81], *tag; char buf[161], tnam[81], prog[2], *bp; int k=0, l, n, m; FILE *fp, *pipe; if (argc != 2) { fprintf(stderr, "Usage: job1 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"); 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) { bp = &buf[m-1]; fprintf(stderr, "\n%% %s\n", bp); if ((pipe=popen(bp,"w")) == NULL) { fprintf(stderr, "### job: pipe open fail ###\n"); exit(1); } while (1) { if (fgets(buf,161,fp) == NULL) { k = 1; break; } if (buf[0] == ':') break; fputs(buf, pipe); } if (pclose(pipe) != 0) { fprintf(stderr, "*** ERROR: Job terminated ***\n"); exit(1); } } fclose(fp); fprintf(stderr, "\n=== End of Job ===\n"); }