Use Windows KTM (atomic transactions) where supported.
Increment current header version to 2
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011 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";
55 razor_file_get_contents(const char *filename, size_t *length)
65 fd = open(filename, O_RDONLY | O_BINARY);
69 if (fstat(fd, &st) < 0) {
76 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
78 addr = malloc(st.st_size);
81 while(nb < st.st_size) {
82 res = read(fd, addr + nb, st.st_size - nb);
95 if (addr == MAP_FAILED)
102 razor_file_free_contents(void *addr, size_t length)
105 return munmap(addr, length);
112 struct qsort_context {
114 razor_compare_with_data_func_t compare;
119 qsort_swap(void *p1, void *p2, size_t size)
123 memcpy(buffer, p1, size);
124 memcpy(p1, p2, size);
125 memcpy(p2, buffer, size);
129 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
130 struct qsort_context *ctx)
132 void *p, *start, *end, *pivot;
133 uint32_t *mp, *mstart, *mend, tmp;
134 int left, right, result;
135 size_t size = ctx->size;
139 end = base + nelem * size;
143 pivot = base + (rand() % nelem) * size;
146 result = ctx->compare(p, pivot, ctx->data);
148 qsort_swap(p, start, size);
158 } else if (result == 0) {
164 qsort_swap(p, end, size);
173 left = (start - base) / size;
174 right = (base + nelem * size - end) / size;
176 __qsort_with_data(base, left, map, ctx);
178 __qsort_with_data(end, right, mend, ctx);
182 razor_qsort_with_data(void *base, size_t nelem, size_t size,
183 razor_compare_with_data_func_t compare, void *data)
185 struct qsort_context ctx;
193 ctx.compare = compare;
196 map = malloc(nelem * sizeof (uint32_t));
197 for (i = 0; i < nelem; i++)
200 __qsort_with_data(base, nelem, map, &ctx);
205 void environment_init(struct environment *env)
208 array_init(&env->string_pool);
209 array_init(&env->vars);
212 void environment_add_variable(struct environment *env,
213 const char *variable, const char *value)
217 assert(!env->is_set);
219 s = array_add(&env->string_pool,
220 strlen(variable) + strlen(value) + 2);
221 sprintf(s, "%s=%s", variable, value);
222 r = array_add(&env->vars, sizeof *r);
223 *r = s - (char *)env->string_pool.data;
226 void environment_set(struct environment *env)
233 count = env->vars.size / sizeof(uint32_t);
234 r = (uint32_t *)env->vars.data;
235 for (i = 0; i < count; i++) {
236 s = env->string_pool.data + *r++;
244 void environment_unset(struct environment *env)
251 count = env->vars.size / sizeof(uint32_t);
252 r = (uint32_t *)env->vars.data;
253 for (i = 0; i < count; i++) {
254 s = env->string_pool.data + *r++;
255 t = strchr(s, '=') + 1;
266 void environment_release(struct environment *env)
268 environment_unset(env);
269 array_release(&env->string_pool);
270 array_release(&env->vars);
273 RAZOR_EXPORT char *razor_concat(const char *s, ...)
283 while((string = va_arg(args, const char *)))
284 len += strlen(string);
288 concat = malloc(len + 1);
296 memcpy(concat, s, len);
298 while((string = va_arg(args, const char *))) {
299 len = strlen(string);
300 memcpy(concat + n, string, len);
311 RAZOR_EXPORT const char *razor_system_arch(void)
316 GetNativeSystemInfo(&si);
317 switch(si.wProcessorArchitecture)
319 case PROCESSOR_ARCHITECTURE_INTEL:
321 case PROCESSOR_ARCHITECTURE_AMD64:
327 static struct utsname un;