Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 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 | |
| 50 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 |
| 51 | # include "TargetConditionals.h" |
| 52 | #endif |
| 53 | |
| 54 | #ifndef PJ_SYS_INFO_BUFFER_SIZE |
| 55 | # define PJ_SYS_INFO_BUFFER_SIZE 64 |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE |
| 60 | # include <sys/types.h> |
| 61 | # include <sys/sysctl.h> |
| 62 | void pj_iphone_os_get_sys_info(pj_sys_info *si, pj_str_t *si_buffer); |
| 63 | #endif |
| 64 | |
| 65 | #if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 66 | PJ_BEGIN_DECL |
| 67 | unsigned pj_symbianos_get_model_info(char *buf, unsigned buf_size); |
| 68 | unsigned pj_symbianos_get_platform_info(char *buf, unsigned buf_size); |
| 69 | void pj_symbianos_get_sdk_info(pj_str_t *name, pj_uint32_t *ver); |
| 70 | PJ_END_DECL |
| 71 | #endif |
| 72 | |
| 73 | |
| 74 | static char *ver_info(pj_uint32_t ver, char *buf) |
| 75 | { |
| 76 | pj_size_t len; |
| 77 | |
| 78 | if (ver == 0) { |
| 79 | *buf = '\0'; |
| 80 | return buf; |
| 81 | } |
| 82 | |
| 83 | sprintf(buf, "-%u.%u", |
| 84 | (ver & 0xFF000000) >> 24, |
| 85 | (ver & 0x00FF0000) >> 16); |
| 86 | len = strlen(buf); |
| 87 | |
| 88 | if (ver & 0xFFFF) { |
| 89 | sprintf(buf+len, ".%u", (ver & 0xFF00) >> 8); |
| 90 | len = strlen(buf); |
| 91 | |
| 92 | if (ver & 0x00FF) { |
| 93 | sprintf(buf+len, ".%u", (ver & 0xFF)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return buf; |
| 98 | } |
| 99 | |
| 100 | static pj_uint32_t parse_version(char *str) |
| 101 | { |
| 102 | char *tok; |
| 103 | int i, maxtok; |
| 104 | pj_uint32_t version = 0; |
| 105 | |
| 106 | while (*str && !pj_isdigit(*str)) |
| 107 | str++; |
| 108 | |
| 109 | maxtok = 4; |
| 110 | for (tok = strtok(str, ".-"), i=0; tok && i<maxtok; |
| 111 | ++i, tok=strtok(NULL, ".-")) |
| 112 | { |
| 113 | int n; |
| 114 | |
| 115 | if (!pj_isdigit(*tok)) |
| 116 | break; |
| 117 | |
| 118 | n = atoi(tok); |
| 119 | version |= (n << ((3-i)*8)); |
| 120 | } |
| 121 | |
| 122 | return version; |
| 123 | } |
| 124 | |
| 125 | PJ_DEF(const pj_sys_info*) pj_get_sys_info(void) |
| 126 | { |
| 127 | static char si_buffer[PJ_SYS_INFO_BUFFER_SIZE]; |
| 128 | static pj_sys_info si; |
| 129 | static pj_bool_t si_initialized; |
| 130 | pj_size_t left = PJ_SYS_INFO_BUFFER_SIZE, len; |
| 131 | |
| 132 | if (si_initialized) |
| 133 | return &si; |
| 134 | |
| 135 | si.machine.ptr = si.os_name.ptr = si.sdk_name.ptr = si.info.ptr = ""; |
| 136 | |
| 137 | #define ALLOC_CP_STR(str,field) \ |
| 138 | do { \ |
| 139 | len = pj_ansi_strlen(str); \ |
| 140 | if (len && left >= len+1) { \ |
| 141 | si.field.ptr = si_buffer + PJ_SYS_INFO_BUFFER_SIZE - left; \ |
| 142 | si.field.slen = len; \ |
| 143 | pj_memcpy(si.field.ptr, str, len+1); \ |
| 144 | left -= (len+1); \ |
| 145 | } \ |
| 146 | } while (0) |
| 147 | |
| 148 | /* |
| 149 | * Machine and OS info. |
| 150 | */ |
| 151 | #if defined(PJ_HAS_UNAME) && PJ_HAS_UNAME |
| 152 | #if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE && \ |
| 153 | (!defined TARGET_IPHONE_SIMULATOR || TARGET_IPHONE_SIMULATOR == 0) |
| 154 | { |
| 155 | pj_str_t buf = {si_buffer + PJ_SYS_INFO_BUFFER_SIZE - left, left}; |
| 156 | pj_str_t machine = {"arm-", 4}; |
| 157 | pj_str_t sdk_name = {"iOS-SDK", 7}; |
| 158 | size_t size = PJ_SYS_INFO_BUFFER_SIZE - machine.slen; |
| 159 | char tmp[PJ_SYS_INFO_BUFFER_SIZE]; |
| 160 | int name[] = {CTL_HW,HW_MACHINE}; |
| 161 | |
| 162 | pj_iphone_os_get_sys_info(&si, &buf); |
| 163 | left -= si.os_name.slen + 1; |
| 164 | |
| 165 | si.os_ver = parse_version(si.machine.ptr); |
| 166 | |
| 167 | pj_memcpy(tmp, machine.ptr, machine.slen); |
| 168 | sysctl(name, 2, tmp+machine.slen, &size, NULL, 0); |
| 169 | ALLOC_CP_STR(tmp, machine); |
| 170 | si.sdk_name = sdk_name; |
| 171 | |
| 172 | #ifdef PJ_SDK_NAME |
| 173 | pj_memcpy(tmp, PJ_SDK_NAME, pj_ansi_strlen(PJ_SDK_NAME) + 1); |
| 174 | si.sdk_ver = parse_version(tmp); |
| 175 | #endif |
| 176 | } |
| 177 | #else |
| 178 | { |
| 179 | struct utsname u; |
| 180 | |
| 181 | /* Successful uname() returns zero on Linux and positive value |
| 182 | * on OpenSolaris. |
| 183 | */ |
| 184 | if (uname(&u) == -1) |
| 185 | goto get_sdk_info; |
| 186 | |
| 187 | ALLOC_CP_STR(u.machine, machine); |
| 188 | ALLOC_CP_STR(u.sysname, os_name); |
| 189 | |
| 190 | si.os_ver = parse_version(u.release); |
| 191 | } |
| 192 | #endif |
| 193 | #elif defined(_MSC_VER) |
| 194 | { |
| 195 | OSVERSIONINFO ovi; |
| 196 | |
| 197 | ovi.dwOSVersionInfoSize = sizeof(ovi); |
| 198 | |
| 199 | if (GetVersionEx(&ovi) == FALSE) |
| 200 | goto get_sdk_info; |
| 201 | |
| 202 | si.os_ver = (ovi.dwMajorVersion << 24) | |
| 203 | (ovi.dwMinorVersion << 16); |
| 204 | #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE |
| 205 | si.os_name = pj_str("wince"); |
| 206 | #else |
| 207 | si.os_name = pj_str("win32"); |
| 208 | #endif |
| 209 | } |
| 210 | |
| 211 | { |
| 212 | SYSTEM_INFO wsi; |
| 213 | |
| 214 | GetSystemInfo(&wsi); |
| 215 | switch (wsi.wProcessorArchitecture) { |
| 216 | #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE |
| 217 | case PROCESSOR_ARCHITECTURE_ARM: |
| 218 | si.machine = pj_str("arm"); |
| 219 | break; |
| 220 | case PROCESSOR_ARCHITECTURE_SHX: |
| 221 | si.machine = pj_str("shx"); |
| 222 | break; |
| 223 | #else |
| 224 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 225 | si.machine = pj_str("x86_64"); |
| 226 | break; |
| 227 | case PROCESSOR_ARCHITECTURE_IA64: |
| 228 | si.machine = pj_str("ia64"); |
| 229 | break; |
| 230 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 231 | si.machine = pj_str("i386"); |
| 232 | break; |
| 233 | #endif /* PJ_WIN32_WINCE */ |
| 234 | } |
| 235 | } |
| 236 | #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 237 | { |
| 238 | pj_symbianos_get_model_info(si_buffer, sizeof(si_buffer)); |
| 239 | ALLOC_CP_STR(si_buffer, machine); |
| 240 | |
| 241 | char *p = si_buffer + sizeof(si_buffer) - left; |
| 242 | unsigned plen; |
| 243 | plen = pj_symbianos_get_platform_info(p, left); |
| 244 | if (plen) { |
| 245 | /* Output format will be "Series60vX.X" */ |
| 246 | si.os_name = pj_str("S60"); |
| 247 | si.os_ver = parse_version(p+9); |
| 248 | } else { |
| 249 | si.os_name = pj_str("Unknown"); |
| 250 | } |
| 251 | |
| 252 | /* Avoid compile warning on Symbian. */ |
| 253 | goto get_sdk_info; |
| 254 | } |
| 255 | #endif |
| 256 | |
| 257 | /* |
| 258 | * SDK info. |
| 259 | */ |
| 260 | get_sdk_info: |
| 261 | |
| 262 | #if defined(__GLIBC__) |
| 263 | si.sdk_ver = (__GLIBC__ << 24) | |
| 264 | (__GLIBC_MINOR__ << 16); |
| 265 | si.sdk_name = pj_str("glibc"); |
| 266 | #elif defined(__GNU_LIBRARY__) |
| 267 | si.sdk_ver = (__GNU_LIBRARY__ << 24) | |
| 268 | (__GNU_LIBRARY_MINOR__ << 16); |
| 269 | si.sdk_name = pj_str("libc"); |
| 270 | #elif defined(__UCLIBC__) |
| 271 | si.sdk_ver = (__UCLIBC_MAJOR__ << 24) | |
| 272 | (__UCLIBC_MINOR__ << 16); |
| 273 | si.sdk_name = pj_str("uclibc"); |
| 274 | #elif defined(_WIN32_WCE) && _WIN32_WCE |
| 275 | /* Old window mobile declares _WIN32_WCE as decimal (e.g. 300, 420, etc.), |
| 276 | * but then it was changed to use hex, e.g. 0x420, etc. See |
| 277 | * http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesnative/thread/8a97c59f-5a1c-4bc6-99e6-427f065ff439/ |
| 278 | */ |
| 279 | #if _WIN32_WCE <= 500 |
| 280 | si.sdk_ver = ( (_WIN32_WCE / 100) << 24) | |
| 281 | ( ((_WIN32_WCE % 100) / 10) << 16) | |
| 282 | ( (_WIN32_WCE % 10) << 8); |
| 283 | #else |
| 284 | si.sdk_ver = ( ((_WIN32_WCE & 0xFF00) >> 8) << 24) | |
| 285 | ( ((_WIN32_WCE & 0x00F0) >> 4) << 16) | |
| 286 | ( ((_WIN32_WCE & 0x000F) >> 0) << 8); |
| 287 | #endif |
| 288 | si.sdk_name = pj_str("cesdk"); |
| 289 | #elif defined(_MSC_VER) |
| 290 | /* No SDK info is easily obtainable for Visual C, so lets just use |
| 291 | * _MSC_VER. The _MSC_VER macro reports the major and minor versions |
| 292 | * of the compiler. For example, 1310 for Microsoft Visual C++ .NET 2003. |
| 293 | * 1310 represents version 13 and a 1.0 point release. |
| 294 | * The Visual C++ 2005 compiler version is 1400. |
| 295 | */ |
| 296 | si.sdk_ver = ((_MSC_VER / 100) << 24) | |
| 297 | (((_MSC_VER % 100) / 10) << 16) | |
| 298 | ((_MSC_VER % 10) << 8); |
| 299 | si.sdk_name = pj_str("msvc"); |
| 300 | #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0 |
| 301 | pj_symbianos_get_sdk_info(&si.sdk_name, &si.sdk_ver); |
| 302 | #endif |
| 303 | |
| 304 | /* |
| 305 | * Build the info string. |
| 306 | */ |
| 307 | { |
| 308 | char tmp[PJ_SYS_INFO_BUFFER_SIZE]; |
| 309 | char os_ver[20], sdk_ver[20]; |
| 310 | int cnt; |
| 311 | |
| 312 | cnt = pj_ansi_snprintf(tmp, sizeof(tmp), |
| 313 | "%s%s%s%s%s%s%s", |
| 314 | si.os_name.ptr, |
| 315 | ver_info(si.os_ver, os_ver), |
| 316 | (si.machine.slen ? "/" : ""), |
| 317 | si.machine.ptr, |
| 318 | (si.sdk_name.slen ? "/" : ""), |
| 319 | si.sdk_name.ptr, |
| 320 | ver_info(si.sdk_ver, sdk_ver)); |
| 321 | if (cnt > 0 && cnt < (int)sizeof(tmp)) { |
| 322 | ALLOC_CP_STR(tmp, info); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | si_initialized = PJ_TRUE; |
| 327 | return &si; |
| 328 | } |