mtd-utils: improve simple_strtoX usage commentary
PATCH 1/2 mtd-utils: improve simple_strtoX usage commentary
From: Artem Bityutskiy <[email protected]>
--- include/common.h | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-)
include/common.h +11 -0
@@ -93,6 +93,17 @@ static inline int is_power_of_2(unsigned long long n)* simple_strtoX - convert a hex/dec/oct string into a number * @snum: buffer to convert * @error: set to 1 when buffer isn't fully consumed +* +* These functions are similar to the standard strtoX() functions, but they are +* a little bit easier to use if you want to convert full string of digits into +* the binary form. The typical usage: +* +* int error = 0; +* unsigned long num; +* +* num = simple_strtoul(str, &error); +* if (error || !is_valid_number(num)) +* error_happened(); */ #define simple_strtoX (func, type) \ static inline type simple_##func(const char *snum, int *error) \
Signature · 1 line
1.7.2.3
PATCH 2/2 mtd-utils: switch ubi and ubifs tools to use common strtoX funcs
From: Artem Bityutskiy <[email protected]>
Instead of using strtol and Co directly, use our share simple_strtoX() helpers. This is just a cleanup.
--- mkfs.ubifs/mkfs.ubifs.c | 29 ++++++++++++++--------------- ubi-utils/src/mtdinfo.c | 7 +++---- ubi-utils/src/ubiattach.c | 15 +++++++-------- ubi-utils/src/ubidetach.c | 11 +++++------ ubi-utils/src/ubiformat.c | 19 +++++++++---------- ubi-utils/src/ubimkvol.c | 15 +++++++-------- ubi-utils/src/ubinfo.c | 11 +++++------ ubi-utils/src/ubinize.c | 19 +++++++++---------- ubi-utils/src/ubirmvol.c | 7 +++---- ubi-utils/src/ubirsvol.c | 11 +++++------ ubi-utils/src/ubiupdatevol.c | 7 +++---- 11 files changed, 70 insertions(+), 81 deletions(-)
mkfs.ubifs/mkfs.ubifs.c +14 -15
@@ -520,10 +520,9 @@ static int open_ubi(const char *node)static int get_options (int argc,char **argv) { -int opt, i; +int i;const char *tbl_file =NULL ;struct stat st; -char *endp; c->fanout =8 ; c->orph_lebs =1 ;
@@ -539,6 +538,8 @@ static int get_options(int argc, char**argv) c->log_lebs =-1 ;while (1 ) { +int opt, error =0 ; + opt =getopt_long (argc, argv, optstring, longopts, &i);if (opt ==-1 )break ;
@@ -599,27 +600,25 @@ static int get_options(int argc, char**argv)printf ("Version " PROGRAM_VERSION "\n" );exit (0 );case 'g' : - debug_level =strtol (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || - debug_level <0 || debug_level >3 ) + debug_level =simple_strtol (optarg, &error); +if (error || debug_level <0 || debug_level >3 )return err_msg ("bad debugging level '%s'" , optarg);break ;case 'f' : - c->fanout =strtol (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || c->fanout <=0 ) + c->fanout =simple_strtol (optarg, &error); +if (error || c->fanout <=0 )return err_msg ("bad fanout %s" , optarg);break ;case 'l' : - c->log_lebs =strtol (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || c->log_lebs <=0 ) + c->log_lebs =simple_strtol (optarg, &error); +if (error || c->log_lebs <=0 )return err_msg ("bad count of log LEBs '%s'" , optarg);break ;case 'p' : - c->orph_lebs =strtol (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || - c->orph_lebs <=0 ) + c->orph_lebs =simple_strtol (optarg, &error); +if (error || c->orph_lebs <=0 )return err_msg ("bad orphan LEB count '%s'" , optarg);break ;
@@ -644,9 +643,9 @@ static int get_options(int argc, char**argv)return err_msg ("bad compressor name" );break ;case 'X' : - c->favor_percent =strtol (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || - c->favor_percent <=0 || c->favor_percent >=100 ) + c->favor_percent =simple_strtol (optarg, &error); +if (error || c->favor_percent <=0 || + c->favor_percent >=100 )return err_msg ("bad favor LZO percent '%s'" , optarg);break ;
ubi-utils/src/mtdinfo.c +3 -4
@@ -87,8 +87,7 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"am:uhV" , long_options,NULL );if (key ==-1 )
@@ -104,8 +103,8 @@ static int parse_opt(int argc, char * const argv[])break ;case 'm' : - args.mtdn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.mtdn <0 ) + args.mtdn =simple_strtoul (optarg, &error); +if (error || args.mtdn <0 )return errmsg ("bad MTD device number: \"%s\"" , optarg);break ;
ubi-utils/src/ubiattach.c +7 -8
@@ -91,8 +91,7 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"p:m:d:O:hV" , long_options,NULL );if (key ==-1 )
@@ -103,22 +102,22 @@ static int parse_opt(int argc, char * const argv[]) args.dev = optarg;break ;case 'd' : - args.devn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.devn <0 ) + args.devn =simple_strtoul (optarg, &error); +if (error || args.devn <0 )return errmsg ("bad UBI device number: \"%s\"" , optarg);break ;case 'm' : - args.mtdn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.mtdn <0 ) + args.mtdn =simple_strtoul (optarg, &error); +if (error || args.mtdn <0 )return errmsg ("bad MTD device number: \"%s\"" , optarg);break ;case 'O' : - args.vidoffs =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vidoffs <=0 ) + args.vidoffs =simple_strtoul (optarg, &error); +if (error || args.vidoffs <=0 )return errmsg ("bad VID header offset: \"%s\"" , optarg);break ;
ubi-utils/src/ubidetach.c +5 -6
@@ -82,8 +82,7 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"p:m:d:hV" , long_options,NULL );if (key ==-1 )
@@ -94,15 +93,15 @@ static int parse_opt(int argc, char * const argv[]) args.dev = optarg;break ;case 'd' : - args.devn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.devn <0 ) + args.devn =simple_strtoul (optarg, &error); +if (error || args.devn <0 )return errmsg ("bad UBI device number: \"%s\"" , optarg);break ;case 'm' : - args.mtdn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.mtdn <0 ) + args.mtdn =simple_strtoul (optarg, &error); +if (error || args.mtdn <0 )return errmsg ("bad MTD device number: \"%s\"" , optarg);break ;
ubi-utils/src/ubiformat.c +9 -10
@@ -134,8 +134,7 @@ static int parse_opt(int argc, char * const argv[]) args.image_seq =rand ();while (1 ) { -int key; -char *endp; +int key, error =0 ;unsigned long int image_seq; key =getopt_long (argc, argv,"nh?Vyqve:x:s:O:f:S:" , long_options,NULL );
@@ -152,14 +151,14 @@ static int parse_opt(int argc, char * const argv[])break ;case 'O' : - args.vid_hdr_offs =strtoul (optarg, &endp,0 ); -if (args.vid_hdr_offs <=0 || *endp !='\0' || endp == optarg) + args.vid_hdr_offs =simple_strtoul (optarg, &error); +if (error || args.vid_hdr_offs <=0 )return errmsg ("bad VID header offset: \"%s\"" , optarg);break ;case 'e' : - args.ec =strtoull (optarg, &endp,0 ); -if (args.ec <0 || *endp !='\0' || endp == optarg) + args.ec =simple_strtoull (optarg, &error); +if (error || args.ec <0 )return errmsg ("bad erase counter value: \"%s\"" , optarg);if (args.ec >=EC_MAX )return errmsg ("too high erase %llu, counter, max is %u" , args.ec,EC_MAX );
@@ -189,14 +188,14 @@ static int parse_opt(int argc, char * const argv[])break ;case 'x' : - args.ubi_ver =strtoul (optarg, &endp,0 ); -if (args.ubi_ver <0 || *endp !='\0' || endp == optarg) + args.ubi_ver =simple_strtoul (optarg, &error); +if (error || args.ubi_ver <0 )return errmsg ("bad UBI version: \"%s\"" , optarg);break ;case 'Q' : - image_seq =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || image_seq >0xFFFFFFFF ) + image_seq =simple_strtoul (optarg, &error); +if (error || image_seq >0xFFFFFFFF )return errmsg ("bad UBI image sequence number: \"%s\"" , optarg); args.image_seq = image_seq;break ;
ubi-utils/src/ubimkvol.c +7 -8
@@ -121,8 +121,7 @@ static int param_sanity_check(void)static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =1 ; key =getopt_long (argc, argv,"a:n:N:s:S:t:h?Vm" , long_options,NULL );if (key ==-1 )
@@ -145,20 +144,20 @@ static int parse_opt(int argc, char * const argv[])break ;case 'S' : - args.lebs =strtoull (optarg, &endp,0 ); -if (endp == optarg || args.lebs <=0 || *endp !='\0' ) + args.lebs =simple_strtoull (optarg, &error); +if (error || args.lebs <=0 )return errmsg ("bad LEB count: \"%s\"" , optarg);break ;case 'a' : - args.alignment =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.alignment <=0 ) + args.alignment =simple_strtoul (optarg, &error); +if (error || args.alignment <=0 )return errmsg ("bad volume alignment: \"%s\"" , optarg);break ;case 'n' : - args.vol_id =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vol_id <0 ) + args.vol_id =simple_strtoul (optarg, &error); +if (error || args.vol_id <0 )return errmsg ("bad volume ID: " "\"%s\"" , optarg);break ;
ubi-utils/src/ubinfo.c +5 -6
@@ -89,8 +89,7 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"an:N:d:hV" , long_options,NULL );if (key ==-1 )
@@ -102,8 +101,8 @@ static int parse_opt(int argc, char * const argv[])break ;case 'n' : - args.vol_id =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vol_id <0 ) + args.vol_id =simple_strtoul (optarg, &error); +if (error || args.vol_id <0 )return errmsg ("bad volume ID: " "\"%s\"" , optarg);break ;
@@ -112,8 +111,8 @@ static int parse_opt(int argc, char * const argv[])break ;case 'd' : - args.devn =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.devn <0 ) + args.devn =simple_strtoul (optarg, &error); +if (error || args.devn <0 )return errmsg ("bad UBI device number: \"%s\"" , optarg);break ;
ubi-utils/src/ubinize.c +9 -10
@@ -159,8 +159,7 @@ static int parse_opt(int argc, char * const argv[]) args.image_seq =rand ();while (1 ) { -int key; -char *endp; +int key, error =0 ;unsigned long int image_seq; key =getopt_long (argc, argv,"o:p:m:s:O:e:x:Q:vhV" , long_options,NULL );
@@ -199,26 +198,26 @@ static int parse_opt(int argc, char * const argv[])break ;case 'O' : - args.vid_hdr_offs =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vid_hdr_offs <0 ) + args.vid_hdr_offs =simple_strtoul (optarg, &error); +if (error || args.vid_hdr_offs <0 )return errmsg ("bad VID header offset: \"%s\"" , optarg);break ;case 'e' : - args.ec =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.ec <0 ) + args.ec =simple_strtoul (optarg, &error); +if (error || args.ec <0 )return errmsg ("bad erase counter value: \"%s\"" , optarg);break ;case 'x' : - args.ubi_ver =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.ubi_ver <0 ) + args.ubi_ver =simple_strtoul (optarg, &error); +if (error || args.ubi_ver <0 )return errmsg ("bad UBI version: \"%s\"" , optarg);break ;case 'Q' : - image_seq =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || image_seq >0xFFFFFFFF ) + image_seq =simple_strtoul (optarg, &error); +if (error || image_seq >0xFFFFFFFF )return errmsg ("bad UBI image sequence number: \"%s\"" , optarg); args.image_seq = image_seq;break ;
ubi-utils/src/ubirmvol.c +3 -4
@@ -89,8 +89,7 @@ static int param_sanity_check(void)static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"n:N:h?V" , long_options,NULL );if (key ==-1 )
@@ -99,8 +98,8 @@ static int parse_opt(int argc, char * const argv[])switch (key) {case 'n' : - args.vol_id =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vol_id <0 ) { + args.vol_id =simple_strtoul (optarg, &error); +if (error || args.vol_id <0 ) {errmsg ("bad volume ID: " "\"%s\"" , optarg);return -1 ; }
ubi-utils/src/ubirsvol.c +5 -6
@@ -107,8 +107,7 @@ static int param_sanity_check(void)static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"s:S:n:N:h?V" , long_options,NULL );if (key ==-1 )
@@ -122,14 +121,14 @@ static int parse_opt(int argc, char * const argv[])break ;case 'S' : - args.lebs =strtoull (optarg, &endp,0 ); -if (endp == optarg || args.lebs <=0 || *endp !='\0' ) + args.lebs =simple_strtoull (optarg, &error); +if (error || args.lebs <=0 )return errmsg ("bad LEB count: \"%s\"" , optarg);break ;case 'n' : - args.vol_id =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.vol_id <0 ) { + args.vol_id =simple_strtoul (optarg, &error); +if (error || args.vol_id <0 ) {errmsg ("bad volume ID: " "\"%s\"" , optarg);return -1 ; }
ubi-utils/src/ubiupdatevol.c +3 -4
@@ -78,8 +78,7 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key; -char *endp; +int key, error =0 ; key =getopt_long (argc, argv,"ts:h?V" , long_options,NULL );if (key ==-1 )
@@ -91,8 +90,8 @@ static int parse_opt(int argc, char * const argv[])break ;case 's' : - args.size =strtoul (optarg, &endp,0 ); -if (*endp !='\0' || endp == optarg || args.size <0 ) + args.size =simple_strtoul (optarg, &error); +if (error || args.size <0 )return errmsg ("bad size: " "\"%s\"" , optarg);break ;
Signature · 1 line
1.7.2.3
Quoted 3 lines · Artem Bityutskiy
+ * num =
simple_strtoul(str, &error);
+ * if (error || !is_valid_number(num))
+ *error_happened();
when i read this, it makes me think there is a "is_valid_number()" function i should be calling.
maybe something like:
if (error || ... number is out of range for your need ...) -mike
Quoted 11 lines · Mike Frysinger
Quoted 3 lines · Artem Bityutskiy
+ * num =
simple_strtoul(str, &error);
+ * if (error || !is_valid_number(num))
+ *error_happened();when i read this, it makes me think there is a "
is_valid_number()" function i should be calling.maybe something like:
if (error || ... number is out of range for your need ...) -mike
I'll do this and push this out, thanks for review!
Signature · 2 lines
Best Regards, Artem Bityutskiy (Артём Битюцкий)