]> git.ipfire.org Git - thirdparty/sarg.git/blob - decomp.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / decomp.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2015
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27 #include "include/conf.h"
28 #include "include/defs.h"
29
30 /*!
31 Open the log file. If it is compressed, uncompress it through a pipe.
32
33 Log files compressed with gzip, bzip2 or compress are uncompressed with zcat or bzcat.
34
35 If the log file does not exist, the process terminates with an error message.
36
37 \param arq The log file to process.
38 \param pipe A variable set to \c true if the log file is opened through a pipe or set to \c false if the file is open directly.
39
40 \date 2009-09-24 - F Marchal\n This function used to uncompress .Z files in
41 place using uncompress but that required a write access to the log directory,
42 could conflict with logrotate and could leave the log file uncompressed if sarg
43 crashed. According to the documentation, zcat is capable of uncompressing .Z
44 files so it is now used.
45
46 \date 2010-05-10 - F Marchal\n
47 The function doesn't use a temporary file any more and read the compressed file through a pipe.
48 */
49 FILE *decomp(const char *arq, bool *pipe)
50 {
51 FILE *fi;
52 char cmd[1024];
53 int arqlen;
54
55 arqlen=strlen(arq);
56 if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) {
57 /*
58 TRANSLATORS: The last %s is the command used to uncompress
59 the log file such as zcat ou bzip2.
60 */
61 debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
62 if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
63 debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
64 exit(EXIT_FAILURE);
65 }
66 *pipe=true;
67 fi=popen(cmd,"r");
68 }
69 else if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) {
70 debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"bzcat");
71 if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) {
72 debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
73 exit(EXIT_FAILURE);
74 }
75 *pipe=true;
76 fi=popen(cmd,"r");
77 }
78 else if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) {
79 debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
80 if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
81 debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
82 exit(EXIT_FAILURE);
83 }
84 *pipe=true;
85 fi=popen(cmd,"r");
86 }
87 else {
88 *pipe=false;
89 fi=MY_FOPEN(arq,"r");
90 }
91 return(fi);
92 }