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 #if __GNUC__ == 2 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 #endif 83 84 85 // Copied from libroot/os/thread.c: 86 87 88 extern "C" status_t 89 _get_thread_info(thread_id thread, thread_info *info, size_t size) 90 { 91 if (info == NULL || size != sizeof(thread_info)) 92 return B_BAD_VALUE; 93 94 return _kern_get_thread_info(thread, info); 95 } 96 97 98 extern "C" status_t 99 snooze(bigtime_t timeout) 100 { 101 return B_ERROR; 102 } 103 104 105 /*! Mini atoi(), so we don't have to include the libroot dependencies. 106 */ 107 extern "C" int 108 atoi(const char* num) 109 { 110 int result = 0; 111 while (*num >= '0' && *num <= '9') { 112 result = (result * 10) + (*num - '0'); 113 num++; 114 } 115 116 return result; 117 } 118 119 120 #if RUNTIME_LOADER_TRACING 121 122 extern "C" void 123 ktrace_printf(const char *format, ...) 124 { 125 va_list list; 126 va_start(list, format); 127 128 char buffer[1024]; 129 vsnprintf(buffer, sizeof(buffer), format, list); 130 _kern_ktrace_output(buffer); 131 132 va_end(list); 133 } 134 135 #endif // RUNTIME_LOADER_TRACING 136