FreeCalypso > hg > fc-sim-tools
comparison libutil/decimal_str.c @ 8:34bbb0585cab
libutil: import from previous fc-pcsc-tools version
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 14 Mar 2021 05:42:37 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 7:b25d4dfe5798 | 8:34bbb0585cab |
|---|---|
| 1 /* | |
| 2 * This module implements some functions for initial parsing of decimal | |
| 3 * string arguments, intended for implementation of commands like | |
| 4 * write-iccid and write-imsi. | |
| 5 */ | |
| 6 | |
| 7 #include <sys/types.h> | |
| 8 #include <ctype.h> | |
| 9 #include <stdio.h> | |
| 10 | |
| 11 parse_decimal_string_arg(arg, dest, maxdigits) | |
| 12 char *arg; | |
| 13 u_char *dest; | |
| 14 unsigned maxdigits; | |
| 15 { | |
| 16 unsigned n, ndig; | |
| 17 | |
| 18 if (!*arg) { | |
| 19 fprintf(stderr, | |
| 20 "error: empty argument given for decimal string\n"); | |
| 21 return(-1); | |
| 22 } | |
| 23 for (n = 0; *arg; ) { | |
| 24 if (!isdigit(*arg)) { | |
| 25 fprintf(stderr, | |
| 26 "error: non-digit char in decimal string argument\n"); | |
| 27 return(-1); | |
| 28 } | |
| 29 if (n >= maxdigits) { | |
| 30 fprintf(stderr, | |
| 31 "error: decimal string exceeds limit of %u digits\n", | |
| 32 maxdigits); | |
| 33 return(-1); | |
| 34 } | |
| 35 dest[n++] = *arg++ - '0'; | |
| 36 } | |
| 37 ndig = n; | |
| 38 while (n < maxdigits) | |
| 39 dest[n++] = 0xF; | |
| 40 return ndig; | |
| 41 } |
