1
0
mirror of git://projects.qi-hardware.com/ben-scans.git synced 2024-11-22 09:16:15 +02:00

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
This commit is contained in:
Werner Almesberger 2010-09-23 07:22:05 -03:00
parent 6ac06aab7c
commit b5a2d5d8ef

View File

@ -1,7 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "util.h" #include "util.h"
#include "array.h" #include "array.h"
@ -9,7 +12,10 @@
#include "face.h" #include "face.h"
struct face *read_face(const char *name) #define CACHE_DIR ".cache"
static struct face *read_file(const char *name)
{ {
FILE *file; FILE *file;
struct face *f; 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;
}