krnl386.exe16: Update a comment.
[wine/testsucceed.git] / tools / winegcc / utils.c
blob05b9a651b9e1ac4f54b1e3216ddb22bb237e503f
1 /*
2 * Useful functions for winegcc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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, "winegcc: ");
45 vfprintf(stderr, s, ap);
46 va_end(ap);
47 exit(2);
50 void* xmalloc(size_t size)
52 void* p;
54 if ((p = malloc (size)) == NULL)
55 error("Could not malloc %d bytes\n", size);
57 return p;
60 void *xrealloc(void* p, size_t size)
62 void* p2 = realloc (p, size);
63 if (size && !p2)
64 error("Could not realloc %d bytes\n", size);
66 return p2;
69 char *xstrdup( const char *str )
71 char *res = strdup( str );
72 if (!res) error("Virtual memory exhausted.\n");
73 return res;
76 int strendswith(const char* str, const char* end)
78 int l = strlen(str);
79 int m = strlen(end);
81 return l >= m && strcmp(str + l - m, end) == 0;
84 char* strmake(const char* fmt, ...)
86 int n;
87 size_t size = 100;
88 va_list ap;
90 while (1)
92 char *p = xmalloc (size);
93 va_start(ap, fmt);
94 n = vsnprintf (p, size, fmt, ap);
95 va_end(ap);
96 if (n == -1) size *= 2;
97 else if ((size_t)n >= size) size = n + 1;
98 else return p;
99 free(p);
103 strarray* strarray_alloc(void)
105 strarray* arr = xmalloc(sizeof(*arr));
106 arr->maximum = arr->size = 0;
107 arr->base = NULL;
108 return arr;
111 void strarray_free(strarray* arr)
113 free(arr->base);
114 free(arr);
117 void strarray_add(strarray* arr, const char* str)
119 if (arr->size == arr->maximum)
121 arr->maximum += 10;
122 arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
124 arr->base[arr->size++] = str;
127 void strarray_del(strarray* arr, unsigned int i)
129 if (i >= arr->size) error("Invalid index i=%d\n", i);
130 memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
131 arr->size--;
134 void strarray_addall(strarray* arr, const strarray* from)
136 unsigned int i;
138 for (i = 0; i < from->size; i++)
139 strarray_add(arr, from->base[i]);
142 strarray* strarray_dup(const strarray* arr)
144 strarray* dup = strarray_alloc();
145 unsigned int i;
147 for (i = 0; i < arr->size; i++)
148 strarray_add(dup, arr->base[i]);
150 return dup;
153 strarray* strarray_fromstring(const char* str, const char* delim)
155 strarray* arr = strarray_alloc();
156 char* buf = strdup(str);
157 const char* tok;
159 for(tok = strtok(buf, delim); tok; tok = strtok(0, delim))
160 strarray_add(arr, strdup(tok));
162 free(buf);
163 return arr;
166 char* strarray_tostring(const strarray* arr, const char* sep)
168 char *str, *newstr;
169 unsigned int i;
171 str = strmake("%s", arr->base[0]);
172 for (i = 1; i < arr->size; i++)
174 newstr = strmake("%s%s%s", str, sep, arr->base[i]);
175 free(str);
176 str = newstr;
179 return str;
182 char* get_basename(const char* file)
184 const char* name;
185 char *base_name, *p;
187 if ((name = strrchr(file, '/'))) name++;
188 else name = file;
190 base_name = strdup(name);
191 if ((p = strrchr(base_name, '.'))) *p = 0;
193 return base_name;
196 void create_file(const char* name, int mode, const char* fmt, ...)
198 va_list ap;
199 FILE *file;
201 if (verbose) printf("Creating file %s\n", name);
202 va_start(ap, fmt);
203 if ( !(file = fopen(name, "w")) )
204 error("Unable to open %s for writing\n", name);
205 vfprintf(file, fmt, ap);
206 va_end(ap);
207 fclose(file);
208 chmod(name, mode);
211 file_type get_file_type(const char* filename)
213 /* see tools/winebuild/res32.c: check_header for details */
214 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 };
215 char buf[sizeof(res_sig)];
216 int fd, cnt;
218 fd = open( filename, O_RDONLY );
219 if (fd == -1) return file_na;
220 cnt = read(fd, buf, sizeof(buf));
221 close( fd );
222 if (cnt == -1) return file_na;
224 if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
225 if (strendswith(filename, ".o")) return file_obj;
226 if (strendswith(filename, ".a")) return file_arh;
227 if (strendswith(filename, ".res")) return file_res;
228 if (strendswith(filename, ".so")) return file_so;
229 if (strendswith(filename, ".dylib")) return file_so;
230 if (strendswith(filename, ".def")) return file_def;
231 if (strendswith(filename, ".spec")) return file_spec;
232 if (strendswith(filename, ".rc")) return file_rc;
234 return file_other;
237 static char* try_lib_path(const char* dir, const char* pre,
238 const char* library, const char* ext,
239 file_type expected_type)
241 char *fullname;
242 file_type type;
244 /* first try a subdir named from the library we are looking for */
245 fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
246 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
247 type = get_file_type(fullname);
248 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
249 if (type == expected_type) return fullname;
250 free( fullname );
252 fullname = strmake("%s/%s%s%s", dir, pre, library, ext);
253 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
254 type = get_file_type(fullname);
255 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
256 if (type == expected_type) return fullname;
257 free( fullname );
258 return 0;
261 static file_type guess_lib_type(enum target_platform platform, const char* dir,
262 const char* library, const char *suffix, char** file)
264 if (platform != PLATFORM_WINDOWS && platform != PLATFORM_CYGWIN)
266 /* Unix shared object */
267 if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
268 return file_so;
270 /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
271 if ((*file = try_lib_path(dir, "lib", library, ".dylib", file_so)))
272 return file_so;
274 /* Windows DLL */
275 if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
276 return file_dll;
279 /* static archives */
280 if ((*file = try_lib_path(dir, "lib", library, suffix, file_arh)))
281 return file_arh;
283 return file_na;
286 file_type get_lib_type(enum target_platform platform, strarray* path, const char *library,
287 const char *suffix, char** file)
289 unsigned int i;
291 if (!suffix) suffix = ".a";
292 for (i = 0; i < path->size; i++)
294 file_type type = guess_lib_type(platform, path->base[i], library, suffix, file);
295 if (type != file_na) return type;
297 return file_na;
300 void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
302 unsigned int i;
303 int status;
304 strarray* arr = strarray_dup(args);
305 const char** argv;
306 char* prog = 0;
308 strarray_add(arr, NULL);
309 argv = arr->base;
311 if (prefix)
313 const char *p = strrchr(argv[0], '/');
314 if (!p) p = argv[0];
315 else p++;
317 for (i = 0; i < prefix->size; i++)
319 struct stat st;
321 free( prog );
322 prog = strmake("%s/%s%s", prefix->base[i], p, EXEEXT);
323 if (stat(prog, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111))
325 argv[0] = prog;
326 break;
331 if (verbose)
333 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
334 printf("\n");
337 if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
339 if (status > 0) error("%s failed\n", argv[0]);
340 else perror("winegcc");
341 exit(3);
344 free(prog);
345 strarray_free(arr);