headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / runtime_loader / utility.cpp
blob6a9e0734b3d02b63c475d10c2fa042380ca4652e
1 /*
2 * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
5 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
7 */
10 #include "runtime_loader_private.h"
12 #include <syscalls.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
20 char *(*gGetEnv)(const char *name) = NULL;
23 extern "C" char *
24 getenv(const char *name)
26 if (gGetEnv != NULL) {
27 // Use libroot's getenv() as soon as it is available to us - the
28 // environment in gProgramArgs is static.
29 return gGetEnv(name);
32 char **environ = gProgramArgs->env;
33 int32 length = strlen(name);
34 int32 i;
36 for (i = 0; environ[i] != NULL; i++) {
37 if (!strncmp(name, environ[i], length) && environ[i][length] == '=')
38 return environ[i] + length + 1;
41 return NULL;
45 extern "C" int
46 printf(const char *format, ...)
48 char buffer[1024];
49 va_list args;
51 va_start(args, format);
52 int length = vsnprintf(buffer, sizeof(buffer), format, args);
53 va_end(args);
55 _kern_write(STDERR_FILENO, 0, buffer, length);
57 return length;
61 extern "C" void
62 dprintf(const char *format, ...)
64 char buffer[1024];
66 va_list list;
67 va_start(list, format);
69 vsnprintf(buffer, sizeof(buffer), format, list);
70 _kern_debug_output(buffer);
72 va_end(list);
75 #if __GNUC__ == 2
76 extern "C" uint32
77 __swap_int32(uint32 value)
79 return value >> 24 | ((value >> 8) & 0xff00) | value << 24
80 | ((value << 8) & 0xff0000);
82 #endif
85 // Copied from libroot/os/thread.c:
88 extern "C" status_t
89 _get_thread_info(thread_id thread, thread_info *info, size_t size)
91 if (info == NULL || size != sizeof(thread_info))
92 return B_BAD_VALUE;
94 return _kern_get_thread_info(thread, info);
98 extern "C" status_t
99 snooze(bigtime_t timeout)
101 return B_ERROR;
105 /*! Mini atoi(), so we don't have to include the libroot dependencies.
107 extern "C" int
108 atoi(const char* num)
110 int result = 0;
111 while (*num >= '0' && *num <= '9') {
112 result = (result * 10) + (*num - '0');
113 num++;
116 return result;
120 #if RUNTIME_LOADER_TRACING
122 extern "C" void
123 ktrace_printf(const char *format, ...)
125 va_list list;
126 va_start(list, format);
128 char buffer[1024];
129 vsnprintf(buffer, sizeof(buffer), format, list);
130 _kern_ktrace_output(buffer);
132 va_end(list);
135 #endif // RUNTIME_LOADER_TRACING