xref: /haiku/src/system/runtime_loader/utility.cpp (revision 99d027cd0238c1d86da86d7c3f4200509ccc61a6)
1 /*
2  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 
9 
10 #include "runtime_loader_private.h"
11 
12 #include <syscalls.h>
13 
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 
20 char *(*gGetEnv)(const char *name) = NULL;
21 
22 
23 extern "C" char *
24 getenv(const char *name)
25 {
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);
30 	}
31 
32 	char **environ = gProgramArgs->env;
33 	int32 length = strlen(name);
34 	int32 i;
35 
36 	for (i = 0; environ[i] != NULL; i++) {
37 		if (!strncmp(name, environ[i], length) && environ[i][length] == '=')
38 			return environ[i] + length + 1;
39 	}
40 
41 	return NULL;
42 }
43 
44 
45 extern "C" int
46 printf(const char *format, ...)
47 {
48 	char buffer[1024];
49 	va_list args;
50 
51 	va_start(args, format);
52 	int length = vsnprintf(buffer, sizeof(buffer), format, args);
53 	va_end(args);
54 
55 	_kern_write(STDERR_FILENO, 0, buffer, length);
56 
57 	return length;
58 }
59 
60 
61 extern "C" void
62 dprintf(const char *format, ...)
63 {
64 	char buffer[1024];
65 
66 	va_list list;
67 	va_start(list, format);
68 
69 	vsnprintf(buffer, sizeof(buffer), format, list);
70 	_kern_debug_output(buffer);
71 
72 	va_end(list);
73 }
74 
75 
76 extern "C" uint32
77 __swap_int32(uint32 value)
78 {
79 	return value >> 24 | ((value >> 8) & 0xff00) | value << 24
80 		| ((value << 8) & 0xff0000);
81 }
82 
83 
84 // Copied from libroot/os/thread.c:
85 
86 
87 extern "C" status_t
88 _get_thread_info(thread_id thread, thread_info *info, size_t size)
89 {
90 	if (info == NULL || size != sizeof(thread_info))
91 		return B_BAD_VALUE;
92 
93 	return _kern_get_thread_info(thread, info);
94 }
95 
96 
97 extern "C" status_t
98 snooze(bigtime_t timeout)
99 {
100 	return B_ERROR;
101 }
102 
103 
104 /*!	Mini atoi(), so we don't have to include the libroot dependencies.
105  */
106 extern "C" int
107 atoi(const char* num)
108 {
109 	int result = 0;
110 	while (*num >= '0' && *num <= '9') {
111 		result = (result * 10) + (*num - '0');
112 		num++;
113 	}
114 
115 	return result;
116 }
117 
118 
119 #if RUNTIME_LOADER_TRACING
120 
121 extern "C" void
122 ktrace_printf(const char *format, ...)
123 {
124 	va_list list;
125 	va_start(list, format);
126 
127 	char buffer[1024];
128 	vsnprintf(buffer, sizeof(buffer), format, list);
129 	_kern_ktrace_output(buffer);
130 
131 	va_end(list);
132 }
133 
134 #endif // RUNTIME_LOADER_TRACING
135