xref: /haiku/src/system/runtime_loader/utility.cpp (revision 0562493379cd52eb7103531f895f10bb8e77c085)
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" uint32
62 __swap_int32(uint32 value)
63 {
64 	return value >> 24 | (value >> 8) & 0xff00 | value << 24
65 		| (value << 8) & 0xff0000;
66 }
67 
68 
69 // Copied from libroot/os/thread.c:
70 
71 
72 extern "C" status_t
73 _get_thread_info(thread_id thread, thread_info *info, size_t size)
74 {
75 	if (info == NULL || size != sizeof(thread_info))
76 		return B_BAD_VALUE;
77 
78 	return _kern_get_thread_info(thread, info);
79 }
80 
81 
82 status_t
83 snooze(bigtime_t timeout)
84 {
85 	return B_ERROR;
86 }
87