ID: 3528310 - add str2char(), move is_fru_id() into 'lib/helper.c'

Commit adds new function str2char() into 'lib/helper.c'. Also, function
is_fru_id() moves from 'lib/ipmi_fru.c' into 'lib/helper.c' in order to be
utilized by other modules.
This commit is contained in:
Zdenek Styblik 2013-05-18 06:08:13 +00:00
parent 72dd3edde4
commit 7472986e1c
3 changed files with 54 additions and 25 deletions

View File

@ -72,8 +72,11 @@ int str2int(const char * str, int32_t * int_ptr);
int str2uint(const char * str, uint32_t * uint_ptr); int str2uint(const char * str, uint32_t * uint_ptr);
int str2short(const char * str, int16_t * shrt_ptr); int str2short(const char * str, int16_t * shrt_ptr);
int str2ushort(const char * str, uint16_t * ushrt_ptr); int str2ushort(const char * str, uint16_t * ushrt_ptr);
int str2char(const char * str, int8_t * chr_ptr);
int str2uchar(const char * str, uint8_t * uchr_ptr); int str2uchar(const char * str, uint8_t * uchr_ptr);
int is_fru_id(const char *argv_ptr, uint8_t *fru_id_ptr);
uint16_t str2val(const char * str, const struct valstr * vs); uint16_t str2val(const char * str, const struct valstr * vs);
void print_valstr(const struct valstr * vs, const char * title, int loglevel); void print_valstr(const struct valstr * vs, const char * title, int loglevel);
void print_valstr_2col(const struct valstr * vs, const char * title, int loglevel); void print_valstr_2col(const struct valstr * vs, const char * title, int loglevel);

View File

@ -334,6 +334,31 @@ int str2ushort(const char * str, uint16_t * ushrt_ptr)
return 0; return 0;
} /* str2ushort(...) */ } /* str2ushort(...) */
/* str2char - safely convert string to int8
*
* @str: source string to convert from
* @chr_ptr: pointer where to store result
*
* returns zero on success
* returns (-1) if one of args is NULL, (-2) or (-3) if conversion fails
*/
int str2char(const char *str, int8_t * chr_ptr)
{
int rc = (-3);
int64_t arg_long = 0;
if (!str || !chr_ptr) {
return (-1);
}
if ((rc = str2long(str, &arg_long)) != 0) {
*chr_ptr = 0;
return rc;
}
if (arg_long < INT8_MIN || arg_long > INT8_MAX) {
return (-3);
}
return 0;
} /* str2char(...) */
/* str2uchar - safely convert string to uint8 /* str2uchar - safely convert string to uint8
* *
* @str: source string to convert from * @str: source string to convert from
@ -642,3 +667,29 @@ ipmi_start_daemon(struct ipmi_intf *intf)
dup(0); dup(0);
dup(0); dup(0);
} }
/* is_fru_id - wrapper for str-2-int FRU ID conversion. Message is printed
* on error.
* FRU ID range: <0..255>
*
* @argv_ptr: source string to convert from; usually argv
* @fru_id_ptr: pointer where to store result
*
* returns zero on success
* returns (-1) on error and message is printed on STDERR
*/
int
is_fru_id(const char *argv_ptr, uint8_t *fru_id_ptr)
{
if (!argv_ptr || !fru_id_ptr) {
lprintf(LOG_ERR, "is_fru_id(): invalid argument(s).");
return (-1);
}
if (str2uchar(argv_ptr, fru_id_ptr) == 0) {
return 0;
}
lprintf(LOG_ERR, "FRU ID '%s' is either invalid or out of range.",
argv_ptr);
return (-1);
} /* is_fru_id(...) */

View File

@ -191,31 +191,6 @@ char * get_fru_area_str(uint8_t * data, uint32_t * offset)
return str; return str;
} }
/* is_fru_id - wrapper for str-2-int FRU ID conversion. Message is printed
* on error.
*
* @argv_ptr: source string to convert from; usually argv
* @fru_id_ptr: pointer where to store result
*
* returns zero on success
* returns (-1) on error and message is printed on STDERR
*/
int
is_fru_id(const char *argv_ptr, uint8_t *fru_id_ptr)
{
if (!argv_ptr || !fru_id_ptr) {
lprintf(LOG_ERR, "is_fru_id(): invalid argument(s).");
return (-1);
}
if (str2uchar(argv_ptr, fru_id_ptr) == 0) {
return 0;
}
lprintf(LOG_ERR, "FRU ID '%s' is either invalid or out of range.",
argv_ptr);
return (-1);
} /* is_fru_id(...) */
/* is_valid_filename - checks file/path supplied by user /* is_valid_filename - checks file/path supplied by user
* *
* input_filename - user input string * input_filename - user input string