2 * decompress.c: decompression abstraction layer
4 * Copyright (C) 2007, 2008 Colin Watson.
6 * This file is part of man-db.
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #endif /* HAVE_CONFIG_H */
30 #include <sys/types.h>
37 #endif /* HAVE_LIBZ */
39 #include "xvasprintf.h"
41 #include "manconfig.h"
44 #include "decompress.h"
49 static void decompress_zlib (void *data ATTRIBUTE_UNUSED)
53 zlibfile = gzdopen (dup (STDIN_FILENO), "r");
59 int r = gzread (zlibfile, buffer, 4096);
62 if (fwrite (buffer, 1, (size_t) r, stdout) < (size_t) r)
70 #endif /* HAVE_LIBZ */
72 extern man_sandbox *sandbox;
74 pipeline *decompress_open (const char *filename)
81 #endif /* HAVE_LIBZ */
83 struct compression *comp;
85 if (stat (filename, &st) < 0 || S_ISDIR (st.st_mode))
89 filename_len = strlen (filename);
90 if (filename_len > 3 && STREQ (filename + filename_len - 3, ".gz")) {
91 cmd = pipecmd_new_function ("zcat", &decompress_zlib, NULL,
93 pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
94 p = pipeline_new_commands (cmd, NULL);
97 #endif /* HAVE_LIBZ */
99 ext = strrchr (filename, '.');
103 for (comp = comp_list; comp->ext; ++comp) {
104 if (!STREQ (comp->ext, ext))
107 cmd = pipecmd_new_argstr (comp->prog);
108 pipecmd_pre_exec (cmd, sandbox_load, sandbox_free,
110 p = pipeline_new_commands (cmd, NULL);
117 ext = strstr (filename, ".Z/");
119 cmd = pipecmd_new_argstr (GUNZIP);
120 pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
121 p = pipeline_new_commands (cmd, NULL);
129 pipeline_want_infile (p, filename);
130 pipeline_want_out (p, -1);
134 pipeline *decompress_fdopen (int fd)
139 #endif /* HAVE_LIBZ */
142 cmd = pipecmd_new_function ("zcat", &decompress_zlib, NULL, NULL);
143 pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
144 p = pipeline_new_commands (cmd, NULL);
145 #else /* HAVE_LIBZ */
147 #endif /* HAVE_LIBZ */
149 pipeline_want_in (p, fd);
150 pipeline_want_out (p, -1);