Only export symbols starting with razor_ in dynamic library.
Apart from being good practice to avoid clashes with higher-level
libraries and the application, this also fixes an obscure bug: The
gnulib library is used both by librazor (the dynamic library) and
by razor (the executable). In doing so, we want to have two separate
copies of the library despite the code duplication this involves.
Without the explicit limit to export only razor_ symbols, the razor
executable under mingw64 was picking up the getopt_long function
from librazor and the optind variable from libgnu which meant that
it did not see optind changing. Hiding librazor's copy of getopt
causes the linker to find libgnu's copy and everything works.
Note that under mingw librazor-#.dll still contains undocumented
(private) razor_ symbols but these will do no harm as long as nobody
tries to use them.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012, 2014 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
37 #include <sys/utsname.h>
45 #include "razor-internal.h"
51 /* Required by gnulib on non-libc platforms */
52 char *program_name = "librazor";
66 razor_file_get_contents(const char *filename, size_t *length, int private,
67 struct razor_error **error)
75 fd = open(filename, O_RDONLY | O_BINARY);
77 razor_set_error_posix(error, filename);
81 if (fstat(fd, &st) < 0) {
82 razor_set_error_posix(error, filename);
90 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
91 if (addr == MAP_FAILED)
96 addr = malloc(st.st_size);
99 while(nb < st.st_size) {
100 res = read(fd, addr + nb, st.st_size - nb);
102 razor_set_error_posix(error, filename);
110 razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
111 "Not enough memory");
118 int razor_file_free_contents(void *addr, size_t length)
121 return munmap(addr, length);
128 struct qsort_context {
130 razor_compare_with_data_func_t compare;
135 qsort_swap(void *p1, void *p2, size_t size)
140 memcpy(buffer, p1, size);
141 memcpy(p1, p2, size);
142 memcpy(p2, buffer, size);
147 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
148 struct qsort_context *ctx)
150 void *p, *start, *end, *pivot;
151 uint32_t *mp, *mstart, *mend, tmp;
152 int left, right, result;
153 size_t size = ctx->size;
157 end = base + nelem * size;
161 pivot = base + (rand() % nelem) * size;
164 result = ctx->compare(p, pivot, ctx->data);
166 qsort_swap(p, start, size);
176 } else if (result == 0) {
182 qsort_swap(p, end, size);
191 left = (start - base) / size;
192 right = (base + nelem * size - end) / size;
194 __qsort_with_data(base, left, map, ctx);
196 __qsort_with_data(end, right, mend, ctx);
200 razor_qsort_with_data(void *base, size_t nelem, size_t size,
201 razor_compare_with_data_func_t compare, void *data)
203 struct qsort_context ctx;
211 ctx.compare = compare;
214 map = malloc(nelem * sizeof (uint32_t));
215 for (i = 0; i < nelem; i++)
218 __qsort_with_data(base, nelem, map, &ctx);
223 void environment_init(struct environment *env)
226 array_init(&env->string_pool);
227 array_init(&env->vars);
230 void environment_add_variable(struct environment *env,
231 const char *variable, const char *value)
235 assert(!env->is_set);
237 s = array_add(&env->string_pool,
238 strlen(variable) + strlen(value) + 2);
239 sprintf(s, "%s=%s", variable, value);
240 r = array_add(&env->vars, sizeof *r);
241 *r = s - (char *)env->string_pool.data;
244 void environment_set(struct environment *env)
254 count = env->vars.size / sizeof(uint32_t);
255 r = (uint32_t *)env->vars.data;
256 for (i = 0; i < count; i++) {
257 s = env->string_pool.data + *r++;
272 void environment_unset(struct environment *env)
279 count = env->vars.size / sizeof(uint32_t);
280 r = (uint32_t *)env->vars.data;
281 for (i = 0; i < count; i++) {
282 s = env->string_pool.data + *r++;
302 void environment_release(struct environment *env)
304 environment_unset(env);
305 array_release(&env->string_pool);
306 array_release(&env->vars);
309 RAZOR_EXPORT char *razor_concat(const char *s, ...)
319 while((string = va_arg(args, const char *)))
320 len += strlen(string);
324 concat = malloc(len + 1);
332 memcpy(concat, s, len);
334 while((string = va_arg(args, const char *))) {
335 len = strlen(string);
336 memcpy(concat + n, string, len);
348 * razor_path_add_root:
350 * Adds a root to a path. path must be an absolute pathname. In POSIX
351 * environments this is equivalent to the concationation of root and path.
352 * In Microsoft Windows an adjustment may need to be made for a drive letter
353 * in path (which will be dropped).
355 * Returns: The new pathname.
357 RAZOR_EXPORT char *razor_path_add_root(const char *path, const char *root)
360 return razor_concat(root, SKIP_DRIVE_LETTER(path), NULL);
365 RAZOR_EXPORT const char *razor_system_arch(void)
370 GetNativeSystemInfo(&si);
371 switch(si.wProcessorArchitecture)
373 case PROCESSOR_ARCHITECTURE_INTEL:
375 case PROCESSOR_ARCHITECTURE_AMD64:
381 static struct utsname un;
392 char *razor_utf16_to_utf8(const wchar_t *utf16, int len)
397 n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
398 if (len >= 0 && utf16[len])
401 (void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
402 if (len >= 0 && utf16[len])
408 wchar_t *razor_utf8_to_utf16(const char *utf8, int len)
413 n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
414 if (len >= 0 && utf8[len])
416 utf16 = malloc(n * sizeof(wchar_t));
417 (void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
418 if (len >= 0 && utf8[len])
424 #endif /* MSWIN_API */