Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pj/os.h> |
| 21 | #include <pj/ctype.h> |
| 22 | #include <pj/errno.h> |
| 23 | #include <pj/string.h> |
| 24 | |
| 25 | /* |
| 26 | * FYI these links contain useful infos about predefined macros across |
| 27 | * platforms: |
| 28 | * - http://predef.sourceforge.net/preos.html |
| 29 | */ |
| 30 | |
| 31 | #if defined(PJ_HAS_SYS_UTSNAME_H) && PJ_HAS_SYS_UTSNAME_H != 0 |
| 32 | /* For uname() */ |
| 33 | # include <sys/utsname.h> |
| 34 | # include <stdlib.h> |
| 35 | # define PJ_HAS_UNAME 1 |
| 36 | #endif |
| 37 | |
| 38 | #if defined(PJ_HAS_LIMITS_H) && PJ_HAS_LIMITS_H != 0 |
| 39 | /* Include <limits.h> to get <features.h> to get various glibc macros. |
| 40 | * See http://predef.sourceforge.net/prelib.html |
| 41 | */ |
| 42 | # include <limits.h> |
| 43 | #endif |
| 44 | |
| 45 | #if defined(_MSC_VER) |
| 46 | /* For all Windows including mobile */ |
| 47 | # include <windows.h> |
| 48 | #endif |
| 49 | |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 50 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 |
| 51 | # include "TargetConditionals.h" |
| 52 | #endif |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 53 | |
| 54 | #ifndef PJ_SYS_INFO_BUFFER_SIZE |
| 55 | # define PJ_SYS_INFO_BUFFER_SIZE 64 |
| 56 | #endif |
| 57 | |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 58 | |
| 59 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE |
| 60 | void pj_iphone_os_get_sys_info(pj_sys_info *si, pj_str_t *si_buffer); |
| 61 | #endif |
Nanang Izzuddin | fea635a | 2011-03-08 06:30:34 +0000 | [diff] [blame^] | 62 | |
| 63 | #if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 64 | PJ_BEGIN_DECL |
| 65 | unsigned pj_symbianos_get_model_info(char *buf, unsigned buf_size); |
| 66 | unsigned pj_symbianos_get_platform_info(char *buf, unsigned buf_size); |
| 67 | void pj_symbianos_get_sdk_info(pj_str_t *name, pj_uint32_t *ver); |
| 68 | PJ_END_DECL |
| 69 | #endif |
| 70 | |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 71 | |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 72 | static char *ver_info(pj_uint32_t ver, char *buf) |
| 73 | { |
| 74 | int len; |
| 75 | |
| 76 | if (ver == 0) { |
| 77 | *buf = '\0'; |
| 78 | return buf; |
| 79 | } |
| 80 | |
| 81 | sprintf(buf, "-%u.%u", |
| 82 | (ver & 0xFF000000) >> 24, |
| 83 | (ver & 0x00FF0000) >> 16); |
| 84 | len = strlen(buf); |
| 85 | |
| 86 | if (ver & 0xFFFF) { |
| 87 | sprintf(buf+len, ".%u", (ver & 0xFF00) >> 8); |
| 88 | len = strlen(buf); |
| 89 | |
| 90 | if (ver & 0x00FF) { |
| 91 | sprintf(buf+len, ".%u", (ver & 0xFF)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return buf; |
| 96 | } |
| 97 | |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 98 | static pj_uint32_t parse_version(char *str) |
| 99 | { |
| 100 | char *tok; |
| 101 | int i, maxtok; |
| 102 | pj_uint32_t version = 0; |
| 103 | |
| 104 | while (*str && !pj_isdigit(*str)) |
| 105 | str++; |
| 106 | |
| 107 | maxtok = 4; |
| 108 | for (tok = strtok(str, ".-"), i=0; tok && i<maxtok; |
| 109 | ++i, tok=strtok(NULL, ".-")) |
| 110 | { |
| 111 | int n; |
| 112 | |
| 113 | if (!pj_isdigit(*tok)) |
| 114 | break; |
| 115 | |
| 116 | n = atoi(tok); |
| 117 | version |= (n << ((3-i)*8)); |
| 118 | } |
| 119 | |
| 120 | return version; |
| 121 | } |
| 122 | |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 123 | PJ_DEF(const pj_sys_info*) pj_get_sys_info(void) |
| 124 | { |
| 125 | static char si_buffer[PJ_SYS_INFO_BUFFER_SIZE]; |
| 126 | static pj_sys_info si; |
| 127 | static pj_bool_t si_initialized; |
| 128 | unsigned left = PJ_SYS_INFO_BUFFER_SIZE, len; |
| 129 | |
| 130 | if (si_initialized) |
| 131 | return &si; |
| 132 | |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 133 | si.machine.ptr = si.os_name.ptr = si.sdk_name.ptr = si.info.ptr = ""; |
| 134 | |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 135 | #define ALLOC_CP_STR(str,field) \ |
| 136 | do { \ |
| 137 | len = pj_ansi_strlen(str); \ |
| 138 | if (len && left >= len+1) { \ |
| 139 | si.field.ptr = si_buffer + PJ_SYS_INFO_BUFFER_SIZE - left; \ |
| 140 | si.field.slen = len; \ |
| 141 | pj_memcpy(si.field.ptr, str, len+1); \ |
| 142 | left -= (len+1); \ |
| 143 | } \ |
| 144 | } while (0) |
| 145 | |
| 146 | /* |
| 147 | * Machine and OS info. |
| 148 | */ |
| 149 | #if defined(PJ_HAS_UNAME) && PJ_HAS_UNAME |
Sauw Ming | 0b7864e | 2011-03-02 02:44:57 +0000 | [diff] [blame] | 150 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE && \ |
Sauw Ming | c808952 | 2011-03-02 10:53:24 +0000 | [diff] [blame] | 151 | (!defined TARGET_IPHONE_SIMULATOR || TARGET_IPHONE_SIMULATOR == 0) |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 152 | { |
| 153 | pj_str_t buf = {si_buffer + PJ_SYS_INFO_BUFFER_SIZE - left, left}; |
| 154 | pj_str_t machine = {"arm", 3}; |
| 155 | pj_str_t sdk_name = {"iOS-SDK", 7}; |
| 156 | char tmp[PJ_SYS_INFO_BUFFER_SIZE]; |
| 157 | |
| 158 | pj_iphone_os_get_sys_info(&si, &buf); |
| 159 | left -= si.os_name.slen + 1; |
| 160 | |
| 161 | si.os_ver = parse_version(si.machine.ptr); |
| 162 | |
| 163 | si.machine = machine; |
| 164 | si.sdk_name = sdk_name; |
| 165 | |
Sauw Ming | 0b7864e | 2011-03-02 02:44:57 +0000 | [diff] [blame] | 166 | #ifdef PJ_SDK_NAME |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 167 | pj_memcpy(tmp, PJ_SDK_NAME, pj_ansi_strlen(PJ_SDK_NAME) + 1); |
| 168 | si.sdk_ver = parse_version(tmp); |
Sauw Ming | 0b7864e | 2011-03-02 02:44:57 +0000 | [diff] [blame] | 169 | #endif |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 170 | } |
| 171 | #else |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 172 | { |
| 173 | struct utsname u; |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 174 | |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 175 | /* Successful uname() returns zero on Linux and positive value |
| 176 | * on OpenSolaris. |
| 177 | */ |
| 178 | if (uname(&u) == -1) |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 179 | goto get_sdk_info; |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 180 | |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 181 | ALLOC_CP_STR(u.machine, machine); |
| 182 | ALLOC_CP_STR(u.sysname, os_name); |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 183 | |
| 184 | si.os_ver = parse_version(u.release); |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 185 | } |
Sauw Ming | 6a970a3 | 2011-03-01 05:25:27 +0000 | [diff] [blame] | 186 | #endif |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 187 | #elif defined(_MSC_VER) |
| 188 | { |
| 189 | OSVERSIONINFO ovi; |
| 190 | |
| 191 | ovi.dwOSVersionInfoSize = sizeof(ovi); |
| 192 | |
Benny Prijono | de5c5ed | 2011-02-28 08:28:48 +0000 | [diff] [blame] | 193 | if (GetVersionEx(&ovi) == FALSE) |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 194 | goto get_sdk_info; |
| 195 | |
| 196 | si.os_ver = (ovi.dwMajorVersion << 24) | |
| 197 | (ovi.dwMinorVersion << 16); |
| 198 | #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE |
| 199 | si.os_name = pj_str("wince"); |
| 200 | #else |
| 201 | si.os_name = pj_str("win32"); |
| 202 | #endif |
| 203 | } |
| 204 | |
| 205 | { |
| 206 | SYSTEM_INFO wsi; |
| 207 | |
| 208 | GetSystemInfo(&wsi); |
| 209 | switch (wsi.wProcessorArchitecture) { |
| 210 | #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE |
| 211 | case PROCESSOR_ARCHITECTURE_ARM: |
| 212 | si.machine = pj_str("arm"); |
| 213 | break; |
| 214 | case PROCESSOR_ARCHITECTURE_SHX: |
| 215 | si.machine = pj_str("shx"); |
| 216 | break; |
| 217 | #else |
| 218 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 219 | si.machine = pj_str("x86_64"); |
| 220 | break; |
| 221 | case PROCESSOR_ARCHITECTURE_IA64: |
| 222 | si.machine = pj_str("ia64"); |
| 223 | break; |
| 224 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 225 | si.machine = pj_str("i386"); |
| 226 | break; |
| 227 | #endif /* PJ_WIN32_WINCE */ |
| 228 | } |
| 229 | } |
Nanang Izzuddin | fea635a | 2011-03-08 06:30:34 +0000 | [diff] [blame^] | 230 | #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 231 | { |
| 232 | pj_symbianos_get_model_info(si_buffer, sizeof(si_buffer)); |
| 233 | ALLOC_CP_STR(si_buffer, machine); |
| 234 | |
| 235 | char *p = si_buffer + sizeof(si_buffer) - left; |
| 236 | unsigned plen; |
| 237 | plen = pj_symbianos_get_platform_info(p, left); |
| 238 | if (plen) { |
| 239 | /* Output format will be "Series60vX.X" */ |
| 240 | si.os_name = pj_str("S60"); |
| 241 | si.os_ver = parse_version(p+9); |
| 242 | } else { |
| 243 | si.os_name = pj_str("Unknown"); |
| 244 | } |
| 245 | } |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 246 | #endif |
| 247 | |
| 248 | /* |
| 249 | * SDK info. |
| 250 | */ |
| 251 | get_sdk_info: |
| 252 | |
| 253 | #if defined(__GLIBC__) |
| 254 | si.sdk_ver = (__GLIBC__ << 24) | |
| 255 | (__GLIBC_MINOR__ << 16); |
| 256 | si.sdk_name = pj_str("glibc"); |
| 257 | #elif defined(__GNU_LIBRARY__) |
| 258 | si.sdk_ver = (__GNU_LIBRARY__ << 24) | |
| 259 | (__GNU_LIBRARY_MINOR__ << 16); |
| 260 | si.sdk_name = pj_str("libc"); |
| 261 | #elif defined(__UCLIBC__) |
| 262 | si.sdk_ver = (__UCLIBC_MAJOR__ << 24) | |
| 263 | (__UCLIBC_MINOR__ << 16); |
| 264 | si.sdk_name = pj_str("uclibc"); |
| 265 | #elif defined(_WIN32_WCE) && _WIN32_WCE |
| 266 | /* Old window mobile declares _WIN32_WCE as decimal (e.g. 300, 420, etc.), |
| 267 | * but then it was changed to use hex, e.g. 0x420, etc. See |
| 268 | * http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesnative/thread/8a97c59f-5a1c-4bc6-99e6-427f065ff439/ |
| 269 | */ |
| 270 | #if _WIN32_WCE <= 500 |
| 271 | si.sdk_ver = ( (_WIN32_WCE / 100) << 24) | |
| 272 | ( ((_WIN32_WCE % 100) / 10) << 16) | |
| 273 | ( (_WIN32_WCE % 10) << 8); |
| 274 | #else |
| 275 | si.sdk_ver = ( ((_WIN32_WCE & 0xFF00) >> 8) << 24) | |
| 276 | ( ((_WIN32_WCE & 0x00F0) >> 4) << 16) | |
| 277 | ( ((_WIN32_WCE & 0x000F) >> 0) << 8); |
| 278 | #endif |
| 279 | si.sdk_name = pj_str("cesdk"); |
| 280 | #elif defined(_MSC_VER) |
| 281 | /* No SDK info is easily obtainable for Visual C, so lets just use |
| 282 | * _MSC_VER. The _MSC_VER macro reports the major and minor versions |
| 283 | * of the compiler. For example, 1310 for Microsoft Visual C++ .NET 2003. |
| 284 | * 1310 represents version 13 and a 1.0 point release. |
| 285 | * The Visual C++ 2005 compiler version is 1400. |
| 286 | */ |
| 287 | si.sdk_ver = ((_MSC_VER / 100) << 24) | |
| 288 | (((_MSC_VER % 100) / 10) << 16) | |
| 289 | ((_MSC_VER % 10) << 8); |
| 290 | si.sdk_name = pj_str("msvc"); |
Nanang Izzuddin | fea635a | 2011-03-08 06:30:34 +0000 | [diff] [blame^] | 291 | #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 292 | pj_symbianos_get_sdk_info(&si.sdk_name, &si.sdk_ver); |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 293 | #endif |
| 294 | |
| 295 | /* |
| 296 | * Build the info string. |
| 297 | */ |
| 298 | { |
| 299 | char tmp[PJ_SYS_INFO_BUFFER_SIZE]; |
| 300 | char os_ver[20], sdk_ver[20]; |
Benny Prijono | de5c5ed | 2011-02-28 08:28:48 +0000 | [diff] [blame] | 301 | int cnt; |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 302 | |
Benny Prijono | de5c5ed | 2011-02-28 08:28:48 +0000 | [diff] [blame] | 303 | cnt = pj_ansi_snprintf(tmp, sizeof(tmp), |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 304 | "%s%s%s%s%s%s%s", |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 305 | si.os_name.ptr, |
| 306 | ver_info(si.os_ver, os_ver), |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 307 | (si.machine.slen ? "/" : ""), |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 308 | si.machine.ptr, |
Benny Prijono | 4df37e9 | 2011-02-28 22:19:23 +0000 | [diff] [blame] | 309 | (si.sdk_name.slen ? "/" : ""), |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 310 | si.sdk_name.ptr, |
| 311 | ver_info(si.sdk_ver, sdk_ver)); |
Benny Prijono | de5c5ed | 2011-02-28 08:28:48 +0000 | [diff] [blame] | 312 | if (cnt > 0 && cnt < (int)sizeof(tmp)) { |
Benny Prijono | e3e6580 | 2011-02-28 07:44:19 +0000 | [diff] [blame] | 313 | ALLOC_CP_STR(tmp, info); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | si_initialized = PJ_TRUE; |
| 318 | return &si; |
| 319 | } |