[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Netsurf build failure: 'PATH_MAX' undeclared (patch)



Hello Guillem,

Apologies for the late reply.
Many thanks for the thorough comments on the patch. I have tried to address the
issues that you pointed out in the attached new version of the patch.

For those interested in tracking this issue, I had file a bug report upstream
with the original patch:
https://bugs.netsurf-browser.org/mantis/view.php?if=2824
If there are no further comments, I will upload the new version of the patch to
that bug report.

Best regards,
João
-- netsurf-3.10.orig/libnsutils/src/time.c
+++ netsurf-3.10/libnsutils/src/time.c
@@ -16,11 +16,11 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__)
+#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__) || defined(__GNU__)
 #include <time.h>
 #elif defined(__riscos)
 #include <oslib/os.h>
-#elif defined(__MACH__)
+#elif defined(__MACH__) && defined(__APPLE__)
 #include <mach/mach.h>
 #include <mach/clock.h>
 #include <mach/mach_time.h>
@@ -41,7 +41,7 @@ nsuerror nsu_getmonotonic_ms(uint64_t *c
     uint64_t current;
     static uint64_t prev = 0; /* previous time so we never go backwards */
 
-#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__)
+#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__) || defined(__GNU__)
     struct timespec tp;
 
     clock_gettime(CLOCK_MONOTONIC, &tp);
@@ -51,7 +51,7 @@ nsuerror nsu_getmonotonic_ms(uint64_t *c
 
     time = os_read_monotonic_time();
     current = time * 10;
-#elif defined(__MACH__)
+#elif defined(__MACH__) && defined(__APPLE__)
     clock_serv_t cclock;
     mach_timespec_t mts;
 
--- netsurf-3.10.orig/netsurf/frontends/framebuffer/fetch.c
+++ netsurf-3.10/netsurf/frontends/framebuffer/fetch.c
@@ -48,13 +48,16 @@
  */
 static nsurl *get_resource_url(const char *path)
 {
-	char buf[PATH_MAX];
+	char *buf = NULL;
 	nsurl *url = NULL;
 
 	if (strcmp(path, "favicon.ico") == 0)
 		path = "favicon.png";
 
-	netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url);
+	buf = filepath_find(respaths, path);
+	netsurf_path_to_nsurl(buf, &url);
+ 
+	free(buf);
 
 	return url;
 }
--- netsurf-3.10.orig/netsurf/frontends/framebuffer/font_freetype.c
+++ netsurf-3.10/netsurf/frontends/framebuffer/font_freetype.c
@@ -120,15 +120,16 @@ fb_new_face(const char *option, const ch
         fb_faceid_t *newf;
         FT_Error error;
         FT_Face aface;
-	char buf[PATH_MAX];
+	char *buf = NULL;
 
         newf = calloc(1, sizeof(fb_faceid_t));
 
         if (option != NULL) {
                 newf->fontfile = strdup(option);
         } else {
-		filepath_sfind(respaths, buf, fontname);
+		buf = filepath_find(respaths, fontname);
                 newf->fontfile = strdup(buf);
+		free(buf);
         }
 
         error = FTC_Manager_LookupFace(ft_cmanager, (FTC_FaceID)newf, &aface);
--- netsurf-3.10.orig/netsurf/frontends/gtk/fetch.c
+++ netsurf-3.10/netsurf/frontends/gtk/fetch.c
@@ -249,14 +249,16 @@ const char *fetch_filetype(const char *u
 
 static nsurl *nsgtk_get_resource_url(const char *path)
 {
-	char buf[PATH_MAX];
+	char *buf = NULL;
 	nsurl *url = NULL;
 
 	/* favicon.ico -> favicon.png */
 	if (strcmp(path, "favicon.ico") == 0) {
 		nsurl_create("resource:favicon.png", &url);
 	} else {
-		netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url);
+		buf = filepath_find(respaths, path);
+		netsurf_path_to_nsurl(buf, &url);
+		free(buf);
 	}
 
 	return url;
--- netsurf-3.10.orig/netsurf/frontends/gtk/gui.c
+++ netsurf-3.10/netsurf/frontends/gtk/gui.c
@@ -335,8 +335,8 @@ static nserror nsgtk_add_named_icons_to_
  */
 static nserror nsgtk_init(int argc, char** argv, char **respath)
 {
-	char buf[PATH_MAX];
-	char *resource_filename;
+	char *buf = NULL;
+	char *resource_filename = NULL;
 	char *addr = NULL;
 	nsurl *url;
 	nserror res;
@@ -407,8 +407,9 @@ static nserror nsgtk_init(int argc, char
 	browser_set_dpi(gdk_screen_get_resolution(gdk_screen_get_default()));
 	NSLOG(netsurf, INFO, "Set CSS DPI to %d", browser_get_dpi());
 
-	filepath_sfinddef(respath, buf, "mime.types", "/etc/");
+	buf = filepath_sfinddef(respath, "mime.types", "/etc/");
 	gtk_fetch_filetype_init(buf);
+	free(buf);
 
 	save_complete_init();
 
--- netsurf-3.10.orig/netsurf/frontends/monkey/fetch.c
+++ netsurf-3.10/netsurf/frontends/monkey/fetch.c
@@ -36,10 +36,12 @@ extern char **respaths;
 
 static nsurl *gui_get_resource_url(const char *path)
 {
-	char buf[PATH_MAX];
+	char *buf = NULL;
 	nsurl *url = NULL;
 
-	netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url);
+	buf = filepath_find(respaths, path);
+	netsurf_path_to_nsurl(buf, &url);
+	free(buf);
 
 	return url;
 }
--- netsurf-3.10.orig/netsurf/frontends/monkey/main.c
+++ netsurf-3.10/netsurf/frontends/monkey/main.c
@@ -379,7 +379,7 @@ main(int argc, char **argv)
 {
 	char *messages;
 	char *options;
-	char buf[PATH_MAX];
+	char *buf = NULL;
 	nserror ret;
 	struct netsurf_table monkey_table = {
 		.misc = &monkey_misc_table,
@@ -441,8 +441,9 @@ main(int argc, char **argv)
 		die("NetSurf failed to initialise");
 	}
 
-	filepath_sfinddef(respaths, buf, "mime.types", "/etc/");
+	buf = filepath_sfinddef(respaths, "mime.types", "/etc/");
 	monkey_fetch_filetype_init(buf);
+	free(buf);
 
 	urldb_load(nsoption_charp(url_file));
 	urldb_load_cookies(nsoption_charp(cookie_file));
--- netsurf-3.10.orig/netsurf/frontends/windows/fetch.c
+++ netsurf-3.10/netsurf/frontends/windows/fetch.c
@@ -76,10 +76,12 @@ static const char *fetch_filetype(const
  */
 static nsurl *nsw32_get_resource_url(const char *path)
 {
-	char buf[PATH_MAX];
+	char *buf = NULL;
 	nsurl *url = NULL;
 
-	netsurf_path_to_nsurl(filepath_sfind(G_resource_pathv, buf, path), &url);
+	buf = filepath_find(G_resource_pathv, path);
+	netsurf_path_to_nsurl(buf, &url);
+	free(buf);
 
 	return url;
 }
--- netsurf-3.10.orig/netsurf/frontends/windows/main.c
+++ netsurf-3.10/netsurf/frontends/windows/main.c
@@ -171,12 +171,13 @@ static nserror set_defaults(struct nsopt
 	DWORD buf_bytes_size = sizeof(TCHAR) * buf_tchar_size;
 	char *ptr = NULL;
 	char *buf;
+	char *buftest;
 	char *fname;
 	HRESULT hres;
 	char dldir[] = "Downloads";
 
 	buf = malloc(buf_bytes_size);
-	if (buf== NULL) {
+	if (buf == NULL) {
 		return NSERROR_NOMEM;
 	}
 	buf[0] = '\0';
@@ -191,8 +192,9 @@ static nserror set_defaults(struct nsopt
 	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) {
+		free(buf);
+		buf = filepath_find(G_resource_pathv, "ca-bundle.crt");
+		if (buf != NULL) {
 			nsoption_setnull_charp(ca_bundle, strdup(buf));
 		}
 	}
@@ -204,6 +206,12 @@ static nserror set_defaults(struct nsopt
 	 * not available so use the obsolete method of user prodile
 	 * with downloads suffixed
 	 */
+	buftest = realloc(buf, buf_bytes_size);
+	if (buftest == NULL) {
+		free(buf);
+		return NSERROR_BAD_SIZE;
+	}
+	buf = buftest
 	buf[0] = '\0';
 
 	hres = SHGetFolderPath(NULL,
--- netsurf-3.10.orig/netsurf/utils/Makefile
+++ netsurf-3.10/netsurf/utils/Makefile
@@ -24,3 +24,6 @@ S_UTILS := \
 	utils.c
 
 S_UTILS := $(addprefix utils/,$(S_UTILS))
+
+#This is needed by asprintf type of functions in utils/filepath.c
+CFLAGS += -D_GNU_SOURCE
--- netsurf-3.10.orig/netsurf/utils/filepath.c
+++ netsurf-3.10/netsurf/utils/filepath.c
@@ -42,27 +42,18 @@
 #define MAX_RESPATH 128
 
 /* exported interface documented in filepath.h */
-char *filepath_vsfindfile(char *str, const char *format, va_list ap)
+char *filepath_vsfindfile(const char *format, va_list ap)
 {
 	char *realpathname;
 	char *pathname;
 	int len;
 
-	pathname = malloc(PATH_MAX);
-	if (pathname == NULL)
-		return NULL; /* unable to allocate memory */
-
-	len = vsnprintf(pathname, PATH_MAX, format, ap);
-
-	if ((len < 0) || (len >= PATH_MAX)) {
-		/* error or output exceeded PATH_MAX length so
-		 * operation is doomed to fail.
-		 */
-		free(pathname);
-		return NULL;
-	}
+	len = vasprintf(&pathname, format, ap);
+
+        if (len < 0) 
+		return NULL; /* error in vasprintf */
 
-	realpathname = realpath(pathname, str);
+	realpathname = realpath(pathname, NULL);
 
 	free(pathname);
 
@@ -70,6 +61,7 @@ char *filepath_vsfindfile(char *str, con
 		/* sucessfully expanded pathname */
 		if (access(realpathname, R_OK) != 0) {
 			/* unable to read the file */
+			free(realpathname);
 			return NULL;
 		}
 	}
@@ -79,13 +71,13 @@ char *filepath_vsfindfile(char *str, con
 
 
 /* exported interface documented in filepath.h */
-char *filepath_sfindfile(char *str, const char *format, ...)
+char *filepath_findfile(const char *format, ...)
 {
 	va_list ap;
 	char *ret;
 
 	va_start(ap, format);
-	ret = filepath_vsfindfile(str, format, ap);
+	ret = filepath_vsfindfile(format, ap);
 	va_end(ap);
 
 	return ret;
@@ -93,28 +85,17 @@ char *filepath_sfindfile(char *str, cons
 
 
 /* exported interface documented in filepath.h */
-char *filepath_findfile(const char *format, ...)
-{
-	char *ret;
-	va_list ap;
-
-	va_start(ap, format);
-	ret = filepath_vsfindfile(NULL, format, ap);
-	va_end(ap);
-
-	return ret;
-}
-
-/* exported interface documented in filepath.h */
-char *filepath_sfind(char **respathv, char *filepath, const char *filename)
+char *filepath_find(char **respathv, const char *filename)
 {
 	int respathc = 0;
+	char *filepath = NULL;
 
 	if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL))
 		return NULL;
 
 	while (respathv[respathc] != NULL) {
-		if (filepath_sfindfile(filepath, "%s/%s", respathv[respathc], filename) != NULL) {
+		filepath = filepath_findfile("%s/%s", respathv[respathc], filename);
+		if (filepath != NULL) {
 			return filepath;
 		}
 
@@ -126,55 +107,33 @@ char *filepath_sfind(char **respathv, ch
 
 
 /* exported interface documented in filepath.h */
-char *filepath_find(char **respathv, const char *filename)
-{
-	char *ret;
-	char *filepath;
-
-	if ((respathv == NULL) || (respathv[0] == NULL))
-		return NULL;
-
-	filepath = malloc(PATH_MAX);
-	if (filepath == NULL)
-		return NULL;
-
-	ret = filepath_sfind(respathv, filepath, filename);
-
-	if (ret == NULL)
-		free(filepath);
-
-	return ret;
-}
-
-
-/* exported interface documented in filepath.h */
 char *
 filepath_sfinddef(char **respathv,
-		  char *filepath,
 		  const char *filename,
 		  const char *def)
 {
-	char t[PATH_MAX];
+	char *t = NULL;
 	char *ret;
 
-	if ((respathv == NULL) || (respathv[0] == NULL) || (filepath == NULL))
+	if ((respathv == NULL) || (respathv[0] == NULL))
 		return NULL;
 
-	ret = filepath_sfind(respathv, filepath, filename);
+	ret = filepath_find(respathv, filename);
 
 	if ((ret == NULL) && (def != NULL)) {
 		/* search failed, return the path specified */
-		ret = filepath;
 		if (def[0] == '~') {
-			snprintf(t, PATH_MAX, "%s/%s/%s", getenv("HOME"), def + 1, filename);
+			asprintf(&t, "%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);
+			asprintf(&t, "%s/%s", def, filename);
 		}
-
+		ret = realpath(t, NULL);
+		if (ret == NULL)
+			ret = t;
+		else 
+			free(t);
 	}
+
 	return ret;
 }
 
--- netsurf-3.10.orig/netsurf/utils/filepath.h
+++ netsurf-3.10/netsurf/utils/filepath.h
@@ -35,14 +35,12 @@
  * normalised path is placed in str and a pointer to str returned
  * otherwise NULL is returned. The string in str is always modified.
  *
- * @param str A buffer to contain the normalised file name must be at
- *            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.
  */
-char *filepath_vsfindfile(char *str, const char *format, va_list ap);
+char *filepath_vsfindfile(const char *format, va_list ap);
 
 
 /**
@@ -50,15 +48,6 @@ char *filepath_vsfindfile(char *str, con
  *
  * Similar to vsfindfile but takes variadic (printf like) parameters
  */
-char *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.
- */
 char *filepath_findfile(const char *format, ...);
 
 
@@ -70,19 +59,9 @@ char *filepath_findfile(const char *form
  * 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.
  */
-char *filepath_sfind(char **respathv, char *filepath, const char *filename);
-
-
-/**
- * Searches an array of resource paths for a file.
- *
- * 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);
 
 
@@ -95,12 +74,11 @@ char *filepath_find(char **respathv, con
  * 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
  */
-char *filepath_sfinddef(char **respathv, char *filepath, const char *filename,
+char *filepath_sfinddef(char **respathv, const char *filename,
 		const char *def);
 
 

Reply to: