1
/* $Id: os_info.c 4411 2013-03-04 04:34:38Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
23
#include <pj/string.h>
26
* FYI these links contain useful infos about predefined macros across
28
* - http://predef.sourceforge.net/preos.html
31
#if defined(PJ_HAS_SYS_UTSNAME_H) && PJ_HAS_SYS_UTSNAME_H != 0
33
# include <sys/utsname.h>
35
# define PJ_HAS_UNAME 1
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
46
/* For all Windows including mobile */
50
#if defined(PJ_DARWINOS) && PJ_DARWINOS != 0
51
# include "TargetConditionals.h"
54
#ifndef PJ_SYS_INFO_BUFFER_SIZE
55
# define PJ_SYS_INFO_BUFFER_SIZE 64
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);
63
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
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);
72
static char *ver_info(pj_uint32_t ver, char *buf)
81
sprintf(buf, "-%u.%u",
82
(ver & 0xFF000000) >> 24,
83
(ver & 0x00FF0000) >> 16);
87
sprintf(buf+len, ".%u", (ver & 0xFF00) >> 8);
91
sprintf(buf+len, ".%u", (ver & 0xFF));
98
static pj_uint32_t parse_version(char *str)
102
pj_uint32_t version = 0;
104
while (*str && !pj_isdigit(*str))
108
for (tok = strtok(str, ".-"), i=0; tok && i<maxtok;
109
++i, tok=strtok(NULL, ".-"))
113
if (!pj_isdigit(*tok))
117
version |= (n << ((3-i)*8));
123
PJ_DEF(const pj_sys_info*) pj_get_sys_info(void)
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;
133
si.machine.ptr = si.os_name.ptr = si.sdk_name.ptr = si.info.ptr = "";
135
#define ALLOC_CP_STR(str,field) \
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); \
147
* Machine and OS info.
149
#if defined(PJ_HAS_UNAME) && PJ_HAS_UNAME
150
#if defined(PJ_DARWINOS) && PJ_DARWINOS != 0 && TARGET_OS_IPHONE && \
151
(!defined TARGET_IPHONE_SIMULATOR || TARGET_IPHONE_SIMULATOR == 0)
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];
158
pj_iphone_os_get_sys_info(&si, &buf);
159
left -= si.os_name.slen + 1;
161
si.os_ver = parse_version(si.machine.ptr);
163
si.machine = machine;
164
si.sdk_name = sdk_name;
167
pj_memcpy(tmp, PJ_SDK_NAME, pj_ansi_strlen(PJ_SDK_NAME) + 1);
168
si.sdk_ver = parse_version(tmp);
175
/* Successful uname() returns zero on Linux and positive value
181
ALLOC_CP_STR(u.machine, machine);
182
ALLOC_CP_STR(u.sysname, os_name);
184
si.os_ver = parse_version(u.release);
187
#elif defined(_MSC_VER)
191
ovi.dwOSVersionInfoSize = sizeof(ovi);
193
if (GetVersionEx(&ovi) == FALSE)
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");
201
si.os_name = pj_str("win32");
209
switch (wsi.wProcessorArchitecture) {
210
#if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE
211
case PROCESSOR_ARCHITECTURE_ARM:
212
si.machine = pj_str("arm");
214
case PROCESSOR_ARCHITECTURE_SHX:
215
si.machine = pj_str("shx");
218
case PROCESSOR_ARCHITECTURE_AMD64:
219
si.machine = pj_str("x86_64");
221
case PROCESSOR_ARCHITECTURE_IA64:
222
si.machine = pj_str("ia64");
224
case PROCESSOR_ARCHITECTURE_INTEL:
225
si.machine = pj_str("i386");
227
#endif /* PJ_WIN32_WINCE */
230
#elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
232
pj_symbianos_get_model_info(si_buffer, sizeof(si_buffer));
233
ALLOC_CP_STR(si_buffer, machine);
235
char *p = si_buffer + sizeof(si_buffer) - left;
237
plen = pj_symbianos_get_platform_info(p, left);
239
/* Output format will be "Series60vX.X" */
240
si.os_name = pj_str("S60");
241
si.os_ver = parse_version(p+9);
243
si.os_name = pj_str("Unknown");
246
/* Avoid compile warning on Symbian. */
256
#if defined(__GLIBC__)
257
si.sdk_ver = (__GLIBC__ << 24) |
258
(__GLIBC_MINOR__ << 16);
259
si.sdk_name = pj_str("glibc");
260
#elif defined(__GNU_LIBRARY__)
261
si.sdk_ver = (__GNU_LIBRARY__ << 24) |
262
(__GNU_LIBRARY_MINOR__ << 16);
263
si.sdk_name = pj_str("libc");
264
#elif defined(__UCLIBC__)
265
si.sdk_ver = (__UCLIBC_MAJOR__ << 24) |
266
(__UCLIBC_MINOR__ << 16);
267
si.sdk_name = pj_str("uclibc");
268
#elif defined(_WIN32_WCE) && _WIN32_WCE
269
/* Old window mobile declares _WIN32_WCE as decimal (e.g. 300, 420, etc.),
270
* but then it was changed to use hex, e.g. 0x420, etc. See
271
* http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesnative/thread/8a97c59f-5a1c-4bc6-99e6-427f065ff439/
273
#if _WIN32_WCE <= 500
274
si.sdk_ver = ( (_WIN32_WCE / 100) << 24) |
275
( ((_WIN32_WCE % 100) / 10) << 16) |
276
( (_WIN32_WCE % 10) << 8);
278
si.sdk_ver = ( ((_WIN32_WCE & 0xFF00) >> 8) << 24) |
279
( ((_WIN32_WCE & 0x00F0) >> 4) << 16) |
280
( ((_WIN32_WCE & 0x000F) >> 0) << 8);
282
si.sdk_name = pj_str("cesdk");
283
#elif defined(_MSC_VER)
284
/* No SDK info is easily obtainable for Visual C, so lets just use
285
* _MSC_VER. The _MSC_VER macro reports the major and minor versions
286
* of the compiler. For example, 1310 for Microsoft Visual C++ .NET 2003.
287
* 1310 represents version 13 and a 1.0 point release.
288
* The Visual C++ 2005 compiler version is 1400.
290
si.sdk_ver = ((_MSC_VER / 100) << 24) |
291
(((_MSC_VER % 100) / 10) << 16) |
292
((_MSC_VER % 10) << 8);
293
si.sdk_name = pj_str("msvc");
294
#elif defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
295
pj_symbianos_get_sdk_info(&si.sdk_name, &si.sdk_ver);
299
* Build the info string.
302
char tmp[PJ_SYS_INFO_BUFFER_SIZE];
303
char os_ver[20], sdk_ver[20];
306
cnt = pj_ansi_snprintf(tmp, sizeof(tmp),
309
ver_info(si.os_ver, os_ver),
310
(si.machine.slen ? "/" : ""),
312
(si.sdk_name.slen ? "/" : ""),
314
ver_info(si.sdk_ver, sdk_ver));
315
if (cnt > 0 && cnt < (int)sizeof(tmp)) {
316
ALLOC_CP_STR(tmp, info);
320
si_initialized = PJ_TRUE;