- /* if child */
- if (pid == 0) {
- /*
- * Child exec barcode program, that take input (string to encode)
- * from pipein, remapped to stdin, and write the output (a PS
- * image) to stdout, remapped to pipeout
- */
- close(pipein[1]); /* close output side of input pipe */
- dup2(pipein[0], STDIN_FILENO); /* remap stdin in pipe input */
- close(pipeout[0]);
- dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
- execlp("barcode", "barcode", size, NULL); //"-o", "-", NULL);
- }
- /*
- * Parent write string to pipe input and close it,
- * then wait child execution and results form pipeout,
- * then fork to convert PS to JPEG using gs
- */
- close(pipein[0]); /* close input side of input pipe */
- write(pipein[1], argv[1], strlen(argv[1]));
- close(pipein[1]);
- waitpid(pid, NULL, 0);
- /*
- * refork to use gs
- */
- pid = fork();
- if (pid == -1) {
- WriteMess("child creation error");
- exit(0);
+ /* create barcode (in PS) */
+ pipein = popen("barcode", "w");
+ /* send barcode string to barcode program */
+ write(fileno(pipein), argv[1], strlen(argv[1]));
+ pclose(pipein);
+ /* close all pipes (in reverse order) */
+ for (i=4; i==0; i--) {
+ pclose((pipe[i]));