]> granicus.if.org Git - yasm/commitdiff
Fix #118: Don't use getcwd(NULL, 0), as it's platform-specific behavior.
authorPeter Johnson <peter@tortall.net>
Tue, 16 Oct 2007 07:40:21 +0000 (07:40 -0000)
committerPeter Johnson <peter@tortall.net>
Tue, 16 Oct 2007 07:40:21 +0000 (07:40 -0000)
Instead write our own yasm__getcwd() which retries getcwd() with
increasing buffer sizes until the path fits (as the initial size is 1024,
in basically all cases it'll succeed on the first try).

Reported by: bird-yasm@anduin.net

svn path=/trunk/yasm/; revision=2004

libyasm/file.c
libyasm/file.h
modules/dbgfmts/dwarf2/dwarf2-info.c

index 48cb6f25c851163ddc9ce0fbd5a09b7d4737ee97..a231d89da421bac590ec7b2ff03a2acf9af31b59 100644 (file)
@@ -35,6 +35,7 @@
 #endif
 
 #include <ctype.h>
+#include <errno.h>
 
 #include "errwarn.h"
 #include "file.h"
@@ -226,6 +227,26 @@ yasm__splitpath_win(const char *path, /*@out@*/ const char **tail)
     return s-path+1;
 }
 
+char *
+yasm__getcwd(void)
+{
+    char *buf;
+    size_t size;
+
+    size = 1024;
+    buf = yasm_xmalloc(size);
+    while (getcwd(buf, size) == NULL) {
+        if (errno != ERANGE) {
+            yasm__fatal(N_("could not determine current working directory"));
+            yasm_xfree(buf);
+            return NULL;
+        }
+        size *= 2;
+        buf = yasm_xrealloc(buf, size);
+    }
+    return buf;
+}
+
 /* FIXME: dumb way for now */
 char *
 yasm__abspath_unix(const char *path)
@@ -233,14 +254,14 @@ yasm__abspath_unix(const char *path)
     char *curdir, *abspath;
     static const char pathsep[2] = "/";
 
-    curdir = getcwd(NULL, 0);
+    curdir = yasm__getcwd();
 
     abspath = yasm_xmalloc(strlen(curdir) + strlen(path) + 2);
     strcpy(abspath, curdir);
     strcat(abspath, pathsep);
     strcat(abspath, path);
 
-    free(curdir);
+    yasm_xfree(curdir);
 
     return abspath;
 }
@@ -252,14 +273,14 @@ yasm__abspath_win(const char *path)
     char *curdir, *abspath, *ch;
     static const char pathsep[2] = "\\";
 
-    curdir = getcwd(NULL, 0);
+    curdir = yasm__getcwd();
 
     abspath = yasm_xmalloc(strlen(curdir) + strlen(path) + 2);
     strcpy(abspath, curdir);
     strcat(abspath, pathsep);
     strcat(abspath, path);
 
-    free(curdir);
+    yasm_xfree(curdir);
 
     /* Replace all / with \ */
     ch = abspath;
index 21676cdd1fad2c8ad1a991394dc536ff9b71dfb8..4a1cd00554e94b14d2b193f2e6be61ec6a3cfb88 100644 (file)
@@ -115,6 +115,12 @@ size_t yasm__splitpath_win(const char *path, /*@out@*/ const char **tail);
 # endif
 #endif
 
+/** Get the current working directory.
+ * \internal
+ * \return Current working directory pathname (newly allocated).
+ */
+/*@only@*/ char *yasm__getcwd(void);
+
 /** Convert a UNIX relative or absolute pathname into an absolute pathname.
  * \internal
  * \param path  pathname
index 9c3d52a5477e00c0c332d5056133df1c87829b27..b6b45eaaf53b9140bc2cdf90b377eb64f0bf77f5 100644 (file)
 #include <util.h>
 /*@unused@*/ RCSID("$Id$");
 
-/* Need either unistd.h or direct.h (on Windows) to prototype getcwd() */
-#if defined(HAVE_UNISTD_H)
-#include <unistd.h>
-#elif defined(HAVE_DIRECT_H)
-#include <direct.h>
-#endif
-
 #include <libyasm.h>
 
 #include "dwarf2-dbgfmt.h"
@@ -337,9 +330,9 @@ yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line,
 
     /* compile directory (current working directory) */
     abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_comp_dir, DW_FORM_string);
-    buf = getcwd(NULL, 0);
+    buf = yasm__getcwd();
     dwarf2_append_str(debug_info, buf);
-    free(buf);
+    yasm_xfree(buf);
 
     /* producer - assembler name */
     abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_producer, DW_FORM_string);