FreeCalypso > hg > sms-coding-utils
annotate enc-text/concat_refno.c @ 18:590b0b5bcbbb
sms-gen-tpdu: support setting VP for MO
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 27 Aug 2023 07:55:13 +0000 |
parents | a16b1b9728f6 |
children |
rev | line source |
---|---|
2
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the messy code for automatic generation and |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * increment of concat SMS reference numbers. |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/param.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/file.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <sys/time.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <ctype.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdio.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <stdlib.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <unistd.h> |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 static int |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 initial_seed() |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 struct timeval tv; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 u_char refno, *cp, *endp; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 gettimeofday(&tv, 0); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 cp = (u_char *) &tv; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 endp = cp + sizeof(struct timeval); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 refno = 0; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 while (cp < endp) |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 refno ^= *cp++; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return refno; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 get_concsms_refno_from_host_fs() |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 { |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 char *homedir, statefile[MAXPATHLEN]; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 int fd, cc; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 char buf[6]; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 u_char refno; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 homedir = getenv("HOME"); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 if (!homedir) { |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 fprintf(stderr, |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 "error: no HOME= defined, needed for concat SMS refno\n"); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 exit(1); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 } |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 sprintf(statefile, "%s/.concat_sms_refno", homedir); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 fd = open(statefile, O_RDWR|O_CREAT, 0666); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (fd < 0) { |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 perror(statefile); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 exit(1); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cc = read(fd, buf, 5); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (cc == 5 && buf[0] == '0' && buf[1] == 'x' && isxdigit(buf[2]) && |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 isxdigit(buf[3]) && buf[4] == '\n') |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 refno = strtoul(buf, 0, 16) + 1; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 else |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 refno = initial_seed(); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 sprintf(buf, "0x%02X\n", refno); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 lseek(fd, 0, SEEK_SET); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 write(fd, buf, 5); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 close(fd); |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 return refno; |
a16b1b9728f6
enc-text: sms-encode-text program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |