From b5a2d5d8ef83c45399fe912555147beaf10631ea Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 23 Sep 2010 07:22:05 -0300 Subject: [PATCH] Files can now be specified as HTTP/HTTPS URLs. - solidify/face.c (read_face): moved code into new function read_file - solidify/face.c (read_face): added wrapper for cached wget of http: and https: URLs --- solidify/face.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/solidify/face.c b/solidify/face.c index 8193005..20a2e01 100644 --- a/solidify/face.c +++ b/solidify/face.c @@ -1,7 +1,10 @@ #include #include +#include #include #include +#include +#include #include "util.h" #include "array.h" @@ -9,7 +12,10 @@ #include "face.h" -struct face *read_face(const char *name) +#define CACHE_DIR ".cache" + + +static struct face *read_file(const char *name) { FILE *file; struct face *f; @@ -68,3 +74,47 @@ struct face *read_face(const char *name) } +struct face *read_face(const char *name) +{ + const char *p; + int cwd; + struct face *face; + + if (strncmp(name, "http:", 5) && strncmp(name, "https:", 6)) + return read_file(name); + p = strrchr(name, '/'); + if (!p || !p[1]) { + fprintf(stderr, "malformed URL: \"%s\"\n", name); + exit(1); + } + cwd = open(".", O_RDONLY); + if (cwd < 0) { + perror("."); + exit(1); + } + if (chdir(CACHE_DIR) < 0) { + perror(CACHE_DIR); + exit(1); + } + if (access(p+1, R_OK) < 0) { + char tmp[1000]; /* @@@ enough */ + int res; + + sprintf(tmp, "wget '%s'", name); + res = system(tmp); + if (res < 0) { + perror("system"); + exit(1); + } + if (!WIFEXITED(res) || WEXITSTATUS(res)) { + fprintf(stderr, "%s: status %d\n", tmp, res); + exit(1); + } + } + face = read_file(p+1); + if (fchdir(cwd) < 0) { + perror("fchdir"); + exit(1); + } + return face; +}