comparison libutil/imsi_entry.c @ 19:40e5097437fa

libutil: add grok_imsi_user_arg()
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 22:38:42 -0800
parents
children
comparison
equal deleted inserted replaced
18:87c077b23996 19:40e5097437fa
1 /*
2 * The library function implemented in this module supports IMSI entry
3 * at UI level, either in the standard form (long string of digits)
4 * or in the shorthand notation introduced in fc-sim-tools.
5 */
6
7 #include <ctype.h>
8 #include <string.h>
9 #include <strings.h>
10
11 grok_imsi_user_arg(arg, dest)
12 char *arg, *dest;
13 {
14 char *cp, *dp;
15 int n, tail_len, remain;
16
17 if (!isdigit(*arg))
18 return(-1);
19 cp = arg;
20 dp = dest;
21 n = 0;
22 while (isdigit(*cp)) {
23 if (n >= 15)
24 return(-1);
25 *dp++ = *cp++;
26 n++;
27 }
28 if (!*cp) {
29 if (n < 6)
30 return(-1);
31 *dp = '\0';
32 return(0);
33 }
34 if (*cp != '-')
35 return(-1);
36 cp++;
37 tail_len = strlen(cp);
38 if (!tail_len)
39 return(-1);
40 remain = 15 - n;
41 if (remain < tail_len + 1)
42 return(-1);
43 while (remain > tail_len) {
44 *dp++ = '0';
45 remain--;
46 }
47 while (*cp) {
48 if (!isdigit(*cp))
49 return(-1);
50 *dp++ = *cp++;
51 }
52 *dp = '\0';
53 return(0);
54 }