// Open the input or output file, setting up a pipe if needed FILE * openfile(char *fname, int *rpid, int *rfd) { int pipefd[4]; int pid; FILE *f; char *efname; while (*fname && (*fname == ' ')) // Skip leading blanks fname++; if (*fname != '|') { // Open file if not pipe *rpid = 0; if (rfd != NULL) *rfd = 1; // Set to stdout just in case efname = findhome(fname); return (fopen(efname, rfd == NULL ? "w" : "r")); } fname++; // Skip | efname = findhome(fname); if (pipe(pipefd) < 0 || (rfd != NULL && pipe(pipefd+2) < 0)) { error("Can't make pipe to child"); *rpid = 0; return (0); } //deraw(rfd==NULL); if ((pid=fork()) == 0) { // if child (void) close(0); // close stdin (void) close(pipefd[1]); (void) dup(pipefd[0]); // connect to first pipe if (rfd != NULL) { // if opening for read (void) close(1); // close stdout (void) close(pipefd[2]); (void) dup(pipefd[3]); // connect to second pipe } (void) signal(SIGINT, SIG_DFL); // reset execl("/bin/sh", "sh", "-c", efname, 0, (char *) NULL); exit (-127); } else { // else parent *rpid = pid; if ((f = fdopen(pipefd[(rfd==NULL?1:2)], rfd==NULL?"w":"r")) == NULL) { (void) kill(pid, 9); error("Can't fdopen %sput", rfd==NULL?"out":"in"); (void) close(pipefd[1]); if (rfd != NULL) (void) close(pipefd[3]); *rpid = 0; return (0); } } (void) close(pipefd[0]); if (rfd != NULL) { (void) close(pipefd[3]); *rfd = pipefd[1]; } return (f); }
int cwritefile(char *fname, int r0, int c0, int rn, int cn) { register FILE *f; int pipefd[2]; int fildes; int pid; char save[PATHLEN]; char *fn; char *busave; if (*fname == '\0') fname = &curfile[0]; fn = fname; while (*fn && (*fn == ' ')) /* Skip leading blanks */ fn++; if (*fn == '|') { error("Can't have encrypted pipe"); return (-1); } (void) strcpy(save, fname); busave = findhome(save); #ifdef DOBACKUPS if (!backup_file(busave) && (yn_ask("Could not create backup copy, Save anyway?: (y,n)") != 1)) return (0); #endif if ((fildes = open (busave, O_TRUNC|O_WRONLY|O_CREAT, 0600)) < 0) { error("Can't create file \"%s\"", save); return (-1); } if (pipe(pipefd) < 0) { error("Can't make pipe to child\n"); return (-1); } if (KeyWord[0] == '\0') { deraw(1); (void) strcpy(KeyWord, getpass("Enter key:")); goraw(); } if ((pid=fork()) == 0) { /* if child */ (void) close(0); /* close stdin */ (void) close(1); /* close stdout */ (void) close(pipefd[1]); /* close pipe output */ (void) dup(pipefd[0]); /* connect to pipe input */ (void) dup(fildes); /* standard out to file */ (void) fprintf(stderr, " "); (void) execl(CRYPT_PATH, "crypt", KeyWord, 0); (void) fprintf(stderr, "execl(%s, \"crypt\", %s, 0) in cwritefile() failed", CRYPT_PATH, KeyWord); exit (-127); } else { /* else parent */ (void) close(fildes); (void) close(pipefd[0]); /* close pipe input */ f = fdopen(pipefd[1], "w"); if (f == 0) { (void) kill(pid, -9); error("Can't fdopen file \"%s\"", save); (void) close(pipefd[1]); return (-1); } } write_fd(f, r0, c0, rn, cn); (void) fclose(f); (void) close(pipefd[1]); while (pid != wait(&fildes)) /**/; (void) strcpy(curfile,save); modflg = 0; error("File \"%s\" written (encrypted).", curfile); return (0); }
void creadfile(char *save, int eraseflg) { register FILE *f; int pipefd[2]; int fildes; int pid; if (eraseflg && strcmp(save, curfile) && modcheck(" first")) return; if ((fildes = open(findhome(save), O_RDONLY, 0)) < 0) { error ("Can't read file \"%s\"", save); return; } if (eraseflg) erasedb(); if (pipe(pipefd) < 0) { error("Can't make pipe to child"); return; } deraw(1); (void) strcpy(KeyWord, getpass("Enter key:")); goraw(); if ((pid=fork()) == 0) { /* if child */ (void) close(0); /* close stdin */ (void) close(1); /* close stdout */ (void) close(pipefd[0]); /* close pipe input */ (void) dup(fildes); /* standard in from file */ (void) dup(pipefd[1]); /* connect to pipe */ (void) fprintf(stderr, " "); (void) execl(CRYPT_PATH, "crypt", KeyWord, 0); (void) fprintf(stderr, "execl(%s, \"crypt\", %s, 0) in creadfile() failed", CRYPT_PATH, KeyWord); exit(-127); } else { /* else parent */ (void) close(fildes); (void) close(pipefd[1]); /* close pipe output */ if ((f = fdopen(pipefd[0], "r")) == (FILE *)0) { (void) kill(pid, 9); error("Can't fdopen file \"%s\"", save); (void)close(pipefd[0]); return; } } loading++; while (fgets(line, sizeof(line), f)) { linelim = 0; if (line[0] != '#') (void) yyparse(); } --loading; (void) fclose(f); (void) close(pipefd[0]); while (pid != wait(&fildes)) /**/; linelim = -1; if (eraseflg) { (void) strcpy(curfile, save); modflg = 0; } }