[Resend winegcc05] Improve memory management

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

 



A few slight differences from last time

2 new files: winegcc-utils.{c,h} used by both winegcc & winewrap.

Changelog:
    Use an extendable array to store the lists of strings.

diff -u -r -N tools.3/Makefile.in tools/Makefile.in
--- tools.3/Makefile.in	2003-08-20 13:08:46.000000000 +0100
+++ tools/Makefile.in	2003-09-08 15:24:40.000000000 +0100
@@ -45,11 +45,11 @@
 bin2res: bin2res.o
 	$(CC) $(CFLAGS) -o bin2res bin2res.o $(LIBPORT)
 
-winegcc: winegcc.o
-	$(CC) $(CFLAGS) -o winegcc winegcc.o $(LIBPORT)
+winegcc: winegcc.o winegcc-utils.o
+	$(CC) $(CFLAGS) -o winegcc winegcc.o winegcc-utils.o $(LIBPORT)
 
-winewrap: winewrap.o
-	$(CC) $(CFLAGS) -o winewrap winewrap.o $(LIBPORT)
+winewrap: winewrap.o winegcc-utils.o
+	$(CC) $(CFLAGS) -o winewrap winewrap.o winegcc-utils.o $(LIBPORT)
 
 install::
 	$(MKINSTALLDIRS) $(bindir) $(mandir)/man$(prog_manext)
diff -u -r -N tools.3/winegcc.c tools/winegcc.c
--- tools.3/winegcc.c	2003-09-08 15:18:38.000000000 +0100
+++ tools/winegcc.c	2003-09-09 17:33:15.000000000 +0100
@@ -27,63 +27,15 @@
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
-#include <sys/stat.h>
 
-static char **tmp_files;
-static int nb_tmp_files;
-static int verbose = 0;
-static int keep_generated = 0;
-
-void error(const char *s, ...)
-{
-    va_list ap;
-    
-    va_start(ap, s);
-    fprintf(stderr, "Error: ");
-    vfprintf(stderr, s, ap);
-    fprintf(stderr, "\n");
-    va_end(ap);
-    exit(2);
-}
+#include "winegcc-utils.h"
 
-char *strmake(const char *fmt, ...) 
-{
-    int n, size = 100;
-    char *p;
-    va_list ap;
-
-    if ((p = malloc (size)) == NULL)
-	error("Can not malloc %d bytes.", size);
-    
-    while (1) 
-    {
-        va_start(ap, fmt);
-	n = vsnprintf (p, size, fmt, ap);
-	va_end(ap);
-        if (n > -1 && n < size) return p;
-        size *= 2;
-	if ((p = realloc (p, size)) == NULL)
-	    error("Can not realloc %d bytes.", size);
-    }
-}
+int verbose = 0;
 
-void spawn(char *const argv[])
-{
-    int i, status;
-    
-    if (verbose)
-    {	
-	for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
-	printf("\n");
-    }
-    if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
-    
-    if (status > 0) error("%s failed.", argv[0]);
-    else perror("Error:");
-    exit(3);
-}
+static int keep_generated = 0;
+static strarray *tmp_files;
 
-int strendswith(const char *str, const char *end)
+static int strendswith(const char *str, const char *end)
 {
     int l = strlen(str);
     int m = strlen(end);
@@ -91,17 +43,18 @@
     return l >= m && strcmp(str + l - m, end) == 0; 
 }
 
-void clean_temp_files()
+static void clean_temp_files()
 {
-    int i;
-    
-    if (keep_generated) return;
-
-    for (i = 0; i < nb_tmp_files; i++)
-	unlink(tmp_files[i]);
+    if (!keep_generated)
+    {
+        int i;
+	for (i = 0; i < tmp_files->size; i++)
+	    unlink(tmp_files->base[i]);
+    }
+    strarray_free(tmp_files);
 }
 
-char *get_temp_file(const char *suffix)
+static char *get_temp_file(const char *suffix)
 {
     char *tmp = strmake("wgcc.XXXXXX%s", suffix);
     int fd = mkstemps( tmp, strlen(suffix) );
@@ -114,33 +67,34 @@
         if (fd == -1) error( "could not create temp file" );
     }
     close( fd );
-    tmp_files = realloc( tmp_files, (nb_tmp_files+1) * sizeof(*tmp_files) );
-    tmp_files[nb_tmp_files++] = tmp;
+    strarray_add(tmp_files, tmp);
 
     return tmp;
 }
 
-char *get_obj_file(char **argv, int n)
+static char *get_obj_file(char **argv, int n)
 {
-    char *tmpobj, **compargv;
-    int i, j;
+    char *tmpobj;
+    strarray* compargv;
+    int j;
 
     if (strendswith(argv[n], ".o")) return argv[n];
     if (strendswith(argv[n], ".a")) return argv[n];
     if (strendswith(argv[n], ".res")) return argv[n];
     
     tmpobj = get_temp_file(".o");
-    compargv = malloc(sizeof(char*) * (n + 10));
-    i = 0;
-    compargv[i++] = "winegcc";
-    compargv[i++] = "-c";
-    compargv[i++] = "-o";
-    compargv[i++] = tmpobj;
+
+    strarray_init(compargv);
+    strarray_add(compargv,"winegcc");
+    strarray_add(compargv, "-c");
+    strarray_add(compargv, "-o");
+    strarray_add(compargv, tmpobj);
     for (j = 1; j <= n; j++)
-	if (argv[j]) compargv[i++] = argv[j];
-    compargv[i] = 0;
+	if (argv[j]) strarray_add(compargv, argv[j]);
+    strarray_add(compargv, NULL);
     
     spawn(compargv);
+    strarray_free(compargv);
 
     return tmpobj;
 }
@@ -148,11 +102,12 @@
 
 int main(int argc, char **argv)
 {
-    char **gcc_argv;
+    strarray *gcc_argv;
     int i, j;
     int linking = 1, cpp = 0, use_static_linking = 0;
     int use_stdinc = 1, use_stdlib = 1, use_msvcrt = 0, gui_app = 0;
 
+    strarray_init(tmp_files);
     atexit(clean_temp_files);
     
     if (strendswith(argv[0], "++")) cpp = 1;
@@ -210,119 +165,121 @@
 
     if (use_static_linking) error("Static linking is not supported.");
 
-    gcc_argv = malloc(sizeof(char*) * (argc + 20));
+    strarray_init(gcc_argv);
 
-    i = 0;
     if (linking)
     {
 	int has_input_files = 0;
-	char **temp_argv;
+	strarray *copy_argv;
 
-	/* we need this to erase some of the parameters as we go along */
-	temp_argv = malloc(sizeof(char*) * argc);
-	memcpy(temp_argv, argv, sizeof(char*) * argc);
-
-	gcc_argv[i++] = "winewrap";
-	if (gui_app) gcc_argv[i++] = "-mgui";
-
-	if (cpp) gcc_argv[i++] = "-C";	
+	/* we need a copy in case we decide to pass args straight to gcc
+	 * and we erase some of the original parameters as we go along
+	 */
+	strarray_init(copy_argv);
+	strarray_add(copy_argv, cpp ? "g++" : "gcc");
+	for( j = 1; j < argc ; j++ )
+	    strarray_add(copy_argv, argv[j]);
+
+ 	strarray_add(gcc_argv, "winewrap");
+ 	if (gui_app) strarray_add(gcc_argv, "-mgui");
+ 
+ 	if (cpp) strarray_add(gcc_argv, "-C");
     	for ( j = 1 ; j < argc ; j++ ) 
     	{
 	    if ( argv[j][0] == '-' )
 	    {
 		switch (argv[j][1])
 		{
-		case 'L':
-		case 'o':
-		    gcc_argv[i++] = argv[j];
-		    temp_argv[j] = 0;
-		    if (!gcc_argv[i-1][2] && j + 1 < argc)
-		    {
-			gcc_argv[i++] = argv[++j];
-			temp_argv[j] = 0;
-		    }
-		    break;
-		case 'l':
-		    gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"; 
-		    temp_argv[j] = 0;
-		    break;
+  		case 'L':
+  		case 'o':
+ 		    strarray_add(gcc_argv, argv[j]);
+ 		    if (!argv[j][2] && j + 1 < argc)
+  		    {
+  			argv[j] = 0;
+ 			strarray_add(gcc_argv, argv[++j]);
+  		    }
+ 		    argv[j] = 0;
+  		    break;
+  		case 'l':
+ 		    strarray_add(gcc_argv, strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"); 
+  		    argv[j] = 0;
+  		    break;
 		default:
 		    ; /* ignore the rest */
 		}
 	    }
 	    else
 	    {
-		gcc_argv[i++] = get_obj_file(temp_argv, j);
-		temp_argv[j] = 0;
+ 		strarray_add(gcc_argv, get_obj_file(argv, j));
+		argv[j] = 0;
 		has_input_files = 1;
 	    }
 	}
 
 	if (has_input_files)
 	{
-	    if (use_stdlib && use_msvcrt) gcc_argv[i++] = "-lmsvcrt";
-	    if (gui_app) gcc_argv[i++] = "-lcomdlg32";
-	    gcc_argv[i++] = "-ladvapi32";
-	    gcc_argv[i++] = "-lshell32";
+	    if (use_stdlib && use_msvcrt) strarray_add(gcc_argv, "-lmsvcrt");
+	    if (gui_app) strarray_add(gcc_argv, "-lcomdlg32");
+	    strarray_add(gcc_argv, "-ladvapi32");
+	    strarray_add(gcc_argv, "-lshell32");
 	}
 	else
 	{
 	    /* if we have nothing to process, just forward stuff to gcc */
-	    memcpy(gcc_argv, argv,  sizeof(char*) * argc);
-	    gcc_argv[0] = cpp ? "g++" : "gcc";
-	    i = argc;
+	    strarray_free(gcc_argv);
+	    gcc_argv = copy_argv;
 	}
     }
     else
     {
-	gcc_argv[i++] = cpp ? "g++" : "gcc";
-
-	gcc_argv[i++] = "-fshort-wchar";
-	gcc_argv[i++] = "-fPIC";
-	if (use_stdinc)
-	{
-	    if (use_msvcrt)
-	    {
-		gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
-	    	gcc_argv[i++] = "-D__MSVCRT__";
-	    }
-	    gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
-	}
-	gcc_argv[i++] = "-DWIN32";
-	gcc_argv[i++] = "-D_WIN32";
-	gcc_argv[i++] = "-D__WIN32";
-	gcc_argv[i++] = "-D__WIN32__";
-	gcc_argv[i++] = "-D__WINNT";
-	gcc_argv[i++] = "-D__WINNT__";
-
-	gcc_argv[i++] = "-D__stdcall=__attribute__((__stdcall__))";
-	gcc_argv[i++] = "-D__cdecl=__attribute__((__cdecl__))";
-	gcc_argv[i++] = "-D__fastcall=__attribute__((__fastcall__))";
-	gcc_argv[i++] = "-D_stdcall=__attribute__((__stdcall__))";
-	gcc_argv[i++] = "-D_cdecl=__attribute__((__cdecl__))";
-	gcc_argv[i++] = "-D_fastcall=__attribute__((__fastcall__))";
-	gcc_argv[i++] = "-D__declspec(x)=__declspec_##x";
-        gcc_argv[i++] = "-D__declspec_align(x)=__attribute__((aligned(x)))";
-        gcc_argv[i++] = "-D__declspec_allocate(x)=__attribute__((section(x)))";
-        gcc_argv[i++] = "-D__declspec_deprecated=__attribute__((deprecated))";
-        gcc_argv[i++] = "-D__declspec_dllimport=__attribute__((dllimport))";
-        gcc_argv[i++] = "-D__declspec_dllexport=__attribute__((dllexport))";
-        gcc_argv[i++] = "-D__declspec_naked=__attribute__((naked))";
-        gcc_argv[i++] = "-D__declspec_noinline=__attribute__((noinline))";
-        gcc_argv[i++] = "-D__declspec_noreturn=__attribute__((noreturn))";
-        gcc_argv[i++] = "-D__declspec_nothrow=__attribute__((nothrow))";
-        gcc_argv[i++] = "-D__declspec_novtable=__attribute__(())"; /* ignore it */
-        gcc_argv[i++] = "-D__declspec_selectany=__attribute__((weak))";
-        gcc_argv[i++] = "-D__declspec_thread=__thread";
-    
-	/* Wine specific defines */
-	gcc_argv[i++] = "-D__WINE__";
-	gcc_argv[i++] = "-DWINE_UNICODE_NATIVE";
-	gcc_argv[i++] = "-D__int8=char";
-	gcc_argv[i++] = "-D__int16=short";
-	gcc_argv[i++] = "-D__int32=int";
-	gcc_argv[i++] = "-D__int64=long long";
-
+ 	strarray_add(gcc_argv, cpp ? "g++" : "gcc");
+  
+ 	strarray_add(gcc_argv, "-fshort-wchar");
+ 	strarray_add(gcc_argv, "-fPIC");
+  	if (use_stdinc)
+  	{
+  	    if (use_msvcrt)
+  	    {
+ 		strarray_add(gcc_argv, "-I" INCLUDEDIR "/msvcrt");
+ 	    	strarray_add(gcc_argv, "-D__MSVCRT__");
+  	    }
+ 	    strarray_add(gcc_argv, "-I" INCLUDEDIR "/windows");
+  	}
+ 	strarray_add(gcc_argv, "-DWIN32");
+ 	strarray_add(gcc_argv, "-D_WIN32");
+ 	strarray_add(gcc_argv, "-D__WIN32");
+ 	strarray_add(gcc_argv, "-D__WIN32__");
+ 	strarray_add(gcc_argv, "-D__WINNT");
+ 	strarray_add(gcc_argv, "-D__WINNT__");
+ 
+ 	strarray_add(gcc_argv, "-D__stdcall=__attribute__((__stdcall__))");
+ 	strarray_add(gcc_argv, "-D__cdecl=__attribute__((__cdecl__))");
+ 	strarray_add(gcc_argv, "-D__fastcall=__attribute__((__fastcall__))");
+ 	strarray_add(gcc_argv, "-D_stdcall=__attribute__((__stdcall__))");
+ 	strarray_add(gcc_argv, "-D_cdecl=__attribute__((__cdecl__))");
+ 	strarray_add(gcc_argv, "-D_fastcall=__attribute__((__fastcall__))");
+ 	strarray_add(gcc_argv, "-D__declspec(x)=__declspec_##x");
+        strarray_add(gcc_argv, "-D__declspec_align(x)=__attribute__((aligned(x)))");
+        strarray_add(gcc_argv, "-D__declspec_allocate(x)=__attribute__((section(x)))");
+        strarray_add(gcc_argv, "-D__declspec_deprecated=__attribute__((deprecated))");
+        strarray_add(gcc_argv, "-D__declspec_dllimport=__attribute__((dllimport))");
+        strarray_add(gcc_argv, "-D__declspec_dllexport=__attribute__((dllexport))");
+        strarray_add(gcc_argv, "-D__declspec_naked=__attribute__((naked))");
+        strarray_add(gcc_argv, "-D__declspec_noinline=__attribute__((noinline))");
+        strarray_add(gcc_argv, "-D__declspec_noreturn=__attribute__((noreturn))");
+        strarray_add(gcc_argv, "-D__declspec_nothrow=__attribute__((nothrow))");
+        strarray_add(gcc_argv, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
+        strarray_add(gcc_argv, "-D__declspec_selectany=__attribute__((weak))");
+        strarray_add(gcc_argv, "-D__declspec_thread=__thread");
+      
+  	/* Wine specific defines */
+ 	strarray_add(gcc_argv, "-D__WINE__");
+ 	strarray_add(gcc_argv, "-DWINE_UNICODE_NATIVE");
+ 	strarray_add(gcc_argv, "-D__int8=char");
+ 	strarray_add(gcc_argv, "-D__int16=short");
+ 	strarray_add(gcc_argv, "-D__int32=int");
+ 	strarray_add(gcc_argv, "-D__int64=long long");
+  
     	for ( j = 1 ; j < argc ; j++ ) 
     	{
 	    if (strcmp("-mno-cygwin", argv[j]) == 0)
@@ -338,13 +295,15 @@
 	    else if (strcmp("-s", argv[j]) == 0)
 	    	; /* ignore this option */
             else
-            	gcc_argv[i++] = argv[j];
+            	strarray_add(gcc_argv, argv[j]);
     	}
     }
 
-    gcc_argv[i] = NULL;
+    strarray_add(gcc_argv, NULL);
 
     spawn(gcc_argv);
 
+    strarray_free(gcc_argv);
+
     return 0;
 }
diff -u -r -N tools.3/winegcc-utils.c tools/winegcc-utils.c
--- tools.3/winegcc-utils.c	1970-01-01 01:00:00.000000000 +0100
+++ tools/winegcc-utils.c	2003-09-09 18:01:02.000000000 +0100
@@ -0,0 +1,127 @@
+/*
+ * Useful functions for winegcc/winewrap
+ *
+ * Copyright 2000 Francois Gouget
+ * Copyright 2002 Dimitrie O. Paun
+ * Copyright 2003 Richard Cohen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include "config.h"
+#include "wine/port.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "winegcc-utils.h"
+
+#if !defined(min)
+# define min(x,y) (((x) < (y)) ? (x) : (y))
+#endif
+
+extern int verbose;
+
+void error(const char *s, ...)
+{
+    va_list ap;
+    
+    va_start(ap, s);
+    fprintf(stderr, "Error: ");
+    vfprintf(stderr, s, ap);
+    fprintf(stderr, "\n");
+    va_end(ap);
+    exit(2);
+}
+
+void *xmalloc(size_t size)
+{
+    void *p;
+    if ((p = malloc (size)) == NULL)
+	error("Can not malloc %d bytes.", size);
+
+    return p;
+}
+
+void *xrealloc(void* p, size_t size)
+{
+    void* p2;
+    if ((p2 = realloc (p, size)) == NULL)
+	error("Can not realloc %d bytes.", size);
+
+    return p2;
+}
+
+char *strmake(const char *fmt, ...) 
+{
+    int n;
+    size_t size = 100;
+    char *p;
+    va_list ap;
+    p = xmalloc (size);
+    
+    while (1) 
+    {
+        va_start(ap, fmt);
+	n = vsnprintf (p, size, fmt, ap);
+	va_end(ap);
+        if (n > -1 && n < size) return p;
+	size = min( size*2, n+1 );
+	p = xrealloc (p, size);
+    }
+}
+
+void _strarray_init(strarray* arr)
+{
+    arr->maximum = arr->size = 0;
+    arr->base = NULL;
+}
+
+void _strarray_free(strarray* arr)
+{
+    free(arr->base);
+    arr->base = NULL;
+    arr->maximum = arr->size = 0;
+}
+
+void strarray_add(strarray* arr, char* str)
+{
+    if (arr->size == arr->maximum)
+    {
+	arr->maximum += 10;
+	arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
+    }
+    arr->base[arr->size++] = str;
+}
+
+void spawn(strarray* arr)
+{
+    int i, status;
+    char **argv = arr->base;
+    
+    if (verbose)
+    {	
+	for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
+	printf("\n");
+    }
+    if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
+    
+    if (status > 0) error("%s failed.", argv[0]);
+    else perror("Error:");
+    exit(3);
+}
+
diff -u -r -N tools.3/winegcc-utils.h tools/winegcc-utils.h
--- tools.3/winegcc-utils.h	1970-01-01 01:00:00.000000000 +0100
+++ tools/winegcc-utils.h	2003-09-09 17:37:15.000000000 +0100
@@ -0,0 +1,44 @@
+/*
+ * Useful functions for winegcc/winewrap
+ *
+ * Copyright 2000 Francois Gouget
+ * Copyright 2002 Dimitrie O. Paun
+ * Copyright 2003 Richard Cohen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+void error(const char *s, ...);
+
+void *xmalloc(size_t size);
+void *xrealloc(void* p, size_t size);
+char *strmake(const char *fmt, ...);
+
+typedef struct {
+    size_t maximum;
+    size_t size;
+    char** base;
+} strarray;
+
+void _strarray_init(strarray* arr);
+#define strarray_init(x) do {x = xmalloc(sizeof(*x)); _strarray_init(x);} while (0)
+void _strarray_free(strarray* arr);
+#define strarray_free(x) do { _strarray_free(x); free(x); x = NULL;} while (0)
+void strarray_add(strarray* arr, char* str);
+
+void spawn(strarray* arr);
+
+
diff -u -r -N tools.3/winewrap.c tools/winewrap.c
--- tools.3/winewrap.c	2003-09-08 15:17:27.000000000 +0100
+++ tools/winewrap.c	2003-09-09 10:41:22.000000000 +0100
@@ -29,6 +29,8 @@
 #include <errno.h>
 #include <sys/stat.h>
 
+#include "winegcc-utils.h"
+
 #ifndef WINEDLLS
 #define WINEDLLS "/usr/local/lib/wine"
 #endif
@@ -215,50 +217,17 @@
 /* Default output name is the same as mingw-ld */
 static char *output_name = "a.exe";
 
-static char **arh_files,  **dll_files,  **lib_files,  **lib_paths,  **obj_files;
-static int nb_arh_files, nb_dll_files, nb_lib_files, nb_lib_paths, nb_obj_files;
-static int verbose = 0;
-static int keep_generated = 0;
+static strarray *arh_files, *dll_files, *lib_files, *lib_paths, *obj_files;
 
-void error(const char *s, ...)
-{
-    va_list ap;
-    
-    va_start(ap, s);
-    fprintf(stderr, "Error: ");
-    vfprintf(stderr, s, ap);
-    fprintf(stderr, "\n");
-    va_end(ap);
-    exit(2);
-}
-
-char *strmake(const char *fmt, ...) 
-{
-    int n, size = 100;
-    char *p;
-    va_list ap;
-
-    if ((p = malloc (size)) == NULL)
-	error("Can not malloc %d bytes.", size);
-    
-    while (1) 
-    {
-        va_start(ap, fmt);
-	n = vsnprintf (p, size, fmt, ap);
-	va_end(ap);
-        if (n > -1 && n < size) return p;
-        size *= 2;
-	if ((p = realloc (p, size)) == NULL)
-	    error("Can not realloc %d bytes.", size);
-    }
-}
+int verbose = 0;
+static int keep_generated = 0;
 
-void rm_temp_file(const char *file)
+static void rm_temp_file(const char *file)
 {
     if (!keep_generated) unlink(file);
 }
 
-void create_file(const char *name, const char *fmt, ...)
+static void create_file(const char *name, const char *fmt, ...)
 {
     va_list ap;
     FILE *file;
@@ -272,23 +241,7 @@
     fclose(file);
 }
 
-void spawn(char *const argv[])
-{
-    int i, status;
-    
-    if (verbose)
-    {	
-	for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
-	printf("\n");
-    }
-    if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
-    
-    if (status > 0) error("%s failed.", argv[0]);
-    else perror("Error:");
-    exit(3);
-}
-
-int is_resource(const char* file)
+static int is_resource(const char* file)
 {
     /* see tools/winebuild/res32.c: check_header for details */
     static const char res_sig[] = { 0,0,0,0, 32,0,0,0, 0xff,0xff, 0,0, 0xff,0xff, 0,0, 0,0,0,0, 0,0, 0,0, 0,0,0,0, 0,0,0,0 };
@@ -336,14 +289,14 @@
 }
 
 /* open the .def library for a given dll */
-static char *open_dll(const char *name)
+static char *find_dll(const char *name)
 {
     char *fullname;
     int i;
 
-    for (i = 0; i < nb_lib_paths; i++)
+    for (i = 0; i < lib_paths->size; i++)
     {
-        if ((fullname = try_dll_path( lib_paths[i], name ))) return fullname;
+        if ((fullname = try_dll_path( lib_paths->base[i], name ))) return fullname;
     }
     return try_dll_path( ".", name );
 }
@@ -355,9 +308,9 @@
     char *fullname;
     int i;
     
-    for (i = 0; i < nb_lib_paths; i++)
+    for (i = 0; i < lib_paths->size; i++)
     {
-        if ((fullname = try_lib_path( lib_paths[i], name ))) return fullname;
+        if ((fullname = try_lib_path( lib_paths->base[i], name ))) return fullname;
     }
 
     for (i = 0; i < sizeof(std_paths)/sizeof(std_paths[0]); i++)
@@ -368,48 +321,48 @@
     return 0;
 }
 
-void add_lib_path(const char* path)
+static void add_lib_path(const char* path)
 {
-    lib_paths = realloc( lib_paths, (nb_lib_paths+1) * sizeof(*lib_paths) );
-    lib_paths[nb_lib_paths++] = strdup(path);
-    dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
-    dll_files[nb_dll_files++] = strmake("-L%s", path);
-    lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
-    lib_files[nb_lib_files++] = strmake("-L%s", path);
+    strarray_add(lib_paths, strdup(path));
+    strarray_add(dll_files, strmake("-L%s", path));
+    strarray_add(lib_files, strmake("-L%s", path));
 }
 
-void add_lib_file(const char* library)
+static void add_lib_file(const char* library)
 {
     char *lib;
     
-    if (open_dll(library))
+    if (find_dll(library))
     {
-        dll_files = realloc( dll_files, (nb_dll_files+1) * sizeof(*dll_files) );
-        dll_files[nb_dll_files++] = strmake("-l%s", library);
+	strarray_add(dll_files, strmake("-l%s", library));
     }
     else if ((lib = find_lib(library)))
     {
-        arh_files = realloc( arh_files, (nb_arh_files+1) * sizeof(*arh_files) );
-        arh_files[nb_arh_files++] = lib;
+        strarray_add(arh_files, lib);
     }
     else
     {
-        lib_files = realloc( lib_files, (nb_lib_files+1) * sizeof(*lib_files) );
-        lib_files[nb_lib_files++] = strmake("-l%s", library);
+        strarray_add(lib_files, strmake("-l%s", library));
     }
 }
 
 int main(int argc, char **argv)
 {
     char *library = 0, *path = 0;
-    int i, j, len, cpp = 0, no_opt = 0, gui_mode = 0, create_wrapper = -1;
+    int i, len, cpp = 0, no_opt = 0, gui_mode = 0, create_wrapper = -1;
     char *base_name, *base_file, *app_temp_name, *wrp_temp_name;
     char *spec_name, *spec_c_name, *spec_o_name;
     char *wspec_name, *wspec_c_name, *wspec_o_name;
     char *wrap_c_name, *wrap_o_name;
-    char **spec_args, **comp_args, **link_args;
-    char **wwrap_args, **wspec_args, **wcomp_args, **wlink_args;
+    strarray *spec_args, *comp_args, *link_args;
+    strarray *wwrap_args, *wspec_args, *wcomp_args, *wlink_args;
    
+    strarray_init(arh_files);
+    strarray_init(dll_files);
+    strarray_init(lib_files);
+    strarray_init(lib_paths);
+    strarray_init(obj_files);
+    
     /* include the standard DLL path first */
     add_lib_path(WINEDLLS);
 	
@@ -470,8 +423,7 @@
 	}
 	
 	/* it's a filename, add it to its list */
-	obj_files = realloc( obj_files, (nb_obj_files+1) * sizeof(*obj_files) );
-	obj_files[nb_obj_files++] = strdup(argv[i]);
+	strarray_add(obj_files, strdup(argv[i]));
     }
 
     /* create wrapper only in C++ by default */
@@ -507,113 +459,110 @@
     wrap_o_name = strmake("%s.o", wrp_temp_name);
 
     /* build winebuild's argument list */
-    spec_args = malloc( (nb_arh_files + nb_dll_files + nb_obj_files + 20) * sizeof (char *) );
-    j = 0;
-    spec_args[j++] = "winebuild";
-    spec_args[j++] = "-o";
-    spec_args[j++] = spec_c_name;
+    strarray_init(spec_args);
+    strarray_add(spec_args, "winebuild");
+    strarray_add(spec_args, "-o");
+    strarray_add(spec_args, spec_c_name);
     if (create_wrapper)
     {
-	spec_args[j++] = "-F";
-	spec_args[j++] = strmake("%s-wrap.dll", base_name);
-	spec_args[j++] = "--spec";
-	spec_args[j++] = spec_name;
+	strarray_add(spec_args, "-F");
+	strarray_add(spec_args, strmake("%s-wrap.dll", base_name));
+	strarray_add(spec_args, "--spec");
+	strarray_add(spec_args, spec_name);
     }
     else
     {
-	spec_args[j++] = "--exe";
-	spec_args[j++] = strmake("%s.exe", base_name);
-        spec_args[j++] = gui_mode ? "-mgui" : "-mcui";
-    }
-    for (i = 0; i < nb_dll_files; i++)
-	spec_args[j++] = dll_files[i];
-    for (i = 0; i < nb_obj_files; i++)
-	spec_args[j++] = obj_files[i];
-    for (i = 0; i < nb_arh_files; i++)
-	spec_args[j++] = arh_files[i];
-    spec_args[j] = 0;
+	strarray_add(spec_args, "--exe");
+	strarray_add(spec_args, strmake("%s.exe", base_name));
+        strarray_add(spec_args, gui_mode ? "-mgui" : "-mcui");
+    }
+    for (i = 0; i < dll_files->size; i++)
+	strarray_add(spec_args, dll_files->base[i]);
+    for (i = 0; i < obj_files->size; i++)
+	strarray_add(spec_args, obj_files->base[i]);
+    for (i = 0; i < arh_files->size; i++)
+	strarray_add(spec_args, arh_files->base[i]);
+    strarray_add(spec_args, NULL);
 
     /* build gcc's argument list */
-    comp_args = malloc ( 20 * sizeof (char *) );
-    j = 0;
-    comp_args[j++] = "gcc";
-    comp_args[j++] = "-fPIC";
-    comp_args[j++] = "-o";
-    comp_args[j++] = spec_o_name;
-    comp_args[j++] = "-c";
-    comp_args[j++] = spec_c_name;
-    comp_args[j] = 0;
+    strarray_init(comp_args);
+    strarray_add(comp_args, "gcc");
+    strarray_add(comp_args, "-fPIC");
+    strarray_add(comp_args, "-o");
+    strarray_add(comp_args, spec_o_name);
+    strarray_add(comp_args, "-c");
+    strarray_add(comp_args, spec_c_name);
+    strarray_add(comp_args, NULL);
     
     /* build ld's argument list */
-    link_args = malloc( (nb_arh_files + nb_obj_files + nb_lib_files + 20) * sizeof (char *) );
-    j = 0;
-    link_args[j++] = cpp ? "g++" : "gcc";
-    link_args[j++] = "-shared";
-    link_args[j++] = "-Wl,-Bsymbolic,-z,defs";
-    link_args[j++] = "-lwine";
-    link_args[j++] = "-lm";
-    for (i = 0; i < nb_lib_files; i++)
-	link_args[j++] = lib_files[i];
-    link_args[j++] = "-o";
-    if (create_wrapper) link_args[j++] = strmake("%s-wrap.dll.so", base_file);
-    else link_args[j++] = strmake("%s.exe.so", base_file);
-    link_args[j++] = spec_o_name;
-    for (i = 0; i < nb_obj_files; i++)
-	if (!is_resource(obj_files[i])) link_args[j++] = obj_files[i];
-    for (i = 0; i < nb_arh_files; i++)
-	link_args[j++] = arh_files[i];
-    link_args[j] = 0;
+    strarray_init(link_args);
+    strarray_add(link_args, cpp ? "g++" : "gcc");
+    strarray_add(link_args, "-shared");
+    strarray_add(link_args, "-Wl,-Bsymbolic,-z,defs");
+    strarray_add(link_args, "-lwine");
+    strarray_add(link_args, "-lm");
+    for (i = 0; i < lib_files->size; i++)
+	strarray_add(link_args, lib_files->base[i]);
+    strarray_add(link_args, "-o");
+    if (create_wrapper)
+	strarray_add(link_args, strmake("%s-wrap.dll.so", base_file));
+    else
+	strarray_add(link_args, strmake("%s.exe.so", base_file));
+    strarray_add(link_args, spec_o_name);
+
+    for (i = 0; i < obj_files->size; i++)
+	if (!is_resource(obj_files->base[i]))
+	    strarray_add(link_args, obj_files->base[i]);
+    for (i = 0; i < arh_files->size; i++)
+	strarray_add(link_args, arh_files->base[i]);
+    strarray_add(link_args, NULL);
   
     /* build wrapper compile argument list */
-    wwrap_args = malloc ( 20 * sizeof (char *) );
-    j = 0;
-    wwrap_args[j++] = "gcc";
-    wwrap_args[j++] = "-fPIC";
-    wwrap_args[j++] = "-I" INCLUDEDIR "/windows";
-    wwrap_args[j++] = "-o";
-    wwrap_args[j++] = wrap_o_name;
-    wwrap_args[j++] = "-c";
-    wwrap_args[j++] = wrap_c_name;
-    wwrap_args[j] = 0;
+    strarray_init(wwrap_args);
+    strarray_add(wwrap_args, "gcc");
+    strarray_add(wwrap_args, "-fPIC");
+    strarray_add(wwrap_args, "-I" INCLUDEDIR "/windows");
+    strarray_add(wwrap_args, "-o");
+    strarray_add(wwrap_args, wrap_o_name);
+    strarray_add(wwrap_args, "-c");
+    strarray_add(wwrap_args, wrap_c_name);
+    strarray_add(wwrap_args, NULL);
      
     /* build wrapper winebuild's argument list */
-    wspec_args = malloc( (nb_dll_files + 20) * sizeof (char *) );
-    j = 0;
-    wspec_args[j++] = "winebuild";
-    wspec_args[j++] = "-o";
-    wspec_args[j++] = wspec_c_name;
-    wspec_args[j++] = "--exe";
-    wspec_args[j++] = strmake("%s.exe", base_name);
-    wspec_args[j++] = gui_mode ? "-mgui" : "-mcui";
-    wspec_args[j++] = wrap_o_name;
-    for (i = 0; i < nb_dll_files; i++)
-	wspec_args[j++] = dll_files[i];
-    wspec_args[j] = 0;
+    strarray_init(wspec_args);
+    strarray_add(wspec_args, "winebuild");
+    strarray_add(wspec_args, "-o");
+    strarray_add(wspec_args, wspec_c_name);
+    strarray_add(wspec_args, "--exe");
+    strarray_add(wspec_args, strmake("%s.exe", base_name));
+    strarray_add(wspec_args, gui_mode ? "-mgui" : "-mcui");
+    strarray_add(wspec_args, wrap_o_name);
+    for (i = 0; i < dll_files->size; i++)
+	strarray_add(wspec_args, dll_files->base[i]);
+    strarray_add(wspec_args, NULL);
 
     /* build wrapper gcc's argument list */
-    wcomp_args = malloc ( 20 * sizeof (char *) );
-    j = 0;
-    wcomp_args[j++] = "gcc";
-    wcomp_args[j++] = "-fPIC";
-    wcomp_args[j++] = "-o";
-    wcomp_args[j++] = wspec_o_name;
-    wcomp_args[j++] = "-c";
-    wcomp_args[j++] = wspec_c_name;
-    wcomp_args[j] = 0;
+    strarray_init(wcomp_args);
+    strarray_add(wcomp_args, "gcc");
+    strarray_add(wcomp_args, "-fPIC");
+    strarray_add(wcomp_args, "-o");
+    strarray_add(wcomp_args, wspec_o_name);
+    strarray_add(wcomp_args, "-c");
+    strarray_add(wcomp_args, wspec_c_name);
+    strarray_add(wcomp_args, NULL);
     
     /* build wrapper ld's argument list */
-    wlink_args = malloc( 20 * sizeof (char *) );
-    j = 0;
-    wlink_args[j++] = cpp ? "g++" : "gcc";
-    wlink_args[j++] = "-shared";
-    wlink_args[j++] = "-Wl,-Bsymbolic,-z,defs";
-    wlink_args[j++] = "-lwine";
-    wlink_args[j++] = "-ldl";
-    wlink_args[j++] = "-o";
-    wlink_args[j++] = strmake("%s.exe.so", base_file);
-    wlink_args[j++] = wspec_o_name;
-    wlink_args[j++] = wrap_o_name;
-    wlink_args[j] = 0;
+    strarray_init(wlink_args);
+    strarray_add(wlink_args, cpp ? "g++" : "gcc");
+    strarray_add(wlink_args, "-shared");
+    strarray_add(wlink_args, "-Wl,-Bsymbolic,-z,defs");
+    strarray_add(wlink_args, "-lwine");
+    strarray_add(wlink_args, "-ldl");
+    strarray_add(wlink_args, "-o");
+    strarray_add(wlink_args, strmake("%s.exe.so", base_file));
+    strarray_add(wlink_args, wspec_o_name);
+    strarray_add(wlink_args, wrap_o_name);
+    strarray_add(wlink_args, NULL);
     
     /* run winebuild to get the .spec.c file */
     if (create_wrapper)

[Index of Archives]     [Gimp for Windows]     [Red Hat]     [Samba]     [Yosemite Camping]     [Graphics Cards]     [Wine Home]

  Powered by Linux