Back to index

extremetuxracer  0.5beta
os_util.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "etracer.h"
00023 
00024 #if defined( COMPILER_IS_UNIX_COMPATIBLE ) && !defined( __MINGW32__ ) 
00025 #   include <sys/utsname.h>
00026 #endif
00027 
00028 #ifdef _MSC_VER
00029 #  define snprintf _snprintf
00030 #endif
00031 
00032 
00033 /* This function only used in Win32; ifdef'd to eliminate "unused" warnings */
00034 #if defined( WIN32 )
00035 
00036 static bool append_to_buff( char **buff, int *size, char *string )
00037 {
00038     int len;
00039 
00040     if ( *size < 0 ) {
00041        return false;
00042     }
00043 
00044     len = snprintf( *buff, *size, "%s", string );
00045 
00046     check_assertion( len >= 0, "buff too small" );
00047     if ( len < 0 ) {
00048        return false;
00049     }
00050     
00051     *buff += len;
00052     *size -= len;
00053 
00054        return true;
00055 }
00056 
00057 #endif /* WIN32 */
00058 
00059 
00060 /*---------------------------------------------------------------------------*/
00069 int get_os_version( char *buff, unsigned int size )
00070 {
00071 
00072 #ifdef WIN32
00073     /* Win32 Version */
00074 
00075     /* See http://www.mvps.org/vb/index2.html?tips/getversionex.htm for 
00076        a table mapping OSVERSIONINFOEX entries to Windows version */
00077 
00078     char tmp_buff[BUFF_LEN];
00079     int tmp_buff_size = BUFF_LEN;
00080     char *ptr = tmp_buff;
00081     int len;
00082     
00083     OSVERSIONINFO osvi;
00084     
00085     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00086     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00087     
00088     if ( !GetVersionEx( (OSVERSIONINFO *) &osvi) ) {
00089        return -1;
00090     }
00091     
00092     switch (osvi.dwPlatformId)
00093     {
00094     case VER_PLATFORM_WIN32_NT:
00095        
00096        /* Check for NT versus 2000 */
00097        if ( osvi.dwMajorVersion <= 4 ) {
00098            if ( !append_to_buff( &ptr, &tmp_buff_size, 
00099                               "Microsoft Windows NT" ) )
00100            {
00101               return -1;
00102            }
00103        }
00104        
00105        if ( osvi.dwMajorVersion == 5 ) {
00106            if ( !append_to_buff( &ptr, &tmp_buff_size, 
00107                               "Microsoft Windows 2000" ) )
00108            {
00109               return -1;
00110            }
00111        }
00112        
00113 
00114        /* Display version, service pack (if any), and build number. */
00115        len = snprintf(ptr, tmp_buff_size, " version %d.%d %s (Build %d)",
00116                      osvi.dwMajorVersion,
00117                      osvi.dwMinorVersion,
00118                      osvi.szCSDVersion,
00119                      osvi.dwBuildNumber & 0xFFFF);
00120 
00121        check_assertion( len >= 0, "tmp_buff too small" );
00122        if ( len < 0 ) {
00123            return -1;
00124        }
00125 
00126        ptr += len;
00127        tmp_buff_size -= len;
00128        
00129        break;
00130        
00131     case VER_PLATFORM_WIN32_WINDOWS:
00132        
00133        if ((osvi.dwMajorVersion > 4) || 
00134             ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
00135        {
00136            if ( osvi.dwMinorVersion <= 10 ) {
00137               if ( strcmp( osvi.szCSDVersion, "A" ) == 0 ) {
00138                   if ( !append_to_buff( &ptr, &tmp_buff_size, 
00139                                      "Microsoft Windows 98 SE") )
00140                   {
00141                      return -1;
00142                   }
00143               } else {
00144                   if ( !append_to_buff( &ptr, &tmp_buff_size, 
00145                                      "Microsoft Windows 98") )
00146                   {
00147                      return -1;
00148                   }
00149               }
00150            } else {
00151               if ( !append_to_buff( &ptr, &tmp_buff_size, 
00152                                   "Microsoft Windows ME") )
00153               {
00154                   return -1;
00155               }
00156            }
00157        } else {
00158            if ( strcmp( osvi.szCSDVersion, "B" ) == 0 ) {
00159               if ( !append_to_buff( &ptr, &tmp_buff_size, 
00160                                   "Microsoft Windows 95 OSR2") )
00161               {
00162                   return -1;
00163               }
00164            } else {
00165               if ( !append_to_buff( &ptr, &tmp_buff_size, 
00166                                   "Microsoft Windows 95") )
00167               {
00168                   return -1;
00169               }
00170            }
00171        }
00172 
00173        /* Append Build */
00174        len = snprintf(ptr, tmp_buff_size, " (Build %d)",
00175                      osvi.dwBuildNumber & 0xFFFF);
00176 
00177        check_assertion( len >= 0, "tmp_buff too small" );
00178        if ( len < 0 ) {
00179            return -1;
00180        }
00181 
00182        ptr += len;
00183        tmp_buff_size -= len;
00184        
00185        break;
00186        
00187     case VER_PLATFORM_WIN32s:
00188        if ( !append_to_buff( &ptr, &tmp_buff_size, "Microsoft Win32s") ) {
00189            return -1;
00190        }
00191        
00192        break;
00193     }
00194 
00195     len = snprintf( buff, size, "%s", tmp_buff );
00196     if ( len < 0 ) {
00197        /* buffer too small */
00198        buff[size-1] = (char)0;
00199        return 1;
00200     }
00201 
00202     return 0;
00203     
00204 #else
00205     /* Unix/Linux version */
00206 
00207     struct utsname utsname;
00208 
00209     if ( uname( &utsname ) >= 0 ) {
00210        if ( strlen( utsname.sysname ) + strlen( utsname.release ) +
00211             strlen( utsname.version ) + 3 > size ) 
00212        {
00213            if ( size > 0 ) {
00214               buff[0] = (char)0;
00215            }
00216            return 1;
00217        }
00218 
00219        sprintf( buff, "%s %s %s", 
00220                utsname.sysname, utsname.release, utsname.version );
00221        
00222        return 0;
00223        
00224     } else {
00225        /* uname failed */
00226        return -1;
00227     }
00228 #endif /* WIN32 */
00229 }
00230 
00231 
00232 /* EOF */