-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutil.c
78 lines (67 loc) · 1.98 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <signal.h>
#include <time.h>
__attribute__ ((format (printf, 1, 2)))
void
term_title(const char *fmt, ...)
{
if (isatty(1)) {
va_list va;
va_start(va, fmt);
fputs("\033]0;", stdout);
vfprintf(stdout, fmt, va);
fputc('\007', stdout);
fflush(stdout);
}
}
/****************************************************************************/
// This is a weird workaround for the non-realtime-awareness of sleep(3):
// http://stackoverflow.com/questions/32152276/real-time-aware-sleep-call
static void
nullhandler(int signal) {}
int
isleep(int seconds, int verbose)
{
if (verbose) {
fprintf(stderr, "Sleeping for %d seconds...", seconds);
fflush(stderr);
}
signal(SIGALRM, nullhandler);
int res=0, elapsed=0;
for (time_t t=time(NULL); (elapsed<seconds) && (res<=0); elapsed=time(NULL)-t)
res = sleep((seconds-elapsed > 30) ? 30 : seconds-elapsed);
signal(SIGALRM, SIG_IGN);
if (verbose)
fputs(res ? " woken by signal!\n\n" : "\n", stderr);
return (res>0);
}
uint32_t
crc16(const uint8_t *buf, size_t len, uint32_t start)
{
uint32_t crc = start; // should be 0xFFFF first time
for (size_t pos = 0; pos < len; pos++) {
crc ^= (uint32_t)buf[pos]; // XOR byte into least sig. byte of crc
for (int i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
crc >>= 1; // Just shift right
}
}
return crc;
}
void
hexlify(FILE *where, const uint8_t *buf, size_t len, bool newl)
{
while (len--) {
fprintf(where, "%2.2x", (int)*buf++);
}
if (newl)
fputc('\n', where);
}