Successfully read kernel versions with only 2 components

When the kernel version as stated in /proc/version did not have at least
three numbers separated by periods, the version would fail to be read.
Sample /proc/version to demonstrate problem:

    Linux version 3.10-3-686-pae ...

The Linux kernel will always have a major number and a minor number
separated by a period.  There is likely also a patch version number too
that would be preceded by a period.  This enhancement will read 2 and 3
component Linux kernel versions.
This commit is contained in:
Mike Fleetwood 2013-12-03 21:44:26 +00:00 committed by Curtis Gedak
parent 32816f28f8
commit a4b82a9305
1 changed files with 8 additions and 8 deletions

View File

@ -724,11 +724,11 @@ Byte_Value Utils::ceil_size( Byte_Value value, Byte_Value rounding_size )
bool Utils::get_kernel_version( int & major_ver, int & minor_ver, int & patch_ver ) bool Utils::get_kernel_version( int & major_ver, int & minor_ver, int & patch_ver )
{ {
static bool read_file = false ; static bool read_file = false ;
static int read_major_ver = -1 ; static int read_major_ver = 0 ;
static int read_minor_ver = -1 ; static int read_minor_ver = 0 ;
static int read_patch_ver = -1 ; static int read_patch_ver = 0 ;
static bool success = false ;
bool success = false ;
if ( ! read_file ) if ( ! read_file )
{ {
std::ifstream input( "/proc/version" ) ; std::ifstream input( "/proc/version" ) ;
@ -736,18 +736,18 @@ bool Utils::get_kernel_version( int & major_ver, int & minor_ver, int & patch_ve
if ( input ) if ( input )
{ {
getline( input, line ) ; getline( input, line ) ;
sscanf( line .c_str(), "Linux version %d.%d.%d", int assigned = sscanf( line .c_str(), "Linux version %d.%d.%d",
&read_major_ver, &read_minor_ver, &read_patch_ver ) ; &read_major_ver, &read_minor_ver, &read_patch_ver ) ;
success = ( assigned >= 2 ) ; //At least 2 kernel version components read
input .close() ; input .close() ;
} }
read_file = true ; read_file = true ;
} }
if ( read_major_ver > -1 && read_minor_ver > -1 && read_patch_ver > -1 ) if ( success )
{ {
major_ver = read_major_ver ; major_ver = read_major_ver ;
minor_ver = read_minor_ver ; minor_ver = read_minor_ver ;
patch_ver = read_patch_ver ; patch_ver = read_patch_ver ;
success = true ;
} }
return success ; return success ;
} }