richedit: Don't put cursor in the table row start paragraph.
[wine/multimedia.git] / tools / winegcc / utils.c
blob225062d307d1d4f550861be9da7428181286bc4e
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., 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 int strendswith(const char* str, const char* end)
71 int l = strlen(str);
72 int m = strlen(end);
74 return l >= m && strcmp(str + l - m, end) == 0;
77 char* strmake(const char* fmt, ...)
79 int n;
80 size_t size = 100;
81 va_list ap;
83 while (1)
85 char *p = xmalloc (size);
86 va_start(ap, fmt);
87 n = vsnprintf (p, size, fmt, ap);
88 va_end(ap);
89 if (n == -1) size *= 2;
90 else if ((size_t)n >= size) size = n + 1;
91 else return p;
92 free(p);
96 strarray* strarray_alloc(void)
98 strarray* arr = xmalloc(sizeof(*arr));
99 arr->maximum = arr->size = 0;
100 arr->base = NULL;
101 return arr;
104 void strarray_free(strarray* arr)
106 free(arr->base);
107 free(arr);
110 void strarray_add(strarray* arr, const char* str)
112 if (arr->size == arr->maximum)
114 arr->maximum += 10;
115 arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
117 arr->base[arr->size++] = str;
120 void strarray_del(strarray* arr, unsigned int i)
122 if (i >= arr->size) error("Invalid index i=%d\n", i);
123 memmove(&arr->base[i], &arr->base[i + 1], (arr->size - i - 1) * sizeof(arr->base[0]));
124 arr->size--;
127 void strarray_addall(strarray* arr, const strarray* from)
129 unsigned int i;
131 for (i = 0; i < from->size; i++)
132 strarray_add(arr, from->base[i]);
135 strarray* strarray_dup(const strarray* arr)
137 strarray* dup = strarray_alloc();
138 unsigned int i;
140 for (i = 0; i < arr->size; i++)
141 strarray_add(dup, arr->base[i]);
143 return dup;
146 strarray* strarray_fromstring(const char* str, const char* delim)
148 strarray* arr = strarray_alloc();
149 char* buf = strdup(str);
150 const char* tok;
152 for(tok = strtok(buf, delim); tok; tok = strtok(0, delim))
153 strarray_add(arr, strdup(tok));
155 free(buf);
156 return arr;
159 char* strarray_tostring(const strarray* arr, const char* sep)
161 char *str, *newstr;
162 unsigned int i;
164 str = strmake("%s", arr->base[0]);
165 for (i = 1; i < arr->size; i++)
167 newstr = strmake("%s%s%s", str, sep, arr->base[i]);
168 free(str);
169 str = newstr;
172 return str;
175 char* get_basename(const char* file)
177 const char* name;
178 char *base_name, *p;
180 if ((name = strrchr(file, '/'))) name++;
181 else name = file;
183 base_name = strdup(name);
184 if ((p = strrchr(base_name, '.'))) *p = 0;
186 return base_name;
189 void create_file(const char* name, int mode, const char* fmt, ...)
191 va_list ap;
192 FILE *file;
194 if (verbose) printf("Creating file %s\n", name);
195 va_start(ap, fmt);
196 if ( !(file = fopen(name, "w")) )
197 error("Unable to open %s for writing\n", name);
198 vfprintf(file, fmt, ap);
199 va_end(ap);
200 fclose(file);
201 chmod(name, mode);
204 file_type get_file_type(const char* filename)
206 /* see tools/winebuild/res32.c: check_header for details */
207 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 };
208 char buf[sizeof(res_sig)];
209 int fd, cnt;
211 fd = open( filename, O_RDONLY );
212 if (fd == -1) return file_na;
213 cnt = read(fd, buf, sizeof(buf));
214 close( fd );
215 if (cnt == -1) return file_na;
217 if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
218 if (strendswith(filename, ".o")) return file_obj;
219 if (strendswith(filename, ".a")) return file_arh;
220 if (strendswith(filename, ".res")) return file_res;
221 if (strendswith(filename, ".so")) return file_so;
222 if (strendswith(filename, ".dylib")) return file_so;
223 if (strendswith(filename, ".def")) return file_def;
224 if (strendswith(filename, ".spec")) return file_spec;
225 if (strendswith(filename, ".rc")) return file_rc;
227 return file_other;
230 static char* try_lib_path(const char* dir, const char* pre,
231 const char* library, const char* ext,
232 file_type expected_type)
234 char *fullname;
235 file_type type;
237 /* first try a subdir named from the library we are looking for */
238 fullname = strmake("%s/%s/%s%s%s", dir, library, pre, library, ext);
239 if (verbose > 1) fprintf(stderr, "Try %s...", fullname);
240 type = get_file_type(fullname);
241 if (verbose > 1) fprintf(stderr, type == expected_type ? "FOUND!\n" : "no\n");
242 if (type == expected_type) return fullname;
243 free( fullname );
245 fullname = strmake("%s/%s%s%s", dir, 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 );
251 return 0;
254 static file_type guess_lib_type(const char* dir, const char* library, char** file)
256 /* Unix shared object */
257 if ((*file = try_lib_path(dir, "lib", library, ".so", file_so)))
258 return file_so;
260 /* Mach-O (Darwin/Mac OS X) Dynamic Library behaves mostly like .so */
261 if ((*file = try_lib_path(dir, "lib", library, ".dylib", file_so)))
262 return file_so;
264 /* Windows DLL */
265 if ((*file = try_lib_path(dir, "lib", library, ".def", file_def)))
266 return file_dll;
267 if ((*file = try_lib_path(dir, "", library, ".def", file_def)))
268 return file_dll;
270 /* Unix static archives */
271 if ((*file = try_lib_path(dir, "lib", library, ".a", file_arh)))
272 return file_arh;
274 return file_na;
277 file_type get_lib_type(strarray* path, const char* library, char** file)
279 unsigned int i;
281 for (i = 0; i < path->size; i++)
283 file_type type = guess_lib_type(path->base[i], library, file);
284 if (type != file_na) return type;
286 return file_na;
289 void spawn(const strarray* prefix, const strarray* args, int ignore_errors)
291 unsigned int i;
292 int status;
293 strarray* arr = strarray_dup(args);
294 const char** argv;
295 char* prog = 0;
297 strarray_add(arr, NULL);
298 argv = arr->base;
300 if (prefix)
302 for (i = 0; i < prefix->size; i++)
304 const char* p;
305 struct stat st;
307 if (!(p = strrchr(argv[0], '/'))) p = argv[0];
308 free( prog );
309 prog = strmake("%s/%s", prefix->base[i], p);
310 if (stat(prog, &st) == 0)
312 if ((st.st_mode & S_IFREG) && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
314 argv[0] = prog;
315 break;
321 if (verbose)
323 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
324 printf("\n");
327 if ((status = spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
329 if (status > 0) error("%s failed\n", argv[0]);
330 else perror("winegcc");
331 exit(3);
334 free(prog);
335 strarray_free(arr);