Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / BarCodePage.c
1 /* BarCodePage.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program barcode 
22  * CGI for barcode generation
23  *
24  * Author: Simone Piccardi
25  * Jun. 2002
26  *
27  * Usage: cgi-bin for apache.
28  * Called by downloading something like:
29  * http://localhost/cgi-bin/barcode?string
30  * where string is the code to be converted
31  *
32  ****************************************************************/
33 /* 
34  * Include needed headers
35  */
36 #include <sys/types.h>   /* primitive system data types */
37 #include <sys/stat.h>    /* file characteristics constants and functions */
38 #include <unistd.h>      /* unix standard library */
39 #include <stdio.h>       /* standard I/O library */
40 #include <stdlib.h>      /* C standard library */
41 #include <string.h>      /* C strings library */
42 #include <wait.h>        /* process termination constants and functions */
43 #include <fcntl.h>       /* file control functions */
44 #include <assert.h>      /* C assertion functions */
45 #include <time.h>        /* date and time constants, types and functions */
46
47
48 #include"macros.h"
49 void WriteMess(char *mess);
50
51 /* Program begin */
52 int main(int argc, char *argv[], char *envp[])
53 {
54 /*
55  * Variables definition  
56  */
57     pid_t pid;
58     int retval;
59     int pipein[2];
60     int pipeout[2];
61     char content[]="Content-type: image/jpeg\n\n";
62     char size[]="-pA9";
63     /* 
64      * Begin
65      */
66     /* create two pipes, pipein and pipeout, to handle communication */
67     if ( (retval = pipe(pipein)) ) {
68         WriteMess("input pipe creation error");
69         exit(0);        
70     }
71     if ( (retval = pipe(pipeout)) ) {
72         WriteMess("output pipe creation error");
73         exit(0);        
74     }    
75     /* First fork: use child to run barcode program */
76     if ( (pid = fork()) == -1 ) {
77         WriteMess("child creation error");
78         exit(0);
79     }
80     /* if child */
81     if (pid == 0) {
82         /*
83          * Child exec barcode program, that take input (string to encode)
84          * from pipein, remapped to stdin, and write the output (a PS
85          * image) to stdout, remapped to pipeout 
86          */
87         close(pipein[1]);                /* close output side of input pipe */
88         dup2(pipein[0], STDIN_FILENO);   /* remap stdin in pipe input */
89         close(pipeout[0]);
90         dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
91         execlp("barcode", "barcode", size, NULL); 
92     } 
93     /*
94      * Parent write string to pipe input and close it, 
95      * then wait child execution and results form pipeout, 
96      * then fork to convert PS to JPEG using gs
97      */
98     close(pipein[0]);        /* close input side of input pipe */
99     write(pipein[1], argv[1], strlen(argv[1]));
100     close(pipein[1]);
101     waitpid(pid, NULL, 0);
102     /* Second fork: use child to run ghostscript*/
103     if ( (pid = fork()) == -1) {
104         WriteMess("child creation error");
105         exit(0);
106     }
107     /* second child, convert PS to JPEG */
108     if (pid == 0) {
109         close(pipeout[1]);              /* close write end */
110         dup2(pipeout[0], STDIN_FILENO); /* remap read end to stdin */
111         /* send mime type */
112         write(STDOUT_FILENO, content, strlen(content));
113         execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
114     }
115     /* still parent */
116     close(pipeout[1]); 
117     waitpid(pid, NULL, 0);
118     exit(0);
119 }
120 /*
121  * Routine to produce an HTML error message on output 
122  */
123 void WriteMess(char *mess)
124 {
125     printf("Content-type: text/html\n\n");
126     perror(mess);
127     printf("<br>\n");
128 }