Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / BarCode.c
1 /* BarCode.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 #include"macros.h"
48
49 /* Program begin */
50 int main(int argc, char *argv[], char *envp[])
51 {
52 /*
53  * Variables definition  
54  */
55     FILE *pipe[4];
56     FILE *pipein;
57     char *cmd_string[4]={
58         "pnmtopng",
59         "pnmmargin -white 10",
60         "pnmcrop",
61         "gs -sDEVICE=ppmraw -sOutputFile=- -sNOPAUSE -q - -c showpage -c quit"
62     };  
63     char content[]="Content-type: image/png\n\n";
64     int i;
65     /* write mime-type to stdout */ 
66     write(STDOUT_FILENO, content, strlen(content));
67     /* execute chain of command */
68     for (i=0; i<4; i++) {
69         pipe[i] = popen(cmd_string[i], "w");
70         dup2(fileno(pipe[i]), STDOUT_FILENO); 
71     }
72     /* create barcode (in PS) */
73     pipein = popen("barcode", "w");
74     /* send barcode string to barcode program */
75     write(fileno(pipein), argv[1], strlen(argv[1]));
76     pclose(pipein);
77     /* close all pipes (in reverse order) */
78     for (i=4; i==0; i--) {
79         pclose((pipe[i]));
80     }
81     exit(0);
82 }