FreeCalypso > hg > fc-pcsc-tools
annotate simtool/pbrestore.c @ 87:bc862d41f96b
pb-restore command implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 17 Feb 2021 07:04:05 +0000 |
parents | simtool/pbupd_file.c@51167ee0151b |
children | ae831b21ef77 |
rev | line source |
---|---|
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
87
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
2 * This module implements pb-restore and pb-update commands. |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <string.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <strings.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include "curfile.h" |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
18
2ef261371347
alpha tag from file parsing functions factored out of pb-update
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
13 extern char *alpha_from_file_qstring(); |
2ef261371347
alpha tag from file parsing functions factored out of pb-update
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
14 extern char *alpha_from_file_hex(); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static |
87
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
17 process_record(line, bin_file_buf, filename_for_errs, lineno_for_errs) |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char *line, *filename_for_errs; |
87
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
19 u_char *bin_file_buf; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 unsigned recno; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 u_char record[255], *fixp; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 u_char digits[20]; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 unsigned ndigits, num_digit_bytes; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 char *cp; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 int c; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 recno = strtoul(line+1, 0, 10); |
85
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
29 if (recno < 1 || recno > curfile_record_count) { |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fprintf(stderr, "%s line %d: record number is out of range\n", |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 filename_for_errs, lineno_for_errs); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 cp = line + 1; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 while (isdigit(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 if (*cp++ != ':') { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n", |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 filename_for_errs, lineno_for_errs); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 cp++; |
85
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
44 memset(record, 0xFF, curfile_record_len); |
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
45 fixp = record + curfile_record_len - 14; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (digit_char_to_gsm(*cp) < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 goto inv_syntax; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 for (ndigits = 0; ; ndigits++) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 c = digit_char_to_gsm(*cp); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (c < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 break; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (ndigits >= 20) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 fprintf(stderr, "%s line %d: too many number digits\n", |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 filename_for_errs, lineno_for_errs); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 digits[ndigits] = c; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (ndigits & 1) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 digits[ndigits++] = 0xF; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 num_digit_bytes = ndigits >> 1; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 fixp[0] = num_digit_bytes + 1; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 pack_digit_bytes(digits, fixp + 2, num_digit_bytes); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 if (*cp++ != ',') |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 goto inv_syntax; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 if (cp[0] != '0' || cp[1] != 'x' && cp[1] != 'X' || !isxdigit(cp[2]) || |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 !isxdigit(cp[3]) || !isspace(cp[4])) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 goto inv_syntax; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 fixp[1] = strtoul(cp, 0, 16); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 cp += 5; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (!strncasecmp(cp, "CCP=", 4)) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 cp += 4; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 fixp[12] = strtoul(cp, 0, 0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 while (*cp && !isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 if (!strncasecmp(cp, "EXT=", 4)) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 cp += 4; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 fixp[13] = strtoul(cp, 0, 0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 while (*cp && !isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 if (*cp == '"') { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 cp++; |
85
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
92 cp = alpha_from_file_qstring(cp, record, |
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
93 curfile_record_len - 14, |
18
2ef261371347
alpha tag from file parsing functions factored out of pb-update
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
94 filename_for_errs, |
2ef261371347
alpha tag from file parsing functions factored out of pb-update
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
95 lineno_for_errs); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 if (!cp) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 } else if (!strncasecmp(cp, "HEX", 3)) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 cp += 3; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 cp++; |
85
51167ee0151b
pb-update implementation: rm vestiges of uicc unification attempt
Mychaela Falconia <falcon@freecalypso.org>
parents:
18
diff
changeset
|
102 cp = alpha_from_file_hex(cp, record, curfile_record_len - 14, |
18
2ef261371347
alpha tag from file parsing functions factored out of pb-update
Mychaela Falconia <falcon@freecalypso.org>
parents:
13
diff
changeset
|
103 filename_for_errs, lineno_for_errs); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 if (!cp) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 } else |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 goto inv_syntax; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 while (isspace(*cp)) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 cp++; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 if (*cp) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 goto inv_syntax; |
87
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
112 if (bin_file_buf) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
113 bcopy(record, bin_file_buf + (recno - 1) * curfile_record_len, |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
114 curfile_record_len); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
115 return(0); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
116 } else |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
117 return update_rec_op(recno, 0x04, record, curfile_record_len); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
118 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
119 |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
120 cmd_pb_restore(argc, argv) |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
121 char **argv; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
122 { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
123 int rc; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
124 FILE *inf; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
125 int lineno; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
126 char linebuf[1024]; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
127 u_char *databuf; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
128 |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
129 rc = phonebook_op_common(argv[1]); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
130 if (rc < 0) |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
131 return(rc); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
132 databuf = malloc(curfile_total_size); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
133 if (!databuf) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
134 perror("malloc for full phonebook EF"); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
135 return(-1); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
136 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
137 inf = fopen(argv[2], "r"); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
138 if (!inf) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
139 perror(argv[2]); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
140 free(databuf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
141 return(-1); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
142 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
143 memset(databuf, 0xFF, curfile_total_size); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
144 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
145 if (!index(linebuf, '\n')) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
146 fprintf(stderr, |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
147 "%s line %d: too long or missing newline\n", |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
148 argv[2], lineno); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
149 fclose(inf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
150 free(databuf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
151 return(-1); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
152 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
153 if (linebuf[0] != '#' || !isdigit(linebuf[1])) |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
154 continue; |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
155 rc = process_record(linebuf, databuf, argv[2], lineno); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
156 if (rc < 0) { |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
157 fclose(inf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
158 free(databuf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
159 return(rc); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
160 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
161 } |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
162 fclose(inf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
163 rc = restore_bin_records(databuf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
164 free(databuf); |
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
165 return(rc); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
166 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
167 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
168 cmd_pb_update(argc, argv) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
169 char **argv; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
170 { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
171 int rc; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
172 FILE *inf; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
173 int lineno; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
174 char linebuf[1024]; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
175 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
176 rc = phonebook_op_common(argv[1]); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
177 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
178 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
179 inf = fopen(argv[2], "r"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
180 if (!inf) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
181 perror(argv[2]); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
182 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
183 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
184 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
185 if (!index(linebuf, '\n')) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
186 fprintf(stderr, |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
187 "%s line %d: too long or missing newline\n", |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
188 argv[2], lineno); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
189 fclose(inf); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
190 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
191 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
192 if (linebuf[0] != '#' || !isdigit(linebuf[1])) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
193 continue; |
87
bc862d41f96b
pb-restore command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
85
diff
changeset
|
194 rc = process_record(linebuf, 0, argv[2], lineno); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
195 if (rc < 0) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
196 fclose(inf); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
197 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
198 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
199 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
200 fclose(inf); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
201 return(0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
202 } |