Aggiornamenti + alcune sockopt di IP
[gapil.git] / listati / BarCodePage.c
1 int main(int argc, char *argv[], char *envp[])
2 {
3     ...
4     /* create two pipes, pipein and pipeout, to handle communication */
5     if ( (retval = pipe(pipein)) ) {
6         WriteMess("input pipe creation error");
7         exit(0);        
8     }
9     if ( (retval = pipe(pipeout)) ) {
10         WriteMess("output pipe creation error");
11         exit(0);        
12     }    
13     /* First fork: use child to run barcode program */
14     if ( (pid = fork()) == -1) {          /* on error exit */
15         WriteMess("child creation error");
16         exit(0);        
17     }
18     /* if child */
19     if (pid == 0) {
20         close(pipein[1]);                /* close pipe write end  */
21         dup2(pipein[0], STDIN_FILENO);   /* remap stdin to pipe read end */
22         close(pipeout[0]);
23         dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
24         execlp("barcode", "barcode", size, NULL);
25     } 
26     close(pipein[0]);                    /* close input side of input pipe */
27     write(pipein[1], argv[1], strlen(argv[1]));  /* write parameter to pipe */
28     close(pipein[1]);                    /* closing write end */
29     waitpid(pid, NULL, 0);               /* wait child completion */
30     /* Second fork: use child to run ghostscript */
31     if ( (pid = fork()) == -1) {
32         WriteMess("child creation error");
33         exit(0);
34     }
35     /* second child, convert PS to JPEG  */
36     if (pid == 0) {                     
37         close(pipeout[1]);              /* close write end */
38         dup2(pipeout[0], STDIN_FILENO); /* remap read end to stdin */
39         /* send mime type */
40         write(STDOUT_FILENO, content, strlen(content));
41         execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
42     }
43     /* still parent */
44     close(pipeout[1]); 
45     waitpid(pid, NULL, 0);
46     exit(0);
47 }