From 9105f3f56940fa6a45c877ade6f8f86cbde0a88f Mon Sep 17 00:00:00 2001 From: Angel Ortega Date: Mon, 21 Jan 2008 08:59:00 +0100 Subject: [PATCH] Use const in support.c where possible. --- ahxm.h | 12 ++++++------ support.c | 43 ++++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/ahxm.h b/ahxm.h index 413100d..a395b7b 100644 --- a/ahxm.h +++ b/ahxm.h @@ -366,9 +366,9 @@ int compile_ahs(char * file); /* in support.c */ -void libpath_add(char * path, int strip); -FILE * libpath_fopen(char * filename, char * mode); -char * libpath_locate(char * filename); -void transconv_add(char * from, char * to, char * convcmd); -char * transconv_pipe(char * cmd, char * ext, char * dir); -char * transconv(char * file, char * ext, char * dir); +void libpath_add(const char * path, int strip); +FILE * libpath_fopen(const char * filename, const char * mode); +char * libpath_locate(const char * filename); +void transconv_add(const char * from, const char * to, const char * convcmd); +char * transconv_pipe(const char * cmd, const char * ext, const char * dir); +char * transconv(const char * file, const char * ext, const char * dir); diff --git a/support.c b/support.c index b92610d..16eb8a2 100644 --- a/support.c +++ b/support.c @@ -52,9 +52,9 @@ int trace = 0; /* transparent converters */ struct transconv { - char *from; /* extension from */ - char *to; /* extension to */ - char *convcmd; /* sprintf() format for converting command */ + const char *from; /* extension from */ + const char *to; /* extension to */ + const char *convcmd; /* sprintf() format for converting command */ }; static struct transconv *transconvs = NULL; @@ -74,10 +74,11 @@ static int n_transconvs = 0; * The last part of the path is stripped before being stored, * and duplication is avoided. */ -void libpath_add(char *path, int strip) +void libpath_add(const char *path, int strip) { int n; char *ptr; + char *p; /* if path starts with ~, set it to $HOME */ if (*path == '~') { @@ -94,16 +95,16 @@ void libpath_add(char *path, int strip) strcat(new, "/"); strcat(new, path + 1); - path = new; + p = new; } else { /* just duplicate */ - path = strdup(path); + p = strdup(path); } /* if no directory path remains, abort */ - if ((ptr = strrchr(path, '/')) == NULL) { - free(path); + if ((ptr = strrchr(p, '/')) == NULL) { + free(p); return; } @@ -113,9 +114,9 @@ void libpath_add(char *path, int strip) /* now try to find if that path is already stored */ for (n = 0; n < n_library_paths; n++) { - if (strcmp(path, library_path[n]) == 0) { + if (strcmp(p, library_path[n]) == 0) { /* found; free and return */ - free(path); + free(p); return; } } @@ -125,7 +126,7 @@ void libpath_add(char *path, int strip) library_path = (char **) realloc(library_path, n_library_paths * sizeof(char *)); /* store */ - library_path[n_library_paths - 1] = path; + library_path[n_library_paths - 1] = p; } @@ -140,7 +141,7 @@ void libpath_add(char *path, int strip) * end of the list is reached. Whenever a file * is successfully opened, its path is also stored. */ -FILE *libpath_fopen(char *filename, char *mode) +FILE *libpath_fopen(const char *filename, const char *mode) { int n; FILE *f = NULL; @@ -179,7 +180,7 @@ FILE *libpath_fopen(char *filename, char *mode) * buffer containing the real path of the file is returned, or * NULL otherwise. */ -char *libpath_locate(char *filename) +char *libpath_locate(const char *filename) { FILE *f; @@ -193,7 +194,7 @@ char *libpath_locate(char *filename) /** transparent conversions **/ -void transconv_add(char *from, char *to, char *convcmd) +void transconv_add(const char *from, const char *to, const char *convcmd) /* adds a converter */ { struct transconv *t; @@ -208,7 +209,7 @@ void transconv_add(char *from, char *to, char *convcmd) } -static char *transconv_sha_file(char *file, char *ext) +static char *transconv_sha_file(const char *file, const char *ext) /* builds a unique cache file basename using a SHA1 hash */ { static char c_file[64]; @@ -217,8 +218,8 @@ static char *transconv_sha_file(char *file, char *ext) int n; SHA1_Init(&c); - SHA1_Update(&c, file, strlen(file)); - SHA1_Update(&c, ext, strlen(ext)); + SHA1_Update(&c, (char *)file, strlen(file)); + SHA1_Update(&c, (char *)ext, strlen(ext)); SHA1_Final(sha1, &c); for (n = 0; n < sizeof(sha1); n++) { @@ -234,7 +235,7 @@ static char *transconv_sha_file(char *file, char *ext) } -static char *transconv_unique_file(char *file, char *ext, char *dir) +static char *transconv_unique_file(const char *file, const char *ext, const char *dir) /* builds a unique cache file name with complete path */ { static char tmp[2048]; @@ -259,7 +260,7 @@ static char *transconv_unique_file(char *file, char *ext, char *dir) } -char *transconv_pipe(char *cmd, char *ext, char *dir) +char *transconv_pipe(const char *cmd, const char *ext, const char *dir) /* executes cmd as a pipe */ { char *c_file = transconv_unique_file(cmd, ext, dir); @@ -281,7 +282,7 @@ char *transconv_pipe(char *cmd, char *ext, char *dir) } -char *transconv(char *file, char *ext, char *dir) +char *transconv(const char *file, const char *ext, const char *dir) /* converts using the transparent converters and the cache, if needed */ { char *this_ext; @@ -295,7 +296,7 @@ char *transconv(char *file, char *ext, char *dir) /* if it's already the desired type, do nothing */ if (strcmp(ext, this_ext) == 0) - return file; + return (char *)file; /* get a unique name */ c_file = transconv_unique_file(file, ext, dir); -- 2.11.4.GIT