@@ -256,6 +256,44 @@ static void zconf_endhelp(void)
BEGIN(INITIAL);
}
+static const struct type_handler {
+ const char *suffix;
+ const char *command;
+} type_handlers[] = {
+ {
+ .suffix = ".gz",
+ .command = "zcat",
+ },
+ {
+ .suffix = ".bz2",
+ .command = "bzcat",
+ },
+ /* Whatever other algorithms you like */
+ {}
+};
+
+static const struct type_handler *get_type_handler(const char *name)
+{
+ char *p = rindex(name, '.');
+ if (p) {
+ const struct type_handler *ops = type_handlers;
+ for (ops = type_handlers; ops->suffix; ops++) {
+ if (!strcasecmp(ops->suffix, p))
+ break;
+ }
+ if (!ops->suffix)
+ return NULL;
+ return ops;
+ }
+ return NULL;
+}
+
+static FILE *zconf_popen(const char *command, const char *name)
+{
+ char cmdbuf[PATH_MAX + strlen(command) + 2];
+ snprintf(cmdbuf, sizeof(cmdbuf), "%s %s", command, name);
+ return popen(cmdbuf, "r");
+}
/*
* Try to open specified file with following names:
@@ -267,15 +305,23 @@ static void zconf_endhelp(void)
*/
FILE *zconf_fopen(const char *name)
{
+ const struct type_handler *handler = get_type_handler(name);
char *env, fullname[PATH_MAX+1];
FILE *f;
- f = fopen(name, "r");
+ if (handler)
+ f = zconf_popen(handler->command, name);
+ else
+ f = fopen(name, "r");
+
if (!f && name != NULL && name[0] != '/') {
env = getenv(SRCTREE);
if (env) {
sprintf(fullname, "%s/%s", env, name);
- f = fopen(fullname, "r");
+ if (handler)
+ f = zconf_popen(handler->command, fullname);
+ else
+ f = fopen(fullname, "r");
}
}
return f;