[winegcc05] Improve memory management

Richard Cohen richard.cohen at virgin.net
Wed Aug 13 21:01:59 CDT 2003


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

Changelog:
	Use an extendable array to store the lists of strings.
-------------- next part --------------
diff -uN tools.4/Makefile.in tools/Makefile.in
--- tools.4/Makefile.in	2003-05-02 21:24:39.000000000 +0100
+++ tools/Makefile.in	2003-08-14 00:58:05.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 -uN tools.4/winegcc.c tools/winegcc.c
--- tools.4/winegcc.c	2003-08-13 15:49:12.000000000 +0100
+++ tools/winegcc.c	2003-08-14 00:55:07.000000000 +0100
@@ -27,61 +27,13 @@
 #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);
-}
-
-char *strmake(const char *fmt, ...) 
-{
-    int n, size = 100;
-    char *p;
-    va_list ap;
+#include "winegcc-utils.h"
 
-    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 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);
-}
+strarray  *tmp_files;
 
 int strendswith(const char *str, const char *end)
 {
@@ -93,12 +45,13 @@
 
 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)
@@ -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)
 {
-    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;
@@ -214,15 +169,14 @@
 
     if (use_static_linking) error("Static linking is not supported.");
 
-    gcc_argv = malloc(sizeof(char*) * (argc + 50));
+    strarray_init(gcc_argv);
 
-    i = 0;
     if (linking)
     {
-	gcc_argv[i++] = "winewrap";
-	if (gui_app) gcc_argv[i++] = "-mgui";
+	strarray_add(gcc_argv, "winewrap");
+	if (gui_app) strarray_add(gcc_argv, "-mgui");
 
-	if (cpp) gcc_argv[i++] = "-C";	
+	if (cpp) strarray_add(gcc_argv, "-C");
     	for ( j = 1 ; j < argc ; j++ ) 
     	{
 	    if ( argv[j][0] == '-' )
@@ -231,16 +185,16 @@
 		{
 		case 'L':
 		case 'o':
-		    gcc_argv[i++] = argv[j];
-		    argv[j] = 0;
-		    if (!gcc_argv[i-1][2] && j + 1 < argc)
+		    strarray_add(gcc_argv, argv[j]);
+		    if (!argv[j][2] && j + 1 < argc)
 		    {
-			gcc_argv[i++] = argv[++j];
 			argv[j] = 0;
+			strarray_add(gcc_argv, argv[++j]);
 		    }
+		    argv[j] = 0;
 		    break;
 		case 'l':
-		    gcc_argv[i++] = strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"; 
+		    strarray_add(gcc_argv, strcmp(argv[j], "-luuid") ? argv[j] : "-lwine_uuid"); 
 		    argv[j] = 0;
 		    break;
 		default:
@@ -249,64 +203,64 @@
 	    }
 	    else
 	    {
-		gcc_argv[i++] = get_obj_file(argv, j);
+		strarray_add(gcc_argv, get_obj_file(argv, j));
 		argv[j] = 0;
 	    }
 	}
-	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
     {
-	gcc_argv[i++] = cpp ? "g++" : "gcc";
+	strarray_add(gcc_argv, cpp ? "g++" : "gcc");
 
-	gcc_argv[i++] = "-fshort-wchar";
-	gcc_argv[i++] = "-fPIC";
+	strarray_add(gcc_argv, "-fshort-wchar");
+	strarray_add(gcc_argv, "-fPIC");
 	if (use_stdinc)
 	{
 	    if (use_msvcrt)
 	    {
-		gcc_argv[i++] = "-I" INCLUDEDIR "/msvcrt";
-	    	gcc_argv[i++] = "-D__MSVCRT__";
+		strarray_add(gcc_argv, "-I" INCLUDEDIR "/msvcrt");
+	    	strarray_add(gcc_argv, "-D__MSVCRT__");
 	    }
-	    gcc_argv[i++] = "-I" INCLUDEDIR "/windows";
+	    strarray_add(gcc_argv, "-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";
+	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 */
-	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, "-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++ ) 
     	{
@@ -323,13 +277,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 -uN tools.4/winegcc-utils.c tools/winegcc-utils.c
--- tools.4/winegcc-utils.c	1970-01-01 01:00:00.000000000 +0100
+++ tools/winegcc-utils.c	2003-08-14 00:51:52.000000000 +0100
@@ -0,0 +1,123 @@
+/*
+ * 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"
+
+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 -uN tools.4/winegcc-utils.h tools/winegcc-utils.h
--- tools.4/winegcc-utils.h	1970-01-01 01:00:00.000000000 +0100
+++ tools/winegcc-utils.h	2003-08-14 00:52:03.000000000 +0100
@@ -0,0 +1,42 @@
+/*
+ * 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);
+
+#if !defined(MIN)
+# define MIN(x,y) (((x) < (y)) ? (x) : (y))
+#endif
+
diff -uN tools.4/winewrap.c tools/winewrap.c
--- tools.4/winewrap.c	2003-08-13 15:23:55.000000000 +0100
+++ tools/winewrap.c	2003-08-14 01:09:55.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
@@ -212,44 +214,11 @@
     "}\n"
 ;
 
-static char *output_name;
-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;
+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);
-}
-
-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);
-    }
-}
+static char *output_name;
+static strarray *arh_files, *dll_files, *lib_files, *lib_paths, *obj_files;
 
 void rm_temp_file(const char *file)
 {
@@ -270,22 +239,6 @@
     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)
 {
     /* see tools/winebuild/res32.c: check_header for details */
@@ -334,14 +287,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 );
 }
@@ -353,9 +306,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,46 +321,46 @@
 
 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)
 {
     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);
 	
@@ -468,8 +421,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 */
@@ -509,113 +461,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)


More information about the wine-patches mailing list