3 * Copyright (C) 2002 Simone Piccardi
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.
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.
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.
19 /****************************************************************
22 * CGI for barcode generation
24 * Author: Simone Piccardi
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
32 * $Id: BarCodePage.c,v 1.1 2002/06/30 13:26:17 piccardi Exp $
34 ****************************************************************/
36 * Include needed headers
38 #include <sys/types.h> /* predefined types */
39 #include <sys/stat.h> /* stat deinitiions */
40 #include <unistd.h> /* include unix standard library */
42 #include <stdio.h> /* include standard I/O library */
43 #include <stdlib.h> /* include standard library */
44 #include <string.h> /* include string library */
45 #include <wait.h> /* include wait call */
51 void WriteMess(char *mess);
55 int main(int argc, char *argv[], char *envp[])
58 * Variables definition
64 char content[]="Content-type: image/jpeg\n\n";
69 /* create two pipes to handle process communication */
70 if ( (retval = pipe(pipein)) ) {
71 WriteMess("input pipe creation error");
74 if ( (retval = pipe(pipeout)) ) {
75 WriteMess("output pipe creation error");
78 /* fork child to run barcode program */
81 WriteMess("child creation error");
87 * Child exec barcode program, that take input (string to encode)
88 * from pipein, remapped to stdin, and write the output (a PS
89 * image) to stdout, remapped to pipeout
91 close(pipein[1]); /* close output side of input pipe */
92 dup2(pipein[0], STDIN_FILENO); /* remap stdin in pipe input */
94 dup2(pipeout[1], STDOUT_FILENO); /* remap stdout in pipe output */
95 execlp("barcode", "barcode", size, NULL); //"-o", "-", NULL);
98 * Parent write string to pipe input and close it,
99 * then wait child execution and results form pipeout,
100 * then fork to convert PS to JPEG using gs
102 close(pipein[0]); /* close input side of input pipe */
103 write(pipein[1], argv[1], strlen(argv[1]));
105 waitpid(pid, NULL, 0);
106 /* Second fork use child to run ghostscript*/
109 WriteMess("child creation error");
112 /* second child, convert PS to JPEG */
114 close(pipeout[1]); /* close write end */
115 dup2(pipeout[0], STDIN_FILENO); /* remap read end to stdin */
117 write(STDOUT_FILENO, content, strlen(content));
118 execlp("gs", "gs", "-q", "-sDEVICE=jpeg", "-sOutputFile=-", "-", NULL);
122 waitpid(pid, NULL, 0);
126 * Routine to produce an HTML error message on output
128 void WriteMess(char *mess)
130 printf("Content-type: text/html\n\n");