-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
102 lines (87 loc) · 1.74 KB
/
util.c
File metadata and controls
102 lines (87 loc) · 1.74 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Some internal utility functions
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "pgpr_internal.h"
static uint32_t fixed_time; /* for testing */
uint32_t pgprCurrentTime(void)
{
return fixed_time ? fixed_time : (uint32_t)time(NULL);
}
void pgprSetFixedTime(int64_t t)
{
fixed_time = (uint32_t)t;
}
void *pgprMemdup(const void *ptr, size_t len)
{
void *mem = ptr ? pgprMalloc(len) : NULL;
if (mem && len)
memcpy(mem, ptr, len);
return mem;
}
char *pgprStrdup(const char *s)
{
return s ? pgprMemdup(s, strlen(s) + 1) : NULL;
}
int pgprAsprintf(char **strp, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (n >= -1) {
size_t nb = n + 1;
*strp = pgprMalloc(nb);
if (*strp) {
va_start(ap, fmt);
n = vsnprintf(*strp, nb, fmt, ap);
va_end(ap);
}
}
return n;
}
#ifndef PGPR_RPM_INTREE
void pgprOOM(size_t num, size_t len)
{
if (num)
fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
else if (len)
fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
else
fprintf(stderr, "Out of memory!\n");
abort();
}
void *pgprMalloc(size_t len)
{
void *r = malloc(len ? len : 1);
if (!r)
pgprOOM(0, len ? len : 1);
return r;
}
void *pgprRealloc(void *old, size_t len)
{
if (old == 0)
old = malloc(len ? len : 1);
else
old = realloc(old, len ? len : 1);
if (!old)
pgprOOM(0, len ? len : 1);
return old;
}
void *pgprCalloc(size_t num, size_t len)
{
void *r;
if (num == 0 || len == 0)
r = malloc(1);
else
r = calloc(num, len);
if (!r)
pgprOOM(num, len ? len : 1);
return r;
}
#endif