Use an extendable array to store the lists of strings.
[wine/multimedia.git] / tools / winegcc / utils.c
blobbd7d7a510a4210f8a89eb839e6d22f1a1d8e978b
1 /*
2 * Useful functions for winegcc/winewrap
4 * Copyright 2000 Francois Gouget
5 * Copyright 2002 Dimitrie O. Paun
6 * Copyright 2003 Richard Cohen
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
31 #include "utils.h"
33 #if !defined(min)
34 # define min(x,y) (((x) < (y)) ? (x) : (y))
35 #endif
37 int verbose = 0;
39 void error(const char *s, ...)
41 va_list ap;
43 va_start(ap, s);
44 fprintf(stderr, "Error: ");
45 vfprintf(stderr, s, ap);
46 fprintf(stderr, "\n");
47 va_end(ap);
48 exit(2);
51 void *xmalloc(size_t size)
53 void *p;
54 if ((p = malloc (size)) == NULL)
55 error("Can not malloc %d bytes.", size);
57 return p;
60 void *xrealloc(void* p, size_t size)
62 void* p2;
63 if ((p2 = realloc (p, size)) == NULL)
64 error("Can not realloc %d bytes.", size);
66 return p2;
69 char *strmake(const char *fmt, ...)
71 int n;
72 size_t size = 100;
73 char *p;
74 va_list ap;
75 p = xmalloc (size);
77 while (1)
79 va_start(ap, fmt);
80 n = vsnprintf (p, size, fmt, ap);
81 va_end(ap);
82 if (n > -1 && n < size) return p;
83 size = min( size*2, n+1 );
84 p = xrealloc (p, size);
88 strarray *strarray_alloc(void)
90 strarray *arr = xmalloc(sizeof(*arr));
91 arr->maximum = arr->size = 0;
92 arr->base = NULL;
93 return arr;
96 void strarray_free(strarray* arr)
98 free(arr->base);
99 free(arr);
102 void strarray_add(strarray* arr, char* str)
104 if (arr->size == arr->maximum)
106 arr->maximum += 10;
107 arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
109 arr->base[arr->size++] = str;
112 void spawn(strarray* arr)
114 int i, status;
115 char **argv = arr->base;
117 if (verbose)
119 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
120 printf("\n");
122 if (!(status = spawnvp( _P_WAIT, argv[0], argv))) return;
124 if (status > 0) error("%s failed.", argv[0]);
125 else perror("Error:");
126 exit(3);