FreeCalypso > hg > freecalypso-tools
comparison uptools/atcmd/smsend_concat.c @ 370:076d533f840d
fcup-smsend: implemented automatic concat SMS refno generation
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 08 Mar 2018 17:47:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
369:06cf82710cca | 370:076d533f840d |
---|---|
1 /* | |
2 * This module contains the messy code for automatic generation and | |
3 * increment of concat SMS reference numbers. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <sys/param.h> | |
8 #include <sys/file.h> | |
9 #include <sys/time.h> | |
10 #include <ctype.h> | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <unistd.h> | |
14 #include "../../rvinterf/include/exitcodes.h" | |
15 | |
16 static int | |
17 initial_seed() | |
18 { | |
19 struct timeval tv; | |
20 u_char refno, *cp, *endp; | |
21 | |
22 gettimeofday(&tv, 0); | |
23 cp = (u_char *) &tv; | |
24 endp = cp + sizeof(struct timeval); | |
25 refno = 0; | |
26 while (cp < endp) | |
27 refno ^= *cp++; | |
28 return refno; | |
29 } | |
30 | |
31 get_concsms_refno_from_host_fs() | |
32 { | |
33 char *homedir, statefile[MAXPATHLEN]; | |
34 int fd, cc; | |
35 char buf[6]; | |
36 u_char refno; | |
37 | |
38 homedir = getenv("HOME"); | |
39 if (!homedir) { | |
40 fprintf(stderr, | |
41 "error: no HOME= defined, needed for concat SMS refno\n"); | |
42 exit(ERROR_UNIX); | |
43 } | |
44 sprintf(statefile, "%s/.concat_sms_refno", homedir); | |
45 fd = open(statefile, O_RDWR|O_CREAT, 0666); | |
46 if (fd < 0) { | |
47 perror(statefile); | |
48 exit(ERROR_UNIX); | |
49 } | |
50 cc = read(fd, buf, 5); | |
51 if (cc == 5 && buf[0] == '0' && buf[1] == 'x' && isxdigit(buf[2]) && | |
52 isxdigit(buf[3]) && buf[4] == '\n') | |
53 refno = strtoul(buf, 0, 16) + 1; | |
54 else | |
55 refno = initial_seed(); | |
56 sprintf(buf, "0x%02X\n", refno); | |
57 lseek(fd, 0, SEEK_SET); | |
58 write(fd, buf, 5); | |
59 close(fd); | |
60 return refno; | |
61 } |