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

@ -2546,28 +2546,39 @@ 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)) {
return;
}
fread(&len, 1, 1, input_file); 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);
@ -2581,14 +2592,8 @@ ipmi_ek_display_chassis_info_area( FILE * input_file, long offset )
"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!");
}
} }
/************************************************************************** /**************************************************************************
@ -2736,27 +2741,38 @@ 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)) {
return;
}
fread(&len, 1, 1, input_file); 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);
@ -2786,18 +2802,12 @@ ipmi_ek_display_product_info_area( FILE * input_file, long offset )
"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!");
}
} }
/************************************************************************** /**************************************************************************
@ -3887,47 +3897,51 @@ ipmi_ekanalyzer_fru_file2structure( char * filename,
{ {
int return_status = ERROR_STATUS; int return_status = ERROR_STATUS;
FILE * input_file; FILE * input_file;
unsigned char last_record = 0;
long multi_offset = 0;
int record_count = 0;
input_file = fopen(filename, "r"); input_file = fopen(filename, "r");
if (input_file == NULL) { if (input_file == NULL) {
lprintf(LOG_ERR, "File: '%s' is not found", filename); lprintf(LOG_ERR, "File: '%s' is not found", filename);
return_status = ERROR_STATUS; return ERROR_STATUS;
} }
else{
long multi_offset = 0;
fseek(input_file, START_DATA_OFFSET, SEEK_SET); fseek(input_file, START_DATA_OFFSET, SEEK_SET);
fread(&multi_offset, 1, 1, input_file); fread(&multi_offset, 1, 1, input_file);
if ( multi_offset <= 0 ){ if (multi_offset < 1) {
lprintf(LOG_NOTICE, "There is no multi record in the file %s\n", lprintf(LOG_NOTICE, "There is no multi record in the file %s\n",
filename); filename);
} fclose(input_file);
else{ return OK_STATUS;
int record_count = 0;
if ( verbose == LOG_DEBUG ){
printf( "start multi offset = 0x%02lx\n", multi_offset );
} }
/* the offset value is in multiple of 8 bytes. */ /* the offset value is in multiple of 8 bytes. */
multi_offset = multi_offset * 8; 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); fseek(input_file, multi_offset, SEEK_SET);
while (!feof(input_file)) { while (!feof(input_file)) {
*list_record = malloc(sizeof(struct ipmi_ek_multi_header)); *list_record = malloc(sizeof(struct ipmi_ek_multi_header));
fread(&(*list_record)->header, START_DATA_OFFSET, 1, input_file); fread(&(*list_record)->header, START_DATA_OFFSET, 1, input_file);
if ( (*list_record)->header.len > 0 ){ if ((*list_record)->header.len == 0) {
(*list_record)->data = record_count++;
malloc ((*list_record)->header.len); continue;
if ( (*list_record)->data == NULL ){ }
lprintf(LOG_ERR, "Lack of memory"); (*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;
} }
else{
unsigned char last_record = 0;
fread ( (*list_record)->data, fread((*list_record)->data, ((*list_record)->header.len), 1,
((*list_record)->header.len), 1, input_file); input_file);
if ( verbose > 0 ) 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);
@ -3942,14 +3956,8 @@ ipmi_ekanalyzer_fru_file2structure( char * filename,
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;
} }