comparison libutil/plmnlist.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 a function for reading PLMN lists from files.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11
12 extern FILE *open_script_input_file();
13
14 read_plmn_list_from_file(filename, buf, ef_len)
15 char *filename;
16 u_char *buf;
17 unsigned ef_len;
18 {
19 FILE *inf;
20 int lineno, rc;
21 char linebuf[1024], *cp, *np;
22 u_char *dp, *endp;
23
24 inf = open_script_input_file(filename);
25 if (!inf) {
26 perror(filename);
27 return(-1);
28 }
29 dp = buf;
30 endp = buf + ef_len;
31 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
32 if (!index(linebuf, '\n')) {
33 fprintf(stderr,
34 "%s line %d: too long or missing newline\n",
35 filename, lineno);
36 fclose(inf);
37 return(-1);
38 }
39 for (cp = linebuf; ; ) {
40 while (isspace(*cp))
41 cp++;
42 if (*cp == '\0' || *cp == '#')
43 break;
44 for (np = cp; *cp && !isspace(*cp); cp++)
45 ;
46 if (*cp)
47 *cp++ = '\0';
48 if (dp >= endp) {
49 fprintf(stderr,
50 "%s line %d: number of PLMN codes exceeds EF size\n",
51 filename, lineno);
52 fclose(inf);
53 return(-1);
54 }
55 rc = encode_plmn_3bytes(np, dp);
56 if (rc < 0) {
57 fprintf(stderr, "%s line %d: invalid MCC-MNC\n",
58 filename, lineno);
59 fclose(inf);
60 return(-1);
61 }
62 dp += 3;
63 }
64 }
65 fclose(inf);
66 while (dp < endp)
67 *dp++ = 0xFF;
68 return(0);
69 }