diff options
author | Thomas White <taw@physics.org> | 2010-09-13 10:54:34 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:26:57 +0100 |
commit | 4a235243ee6838b2745d9b3e8999d29fa76c394a (patch) | |
tree | 6440b10da74abba99edeaa2dcc72bf440bc39c68 /src/utils.c | |
parent | fcee934f899225e8e65aec86dea5106b7133c1a5 (diff) |
facetron: Add options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index a47f234f..196cda3f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -13,6 +13,8 @@ #include <string.h> #include <stdio.h> #include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> #include "utils.h" #include "image.h" @@ -442,3 +444,42 @@ ReflItemList *intersection_items(ReflItemList *i1, ReflItemList *i2) return res; } + + +char *check_prefix(char *prefix) +{ + int r; + struct stat statbuf; + char *new; + size_t len; + + /* Is "prefix" a directory? */ + r = stat(prefix, &statbuf); + if ( r != 0 ) { + /* "prefix" probably doesn't exist. This is fine - assume + * the user knows what they're doing, and that "prefix" + * suffixed with the actual filename will produce something + * sensible. */ + return prefix; + } + + if ( !S_ISDIR(statbuf.st_mode) ) { + /* Also fine, as above. */ + return prefix; + } + + /* Does the prefix end in a slash? */ + if ( prefix[strlen(prefix)-1] == '/' ) { + /* This looks sensible. */ + return prefix; + } + + STATUS("Your prefix ('%s') is a directory, but doesn't end" + " with a slash. I'm going to add it for you.\n", prefix); + STATUS("If this isn't what you want, run with --no-check-prefix.\n"); + len = strlen(prefix)+2; + new = malloc(len); + snprintf(new, len, "%s/", prefix); + free(prefix); + return new; +} |