annotate miscutil/make-imeisv.c @ 786:b761029f9c03

simagent: spenh command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 13 Mar 2021 22:41:33 +0000
parents 2dcfad8a3ed0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
744
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This utility constructs a 16-digit IMEISV from a 15-digit IMEI
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (which must have a valid Luhn check digit) and a 2-digit SV field.
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * It is intended for use in shell scripts.
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ctype.h>
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 parse_imei_arg(input, buf)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 char *input, *buf;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 char *cp;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 int i;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 cp = input;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (!isdigit(*cp)) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 inv: fprintf(stderr,
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 "error: IMEI argument must have 15 decimal digits\n");
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 exit(1);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 for (i = 0; i < 15; i++) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (ispunct(*cp))
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 cp++;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (!isdigit(*cp))
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 goto inv;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 buf[i] = *cp++;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (*cp)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 goto inv;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 check_luhn(digits)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 char *digits;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 int i, dig, sum;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 sum = 0;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 for (i = 0; i < 14; i++) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 dig = digits[i] - '0';
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (i & 1) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 dig *= 2;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (dig > 9)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 dig -= 9;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 sum += dig;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 dig = sum % 10;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (dig)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 dig = 10 - dig;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (digits[14] != dig + '0') {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 fprintf(stderr, "error: given IMEI fails Luhn check\n");
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 exit(1);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 main(argc, argv)
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 char **argv;
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 char imeibuf[15];
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (argc != 3) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(stderr, "usage: %s IMEI SV\n", argv[0]);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 parse_imei_arg(argv[1], imeibuf);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 check_luhn(imeibuf);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (!isdigit(argv[2][0]) || !isdigit(argv[2][1]) || argv[2][2]) {
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 fprintf(stderr,
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 "error: SV argument must have 2 decimal digits\n");
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 exit(1);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 printf("%.8s-%.6s-%s\n", imeibuf, imeibuf + 8, argv[2]);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(0);
2dcfad8a3ed0 make-imeisv utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }