ID: 3608765 - 'lib/ipmi_ekanalyzer.c' - reverse checks to reduce indentation

Rewrote ipmi_ek_display_chassis_info_area,
ipmi_ek_display_product_info_area, and
ipmi_ekanalyzer_fru_file2structure to reverse the checks of error
conditions to reduce the amount of indentation required.

Commit for Dan Gora
This commit is contained in:
Zdenek Styblik 2013-04-15 11:33:17 +00:00
parent 184994586e
commit 4299cfa385

View File

@ -2544,51 +2544,56 @@ ipmi_ek_display_fru_header_detail( char * filename )
* *
***************************************************************************/ ***************************************************************************/
static void static void
ipmi_ek_display_chassis_info_area( FILE * input_file, long offset ) ipmi_ek_display_chassis_info_area(FILE * input_file, long offset)
{ {
if ( input_file != NULL ){ unsigned char data = 0;
unsigned char ch_type = 0;
unsigned int len;
size_t file_offset;
if (input_file == NULL) {
return;
}
printf("%s\n", EQUAL_LINE_LIMITER); printf("%s\n", EQUAL_LINE_LIMITER);
printf("Chassis Info Area\n"); printf("Chassis Info Area\n");
printf("%s\n", EQUAL_LINE_LIMITER); printf("%s\n", EQUAL_LINE_LIMITER);
fseek (input_file, offset, SEEK_SET); fseek(input_file, offset, SEEK_SET);
if ( !feof(input_file) ){ if (feof(input_file)) {
unsigned char data = 0; lprintf(LOG_ERR, "Invalid Chassis Info Area!");
unsigned int len = 0; return;
}
fread (&data, 1, 1, input_file); fread(&data, 1, 1, input_file);
printf("Format Version Number: %d\n", (data & 0x0f) ); printf("Format Version Number: %d\n", (data & 0x0f));
if ( !feof(input_file) ){ if (feof(input_file)) {
fread (&len, 1, 1, input_file); return;
}
fread(&len, 1, 1, input_file);
/* len is in factor of 8 bytes */ /* len is in factor of 8 bytes */
len = len * 8; len = len * 8;
printf("Area Length: %d\n", len); printf("Area Length: %d\n", len);
len -= 2; len -= 2;
if (feof(input_file)) {
return;
} }
if ( !feof(input_file) ){
unsigned char ch_type = 0;
size_t file_offset = ftell (input_file);
/* Chassis Type*/ /* Chassis Type*/
fread (&ch_type, 1, 1, input_file); fread(&ch_type, 1, 1, input_file);
printf("Chassis Type: %d\n", ch_type); printf("Chassis Type: %d\n", ch_type);
len --; len--;
/* Chassis Part Number*/ /* Chassis Part Number*/
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Chassis Part Number", &len); "Chassis Part Number", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Chassis Serial */ /* Chassis Serial */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Chassis Serial Number", &len); "Chassis Serial Number", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Custom product info area */ /* Custom product info area */
file_offset = ipmi_ek_display_board_info_area ( file_offset = ipmi_ek_display_board_info_area(input_file,
input_file, "Custom", &len); "Custom", &len);
}
}
}
else{
lprintf(LOG_ERR, "Invalid Chassis Info Area!");
}
} }
/************************************************************************** /**************************************************************************
@ -2734,70 +2739,75 @@ ipmi_ek_display_board_info_area( FILE * input_file, char * board_type,
* *
***************************************************************************/ ***************************************************************************/
static void static void
ipmi_ek_display_product_info_area( FILE * input_file, long offset ) ipmi_ek_display_product_info_area(FILE * input_file, long offset)
{ {
if ( input_file != NULL ){ unsigned char data = 0;
unsigned int len;
size_t file_offset = ftell(input_file);
if (input_file == NULL) {
return;
}
printf("%s\n", EQUAL_LINE_LIMITER); printf("%s\n", EQUAL_LINE_LIMITER);
printf("Product Info Area\n"); printf("Product Info Area\n");
printf("%s\n", EQUAL_LINE_LIMITER); printf("%s\n", EQUAL_LINE_LIMITER);
fseek (input_file, offset, SEEK_SET); fseek(input_file, offset, SEEK_SET);
if ( !feof(input_file) ){ if (feof(input_file)) {
unsigned char data = 0; lprintf(LOG_ERR, "Invalid Product Info Area!");
unsigned int len = 0; return;
}
fread (&data, 1, 1, input_file); fread(&data, 1, 1, input_file);
printf("Format Version Number: %d\n", (data & 0x0f) ); printf("Format Version Number: %d\n", (data & 0x0f));
if ( !feof(input_file) ){ if (feof(input_file)) {
fread (&len, 1, 1, input_file); return;
}
fread(&len, 1, 1, input_file);
/* length is in factor of 8 bytes */ /* length is in factor of 8 bytes */
len = len * 8; len = len * 8;
printf("Area Length: %d\n", len); printf("Area Length: %d\n", len);
len -= 2; /* -1 byte of format version and -1 byte itself */ len -= 2; /* -1 byte of format version and -1 byte itself */
if (feof(input_file)) {
return;
} }
if ( !feof(input_file) ){
size_t file_offset = ftell (input_file);
fread (&data, 1, 1, input_file); fread(&data, 1, 1, input_file);
printf("Language Code: %d\n", data); printf("Language Code: %d\n", data);
len --; len--;
/* Product Mfg */ /* Product Mfg */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Product Manufacture Data", &len); "Product Manufacture Data", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Product Name */ /* Product Name */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Product Name", &len); "Product Name", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Product Part */ /* Product Part */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Product Part/Model Number", &len); "Product Part/Model Number", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Product Version */ /* Product Version */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Product Version", &len); "Product Version", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Product Serial */ /* Product Serial */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Product Serial Number", &len); "Product Serial Number", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Product Asset Tag */ /* Product Asset Tag */
file_offset = ipmi_ek_display_board_info_area ( input_file, file_offset = ipmi_ek_display_board_info_area(input_file,
"Asset Tag", &len); "Asset Tag", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* FRU file ID */ /* FRU file ID */
file_offset = ipmi_ek_display_board_info_area ( file_offset = ipmi_ek_display_board_info_area(input_file,
input_file, "FRU File ID", &len); "FRU File ID", &len);
fseek (input_file, file_offset, SEEK_SET); fseek(input_file, file_offset, SEEK_SET);
/* Custom product info area */ /* Custom product info area */
file_offset = ipmi_ek_display_board_info_area ( file_offset = ipmi_ek_display_board_info_area(input_file,
input_file, "Custom", &len); "Custom", &len);
}
}
}
else{
lprintf(LOG_ERR, "Invalid Product Info Area!");
}
} }
/************************************************************************** /**************************************************************************
@ -3880,76 +3890,74 @@ ipmi_ek_display_clock_config_record( struct ipmi_ek_multi_header * record )
* *
***************************************************************************/ ***************************************************************************/
static int static int
ipmi_ekanalyzer_fru_file2structure( char * filename, ipmi_ekanalyzer_fru_file2structure(char * filename,
struct ipmi_ek_multi_header ** list_head, struct ipmi_ek_multi_header ** list_head,
struct ipmi_ek_multi_header ** list_record, struct ipmi_ek_multi_header ** list_record,
struct ipmi_ek_multi_header ** list_last ) struct ipmi_ek_multi_header ** list_last)
{ {
int return_status = ERROR_STATUS; int return_status = ERROR_STATUS;
FILE * input_file; FILE * input_file;
unsigned char last_record = 0;
input_file = fopen ( filename, "r");
if ( input_file == NULL ){
lprintf(LOG_ERR, "File: '%s' is not found", filename);
return_status = ERROR_STATUS;
}
else{
long multi_offset = 0; long multi_offset = 0;
fseek ( input_file, START_DATA_OFFSET, SEEK_SET );
fread ( &multi_offset, 1, 1, input_file );
if ( multi_offset <= 0 ){
lprintf(LOG_NOTICE, "There is no multi record in the file %s\n",
filename);
}
else{
int record_count = 0; int record_count = 0;
if ( verbose == LOG_DEBUG ){ input_file = fopen(filename, "r");
printf( "start multi offset = 0x%02lx\n", multi_offset ); if (input_file == NULL) {
lprintf(LOG_ERR, "File: '%s' is not found", filename);
return ERROR_STATUS;
} }
/*the offset value is in multiple of 8 bytes.*/
multi_offset = multi_offset * 8;
fseek ( input_file, multi_offset, SEEK_SET );
while ( !feof( input_file ) ){
*list_record = malloc ( sizeof (struct ipmi_ek_multi_header) );
fread ( &(*list_record)->header, START_DATA_OFFSET, 1, input_file);
if ( (*list_record)->header.len > 0 ){
(*list_record)->data =
malloc ((*list_record)->header.len);
if ( (*list_record)->data == NULL ){
lprintf(LOG_ERR, "Lack of memory");
}
else{
unsigned char last_record = 0;
fread ( (*list_record)->data, fseek(input_file, START_DATA_OFFSET, SEEK_SET);
((*list_record)->header.len), 1, input_file); fread(&multi_offset, 1, 1, input_file);
if ( verbose > 0 ) if (multi_offset < 1) {
lprintf(LOG_NOTICE, "There is no multi record in the file %s\n",
filename);
fclose(input_file);
return OK_STATUS;
}
/* the offset value is in multiple of 8 bytes. */
multi_offset = multi_offset * 8;
if (verbose == LOG_DEBUG) {
printf("start multi offset = 0x%02lx\n", multi_offset);
}
fseek(input_file, multi_offset, SEEK_SET);
while (!feof(input_file)) {
*list_record = malloc(sizeof(struct ipmi_ek_multi_header));
fread(&(*list_record)->header, START_DATA_OFFSET, 1, input_file);
if ((*list_record)->header.len == 0) {
record_count++;
continue;
}
(*list_record)->data = malloc((*list_record)->header.len);
if ((*list_record)->data == NULL) {
lprintf(LOG_ERR, "Failed to allocation memory size %d\n",
(*list_record)->header.len);
record_count++;
continue;
}
fread((*list_record)->data, ((*list_record)->header.len), 1,
input_file);
if (verbose > 0) {
printf("Record %d has length = %02x\n", record_count, printf("Record %d has length = %02x\n", record_count,
(*list_record)->header.len); (*list_record)->header.len);
if ( verbose > 1 ){ }
if (verbose > 1) {
int i; int i;
printf("%02x\t", (*list_record)->header.type); printf("%02x\t", (*list_record)->header.type);
for ( i = 0; i < ( (*list_record)->header.len ); i++ ){ for (i = 0; i < ((*list_record)->header.len); i++) {
printf("%02x\t", (*list_record)->data[i]); printf("%02x\t", (*list_record)->data[i]);
} }
printf("\n"); printf("\n");
} }
ipmi_ek_add_record2list ( list_record, list_head, list_last ); ipmi_ek_add_record2list(list_record, list_head, list_last);
/*mask the 8th bits to see if it is the last record*/ /* mask the 8th bits to see if it is the last record */
last_record = ((*list_record)->header.format) & 0x80; last_record = ((*list_record)->header.format) & 0x80;
if ( last_record ){ if (last_record) {
break; break;
} }
}
}
record_count++; record_count++;
} }
}
fclose( input_file );
return_status = OK_STATUS;
}
return return_status; return return_status;
} }