diff options
author | Thomas White <taw@bitwiz.org.uk> | 2010-12-07 18:11:18 -0700 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:08 +0100 |
commit | 5084f60ed55119d709cfd7f75fcc5823f58bc094 (patch) | |
tree | 5accca8352c77a852d6fec8b67fc6c3b810586e1 | |
parent | c85230a897b62f223951f7cc5693c6175e2d673d (diff) |
Remove use of horribly ambiguous basename() function
-rw-r--r-- | src/cubeit.c | 3 | ||||
-rw-r--r-- | src/displaywindow.c | 6 | ||||
-rw-r--r-- | src/facetron.c | 5 | ||||
-rw-r--r-- | src/reintegrate.c | 3 | ||||
-rw-r--r-- | src/utils.c | 35 | ||||
-rw-r--r-- | src/utils.h | 1 |
6 files changed, 46 insertions, 7 deletions
diff --git a/src/cubeit.c b/src/cubeit.c index c5766d6f..808e2e23 100644 --- a/src/cubeit.c +++ b/src/cubeit.c @@ -403,7 +403,7 @@ static void *get_image(void *qp) if ( qargs->config_basename ) { char *tmp; - tmp = strdup(basename(filename)); + tmp = safe_basename(filename); free(filename); filename = tmp; } @@ -414,6 +414,7 @@ static void *get_image(void *qp) pargs->cell = cell; pargs->filename = malloc(1024); snprintf(pargs->filename, 1023, "%s%s", qargs->prefix, filename); + free(filename); return pargs; } diff --git a/src/displaywindow.c b/src/displaywindow.c index b37a483a..04377ef5 100644 --- a/src/displaywindow.c +++ b/src/displaywindow.c @@ -1137,8 +1137,10 @@ DisplayWindow *displaywindow_open(const char *filename, const char *peaks, if ( filename == NULL ) { title = strdup("No file - hdfsee"); } else { - title = malloc(strlen(basename(filename))+14); - sprintf(title, "%s - hdfsee", basename(filename)); + char *bn = safe_basename(filename); + title = malloc(strlen(bn)+14); + sprintf(title, "%s - hdfsee", bn); + free(bn); } gtk_window_set_title(GTK_WINDOW(dw->window), title); free(title); diff --git a/src/facetron.c b/src/facetron.c index f16de2f7..5223e070 100644 --- a/src/facetron.c +++ b/src/facetron.c @@ -394,12 +394,13 @@ int main(int argc, char *argv[]) /* Mangle the filename now */ if ( config_basename ) { char *tmp; - tmp = strdup(basename(filename)); + tmp = safe_basename(filename); free(filename); filename = tmp; } fnamereal = malloc(1024); snprintf(fnamereal, 1023, "%s%s", prefix, filename); + free(filename); images[i].filename = fnamereal; images[i].div = beam->divergence; @@ -416,8 +417,6 @@ int main(int argc, char *argv[]) /* Get reflections from this image */ integrate_image(&images[i]); - free(filename); - progress_bar(i, n_total_patterns-1, "Loading pattern data"); } diff --git a/src/reintegrate.c b/src/reintegrate.c index e306e601..b5a388a6 100644 --- a/src/reintegrate.c +++ b/src/reintegrate.c @@ -170,7 +170,7 @@ static void *get_image(void *qp) if ( qargs->config_basename ) { char *tmp; - tmp = strdup(basename(filename)); + tmp = safe_basename(filename); free(filename); filename = tmp; } @@ -181,6 +181,7 @@ static void *get_image(void *qp) pargs->cell = cell; pargs->filename = malloc(1024); snprintf(pargs->filename, 1023, "%s%s", qargs->prefix, filename); + free(filename); return pargs; } diff --git a/src/utils.c b/src/utils.c index 99a2dd75..e8239994 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,6 +9,7 @@ * */ +#include <libgen.h> #include <math.h> #include <string.h> #include <stdio.h> @@ -485,3 +486,37 @@ char *check_prefix(char *prefix) free(prefix); return new; } + + +char *safe_basename(const char *in) +{ + int i; + char *cpy; + char *res; + + cpy = strdup(in); + + /* Get rid of any trailing slashes */ + for ( i=strlen(cpy)-1; i>0; i-- ) { + if ( cpy[i] == '/' ) { + cpy[i] = '\0'; + } else { + break; + } + } + + /* Find the base name */ + for ( i=strlen(cpy)-1; i>=0; i-- ) { + if ( cpy[i] == '/' ) { + i++; + break; + } + } + + res = strdup(cpy+i); + /* If we didn't find a previous slash, i==0 so res==cpy */ + + free(cpy); + + return res; +} diff --git a/src/utils.h b/src/utils.h index 1fbd5e4e..29e2aeda 100644 --- a/src/utils.h +++ b/src/utils.h @@ -223,6 +223,7 @@ extern ReflItemList *intersection_items(ReflItemList *i1, ReflItemList *i2); /* ------------------------------ File handling ----------------------------- */ extern char *check_prefix(char *prefix); +extern char *safe_basename(const char *in); #endif /* UTILS_H */ |