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
23 #include "wine/port.h"
34 # define min(x,y) (((x) < (y)) ? (x) : (y))
39 void error(const char *s
, ...)
44 fprintf(stderr
, "Error: ");
45 vfprintf(stderr
, s
, ap
);
46 fprintf(stderr
, "\n");
51 void *xmalloc(size_t size
)
54 if ((p
= malloc (size
)) == NULL
)
55 error("Can not malloc %d bytes.", size
);
60 void *xrealloc(void* p
, size_t size
)
63 if ((p2
= realloc (p
, size
)) == NULL
)
64 error("Can not realloc %d bytes.", size
);
69 char *strmake(const char *fmt
, ...)
80 n
= vsnprintf (p
, size
, fmt
, 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;
96 void strarray_free(strarray
* arr
)
102 void strarray_add(strarray
* arr
, char* str
)
104 if (arr
->size
== arr
->maximum
)
107 arr
->base
= xrealloc(arr
->base
, sizeof(*(arr
->base
)) * arr
->maximum
);
109 arr
->base
[arr
->size
++] = str
;
112 void spawn(strarray
* arr
)
115 char **argv
= arr
->base
;
119 for(i
= 0; argv
[i
]; i
++) printf("%s ", argv
[i
]);
122 if (!(status
= spawnvp( _P_WAIT
, argv
[0], argv
))) return;
124 if (status
> 0) error("%s failed.", argv
[0]);
125 else perror("Error:");