Update hhvm version.h
[hiphop-php.git] / hphp / util / kernel-version.cpp
blob3eaaad066a4b302a11ecc7a992ac23f674824568
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/util/kernel-version.h"
17 #include "hphp/util/assertions.h"
18 #include "hphp/util/portability.h"
20 #include <stdio.h>
21 #include <iostream>
23 #ifdef _MSC_VER
24 #include <Windows.h>
25 #else
26 #include <unistd.h>
27 #include <sys/utsname.h>
28 #endif
30 namespace HPHP {
32 void KernelVersion::parse(const char* s) {
33 char dashdot[2];
34 // Assume no part of the uname string will be more than 128 chars
35 char release[128];
36 release[0] = 0;
37 char build[128];
38 build[0] = 0;
39 sscanf(s,
40 "%d.%d%1[.-]%127[A-Za-z0-9]-%127[A-Za-z0-9]_fbk%d_",
41 &m_major, &m_minor, dashdot, release, build, &m_fbk);
42 m_release_str = release[0] == 0 ? "" : (const char*) release;
43 m_build_str = build[0] == 0 ? "" : (const char*) build;
44 // Populate the int-based release and build, if we have all digits in them
45 if (isNumber(m_release_str)) {
46 m_release = atoi(m_release_str.c_str());
48 if (isNumber(m_build_str)) {
49 m_build = atoi(m_build_str.c_str());
53 KernelVersion::KernelVersion() {
54 #ifdef _MSC_VER
55 OSVERSIONINFO verInf;
56 verInf.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
57 GetVersionEx(&verInf);
58 m_major = verInf.dwMajorVersion;
59 m_minor = verInf.dwMinorVersion;
60 m_build = verInf.dwBuildNumber;
61 m_release = verInf.dwPlatformId;
62 #else
63 struct utsname uts;
64 DEBUG_ONLY int err = uname(&uts);
65 assert(err == 0);
66 parse(uts.release);
67 #endif
70 KernelVersion::KernelVersion(const char* s) {
71 m_major = m_minor = m_fbk = m_release = m_build = -1;
72 m_release_str = m_build_str = "";
73 parse(s);
76 bool KernelVersion::isNumber(const std::string s) {
77 return !s.empty() && s.find_first_not_of("0123456789") == std::string::npos;