forked from E3V3A/gsm-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.c
More file actions
43 lines (41 loc) · 1.28 KB
/
compat.c
File metadata and controls
43 lines (41 loc) · 1.28 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <time64.h>
#define CHAR_BIT 8
// From http://code.metager.de/source/xref/chromium/base/os_compat_android.cc
// 32-bit Android has only timegm64() and not timegm().
// We replicate the behaviour of timegm() when the result overflows time_t.
time_t timegm(struct tm* const t) {
// time_t is signed on Android.
static const time_t kTimeMax = ~(1L << (sizeof(time_t) * CHAR_BIT - 1));
static const time_t kTimeMin = (1L << (sizeof(time_t) * CHAR_BIT - 1));
time64_t result = timegm64(t);
if (result < kTimeMin || result > kTimeMax)
return -1;
return result;
}
// https://android.googlesource.com/platform/external/elfutils/+/android-4.3.1_r1/bionic-fixup/getline.c
ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
char *ptr;
ptr = fgetln(stream, n);
if (ptr == NULL) {
return -1;
}
/* Free the original ptr */
if (*lineptr != NULL) free(*lineptr);
/* Add one more space for '\0' */
size_t len = n[0] + 1;
/* Update the length */
n[0] = len;
/* Allocate a new buffer */
*lineptr = malloc(len);
/* Copy over the string */
memcpy(*lineptr, ptr, len-1);
/* Write the NULL character */
(*lineptr)[len-1] = '\0';
/* Return the length of the new buffer */
return len;
}