From: Olivier Valentin <valen...@free.fr> --- frontends/framebuffer/fetch.c | 3 +- frontends/framebuffer/font_freetype.c | 4 +- frontends/framebuffer/gui.c | 4 +- frontends/gtk/fetch.c | 3 +- frontends/gtk/gui.c | 2 +- frontends/gtk/resources.c | 3 +- frontends/monkey/fetch.c | 3 +- frontends/monkey/main.c | 4 +- frontends/windows/fetch.c | 3 +- frontends/windows/main.c | 11 ++-- utils/filepath.c | 75 +++++++++++++++------------ utils/filepath.h | 35 ++++++------- 12 files changed, 78 insertions(+), 72 deletions(-)
diff --git a/frontends/framebuffer/fetch.c b/frontends/framebuffer/fetch.c index 23cbb4f21..d58a82205 100644 --- a/frontends/framebuffer/fetch.c +++ b/frontends/framebuffer/fetch.c @@ -54,7 +54,8 @@ static nsurl *get_resource_url(const char *path) if (strcmp(path, "favicon.ico") == 0) path = "favicon.png"; - netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url); + if (filepath_sfind(respaths, buf, path) == NSERROR_OK) + netsurf_path_to_nsurl(buf, &url); return url; } diff --git a/frontends/framebuffer/font_freetype.c b/frontends/framebuffer/font_freetype.c index 3912821f7..0c43a95ee 100644 --- a/frontends/framebuffer/font_freetype.c +++ b/frontends/framebuffer/font_freetype.c @@ -120,15 +120,13 @@ fb_new_face(const char *option, const char *resname, const char *fontname) fb_faceid_t *newf; FT_Error error; FT_Face aface; - char buf[PATH_MAX]; newf = calloc(1, sizeof(fb_faceid_t)); if (option != NULL) { newf->fontfile = strdup(option); } else { - filepath_sfind(respaths, buf, fontname); - newf->fontfile = strdup(buf); + filepath_find(respaths, fontname, &newf->fontfile); } error = FTC_Manager_LookupFace(ft_cmanager, (FTC_FaceID)newf, &aface); diff --git a/frontends/framebuffer/gui.c b/frontends/framebuffer/gui.c index f0a8f5e58..74768c224 100644 --- a/frontends/framebuffer/gui.c +++ b/frontends/framebuffer/gui.c @@ -2214,13 +2214,13 @@ main(int argc, char** argv) if (ret != NSERROR_OK) { die("Options failed to initialise"); } - options = filepath_find(respaths, "Choices"); + filepath_find(respaths, "Choices", &options); nsoption_read(options, nsoptions); free(options); nsoption_commandline(&argc, argv, nsoptions); /* message init */ - messages = filepath_find(respaths, "Messages"); + filepath_find(respaths, "Messages", &messages); ret = messages_add_from_file(messages); free(messages); if (ret != NSERROR_OK) { diff --git a/frontends/gtk/fetch.c b/frontends/gtk/fetch.c index d77073a63..36ee36c17 100644 --- a/frontends/gtk/fetch.c +++ b/frontends/gtk/fetch.c @@ -257,7 +257,8 @@ static nsurl *nsgtk_get_resource_url(const char *path) if (strcmp(path, "favicon.ico") == 0) { nsurl_create("resource:favicon.png", &url); } else { - netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url); + if (filepath_sfind(respaths, buf, path) == NSERROR_OK) + netsurf_path_to_nsurl(buf, &url); } return url; diff --git a/frontends/gtk/gui.c b/frontends/gtk/gui.c index 644237e09..fa0c4063b 100644 --- a/frontends/gtk/gui.c +++ b/frontends/gtk/gui.c @@ -942,7 +942,7 @@ static nserror nsgtk_setup(int argc, char** argv, char **respath) } /* Search engine sources */ - resource_filename = filepath_find(respath, "SearchEngines"); + filepath_find(respath, "SearchEngines", &resource_filename); search_web_init(resource_filename); if (resource_filename != NULL) { NSLOG(netsurf, INFO, "Using '%s' as Search Engines file", diff --git a/frontends/gtk/resources.c b/frontends/gtk/resources.c index fc17f7418..425517b53 100644 --- a/frontends/gtk/resources.c +++ b/frontends/gtk/resources.c @@ -246,8 +246,7 @@ init_resource(char **respath, struct nsgtk_resource_s *resource) #endif /* look for file on disc */ - resname = filepath_find(respath, resource->name); - if (resname != NULL) { + if (filepath_find(respath, resource->name, &resname) == NSERROR_OK) { /* found an entry on the path */ resource->path = resname; resource->type = NSGTK_RESOURCE_FILE; diff --git a/frontends/monkey/fetch.c b/frontends/monkey/fetch.c index 256d79b59..96ba28d67 100644 --- a/frontends/monkey/fetch.c +++ b/frontends/monkey/fetch.c @@ -39,7 +39,8 @@ static nsurl *gui_get_resource_url(const char *path) char buf[PATH_MAX]; nsurl *url = NULL; - netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url); + if (filepath_sfind(respaths, buf, path) == NSERROR_OK) + netsurf_path_to_nsurl(buf, &url); return url; } diff --git a/frontends/monkey/main.c b/frontends/monkey/main.c index 463f0bea6..2d6c1bd1b 100644 --- a/frontends/monkey/main.c +++ b/frontends/monkey/main.c @@ -434,12 +434,12 @@ main(int argc, char **argv) if (ret != NSERROR_OK) { die("Options failed to initialise"); } - options = filepath_find(respaths, "Choices"); + filepath_find(respaths, "Choices", &options); nsoption_read(options, nsoptions); free(options); nsoption_commandline(&argc, argv, nsoptions); - messages = filepath_find(respaths, "Messages"); + filepath_find(respaths, "Messages", &messages); ret = messages_add_from_file(messages); if (ret != NSERROR_OK) { NSLOG(netsurf, INFO, "Messages failed to load"); diff --git a/frontends/windows/fetch.c b/frontends/windows/fetch.c index 287f20f6c..cad99446b 100644 --- a/frontends/windows/fetch.c +++ b/frontends/windows/fetch.c @@ -79,7 +79,8 @@ static nsurl *nsw32_get_resource_url(const char *path) char buf[PATH_MAX]; nsurl *url = NULL; - netsurf_path_to_nsurl(filepath_sfind(G_resource_pathv, buf, path), &url); + if (filepath_sfind(G_resource_pathv, buf, path) == NSERROR_OK) + netsurf_path_to_nsurl(buf, &url); return url; } diff --git a/frontends/windows/main.c b/frontends/windows/main.c index 561ebe848..579dbc984 100644 --- a/frontends/windows/main.c +++ b/frontends/windows/main.c @@ -191,9 +191,8 @@ static nserror set_defaults(struct nsoption_s *defaults) if (res_len > 0) { nsoption_setnull_charp(ca_bundle, strdup(buf)); } else { - ptr = filepath_sfind(G_resource_pathv, buf, "ca-bundle.crt"); - if (ptr != NULL) { - nsoption_setnull_charp(ca_bundle, strdup(buf)); + if (filepath_find(G_resource_pathv, buf, "ca-bundle.crt", &ptr) == NSERROR_OK) { + nsoption_setnull_charp(ca_bundle, ptr); } } @@ -303,10 +302,8 @@ static nserror nsw32_messages_init(char **respaths) res = messages_add_from_inline(data, data_size); } else { /* Obtain path to messages */ - messages = filepath_find(respaths, "messages"); - if (messages == NULL) { - res = NSERROR_NOT_FOUND; - } else { + res = filepath_find(respaths, "messages", &messages); + if (messages == NSERROR_OK) { res = messages_add_from_file(messages); free(messages); } diff --git a/utils/filepath.c b/utils/filepath.c index b87e2bf0d..5cfcba548 100644 --- a/utils/filepath.c +++ b/utils/filepath.c @@ -42,7 +42,7 @@ #define MAX_RESPATH 128 /* exported interface documented in filepath.h */ -char *filepath_vsfindfile(char *str, const char *format, va_list ap) +nserror filepath_vsfindfile(char *str, const char *format, va_list ap) { char *realpathname; char *pathname; @@ -50,7 +50,7 @@ char *filepath_vsfindfile(char *str, const char *format, va_list ap) pathname = malloc(PATH_MAX); if (pathname == NULL) - return NULL; /* unable to allocate memory */ + return NSERROR_NOMEM; /* unable to allocate memory */ len = vsnprintf(pathname, PATH_MAX, format, ap); @@ -59,7 +59,7 @@ char *filepath_vsfindfile(char *str, const char *format, va_list ap) * operation is doomed to fail. */ free(pathname); - return NULL; + NSERROR_BAD_SIZE; } realpathname = realpath(pathname, str); @@ -70,19 +70,19 @@ char *filepath_vsfindfile(char *str, const char *format, va_list ap) /* sucessfully expanded pathname */ if (access(realpathname, R_OK) != 0) { /* unable to read the file */ - return NULL; + return NSERROR_NOT_FOUND; } } - return realpathname; + return NSERROR_OK; } /* exported interface documented in filepath.h */ -char *filepath_sfindfile(char *str, const char *format, ...) +nserror filepath_sfindfile(char *str, const char *format, ...) { va_list ap; - char *ret; + nserror ret; va_start(ap, format); ret = filepath_vsfindfile(str, format, ap); @@ -93,87 +93,96 @@ char *filepath_sfindfile(char *str, const char *format, ...) /* exported interface documented in filepath.h */ -char *filepath_findfile(const char *format, ...) +nserror filepath_findfile(char **retstr, const char *format, ...) { - char *ret; + nserror ret; va_list ap; + *retstr = malloc(PATH_MAX); + if (*retstr == NULL) + return NSERROR_NOMEM; /* unable to allocate memory */ + va_start(ap, format); - ret = filepath_vsfindfile(NULL, format, ap); + ret = filepath_vsfindfile(*retstr, format, ap); va_end(ap); + if (ret != NSERROR_OK) { + free(*retstr); + *retstr = NULL; + } + return ret; } /* exported interface documented in filepath.h */ -char *filepath_sfind(char **respathv, char *filepath, const char *filename) +nserror filepath_sfind(char **respathv, char *filepath, const char *filename) { int respathc = 0; if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) - return NULL; + return NSERROR_INVALID; while (respathv[respathc] != NULL) { - if (filepath_sfindfile(filepath, "%s/%s", respathv[respathc], filename) != NULL) { - return filepath; + if (filepath_sfindfile(filepath, "%s/%s", respathv[respathc], filename) == NSERROR_OK) { + return NSERROR_OK; } respathc++; } - return NULL; + return NSERROR_NOT_FOUND; } /* exported interface documented in filepath.h */ -char *filepath_find(char **respathv, const char *filename) +nserror filepath_find(char **respathv, const char *filename, char **retstr) { - char *ret; - char *filepath; + nserror ret; if ((respathv == NULL) || (respathv[0] == NULL)) - return NULL; + return NSERROR_INVALID; - filepath = malloc(PATH_MAX); - if (filepath == NULL) - return NULL; + *retstr = malloc(PATH_MAX); + if (*retstr == NULL) + return NSERROR_NOMEM; - ret = filepath_sfind(respathv, filepath, filename); + ret = filepath_sfind(respathv, *retstr, filename); - if (ret == NULL) - free(filepath); + if (ret == NSERROR_OK) { + free(*retstr); + *retstr = NULL; + } return ret; } /* exported interface documented in filepath.h */ -char * +nserror filepath_sfinddef(char **respathv, char *filepath, const char *filename, const char *def) { char t[PATH_MAX]; - char *ret; + nserror ret; if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL)) - return NULL; + return NSERROR_INVALID; ret = filepath_sfind(respathv, filepath, filename); - if ((ret == NULL) && (def != NULL)) { + if ((ret != NSERROR_OK) && (def != NULL)) { /* search failed, return the path specified */ - ret = filepath; + ret = NSERROR_OK; if (def[0] == '~') { snprintf(t, PATH_MAX, "%s/%s/%s", getenv("HOME"), def + 1, filename); } else { snprintf(t, PATH_MAX, "%s/%s", def, filename); } - if (realpath(t, ret) == NULL) { - strncpy(ret, t, PATH_MAX); + if (realpath(t, filepath) == NULL) { + strncpy(filepath, t, PATH_MAX); } - } return ret; } diff --git a/utils/filepath.h b/utils/filepath.h index 784264b33..e87437d90 100644 --- a/utils/filepath.h +++ b/utils/filepath.h @@ -39,10 +39,9 @@ * least PATH_MAX bytes long. * @param format A printf format for the filename. * @param ap The list of arguments for the format. - * @return A pointer to the expanded filename or NULL if the file is - * not present or accessible. + * @return NSERROR_OK on success or error code on failure. */ -char *filepath_vsfindfile(char *str, const char *format, va_list ap); +nserror filepath_vsfindfile(char *str, const char *format, va_list ap); /** @@ -50,16 +49,16 @@ char *filepath_vsfindfile(char *str, const char *format, va_list ap); * * Similar to vsfindfile but takes variadic (printf like) parameters */ -char *filepath_sfindfile(char *str, const char *format, ...); +nserror filepath_sfindfile(char *str, const char *format, ...); /** * Create a normalised file name. * * Similar to sfindfile but allocates its own storage for the - * returned string. The caller must free this sorage. + * returned string in retstr. The caller must free this storage. */ -char *filepath_findfile(const char *format, ...); +nserror filepath_findfile(char **retstr, const char *format, ...); /** @@ -69,12 +68,12 @@ char *filepath_findfile(const char *format, ...); * normalised file name of the first acessible file or NULL if no file * can be found in any of the resource paths. * - * \param respathv The resource path vector to iterate. - * \param filepath The buffer to place the result in. - * \param filename The filename of the resource to search for. - * \return A pointer to filepath if a target is found or NULL if not. + * @param respathv The resource path vector to iterate. + * @param filepath The buffer to place the result in. + * @param filename The filename of the resource to search for. + * @return NSERROR_OK on success or error code on failure. */ -char *filepath_sfind(char **respathv, char *filepath, const char *filename); +nserror filepath_sfind(char **respathv, char *filepath, const char *filename); /** @@ -83,7 +82,7 @@ char *filepath_sfind(char **respathv, char *filepath, const char *filename); * Similar to filepath_sfind except it allocates its own storage for * the returned string. The caller must free this sorage. */ -char *filepath_find(char **respathv, const char *filename); +nserror filepath_find(char **respathv, const char *filename, char **retstr); /** @@ -94,13 +93,13 @@ char *filepath_find(char **respathv, const char *filename); * fails the returned path is set to the concatination of the default * path and the filename. * - * \param respathv The resource path vector to iterate. - * \param filepath The buffer to place the result in. Must have space for PATH_MAX bytes. - * \param filename The filename of the resource to search for. - * \param def The default path to use - * \return A pointer to filepath if a target is found or the default if not + * @param respathv The resource path vector to iterate. + * @param filepath The buffer to place the result in. Must have space for PATH_MAX bytes. + * @param filename The filename of the resource to search for. + * @param def The default path to use + * @return NSERROR_OK on success or error code on failure. */ -char *filepath_sfinddef(char **respathv, char *filepath, const char *filename, +nserror filepath_sfinddef(char **respathv, char *filepath, const char *filename, const char *def); -- 2.45.2