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)
55 if ((p = malloc (size)) == NULL)
56 error("Can not malloc %d bytes.", size);
61 void *xrealloc(void* p, size_t size)
64 if ((p2 = realloc (p, size)) == NULL)
65 error("Can not realloc %d bytes.", size);
70 int strendswith(const char* str, const char* end)
75 return l >= m && strcmp(str + l - m, end) == 0;
78 char* strmake(const char* fmt, ...)
89 n = vsnprintf (p, size, fmt, ap);
91 if (n > -1 && (size_t)n < size) return p;
92 size = min( size*2, (size_t)n+1 );
93 p = xrealloc (p, size);
97 strarray* strarray_alloc(void)
99 strarray* arr = xmalloc(sizeof(*arr));
100 arr->maximum = arr->size = 0;
105 void strarray_free(strarray* arr)
111 void strarray_add(strarray* arr, const char* str)
113 if (arr->size == arr->maximum)
116 arr->base = xrealloc(arr->base, sizeof(*(arr->base)) * arr->maximum);
118 arr->base[arr->size++] = str;
121 strarray* strarray_dup(const strarray* arr)
123 strarray* dup = strarray_alloc();
126 for (i = 0; i < arr->size; i++)
127 strarray_add(dup, arr->base[i]);
132 char* get_basename(const char* file)
137 if ((name = strrchr(file, '/'))) name++;
140 base_name = strdup(name);
141 if ((p = strrchr(base_name, '.'))) *p = 0;
146 void create_file(const char* name, const char* fmt, ...)
151 if (verbose) printf("Creating file %s\n", name);
153 if ( !(file = fopen(name, "w")) )
154 error ("Can not create %s.", name);
155 vfprintf(file, fmt, ap);
160 file_type get_file_type(const char* dir, const char* filename)
162 /* see tools/winebuild/res32.c: check_header for details */
163 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 };
164 char buf[sizeof(res_sig)];
168 fullname = strmake("%s/%s", dir, filename);
169 fd = open( fullname, O_RDONLY );
170 cnt = read(fd, buf, sizeof(buf));
171 if (cnt == -1) error("Can't read file: %s/%s", dir, filename);
175 if (fd == -1) return file_na;
177 if (cnt == sizeof(res_sig) && !memcmp(buf, res_sig, sizeof(res_sig))) return file_res;
178 if (strendswith(filename, ".o")) return file_obj;
179 if (strendswith(filename, ".a")) return file_arh;
180 if (strendswith(filename, ".res")) return file_res;
181 if (strendswith(filename, ".so")) return file_so;
182 if (strendswith(filename, ".def")) return file_dll;
183 if (strendswith(filename, ".rc")) return file_rc;
188 static file_type try_lib_path(const char* dir, const char* pre,
189 const char* library, const char* ext)
194 fullname = strmake("%s%s%s", pre, library, ext);
195 if (verbose > 1) fprintf(stderr, "Try %s/%s...", dir, fullname);
196 type = get_file_type(dir, fullname);
198 if (verbose > 1) fprintf(stderr, type == file_na ? "no\n" : "FOUND!\n");
202 static file_type guess_lib_type(const char* dir, const char* library)
204 /* Unix shared object */
205 if (try_lib_path(dir, "lib", library, ".so") == file_so)
209 if (try_lib_path(dir, "lib", library, ".def") == file_dll)
211 if (try_lib_path(dir, "", library, ".def") == file_dll)
214 /* Unix static archives */
215 if (try_lib_path(dir, "lib", library, ".a") == file_arh)
221 file_type get_lib_type(strarray* path, const char* library)
225 for (i = 0; i < path->size; i++)
227 file_type type = guess_lib_type(path->base[i], library);
228 if (type != file_na) return type;
233 void spawn(const strarray* args)
236 strarray* arr = strarray_dup(args);
237 const char **argv = arr->base;
239 strarray_add(arr, NULL);
242 for(i = 0; argv[i]; i++) printf("%s ", argv[i]);
246 if ((status = spawnvp( _P_WAIT, argv[0], argv)))
248 if (status > 0) error("%s failed.", argv[0]);
249 else perror("Error:");