-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.c
172 lines (146 loc) · 2.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (C) 2021 Edward LEI <edward_lei72@hotmail.com>
*
* license: MIT license
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "util.h"
/* dst - The destination pointer
* src - The source pointer
* return Pointer to the end of the destination string - the next destination
*/
char *strbld(char *dst,
char const *src)
{
while ((*dst = *src++)) {
dst++;
}
return dst;
}
/*
* outbuf - buffer to hold the output number, size must >= 16
* n - number to be converted
* base - number base for conversion; decimal=10,hex=16
* sign - sign bit set in output? ex. '+' or '-', ' ' if not set
*
* return - the length of the outbuf
*/
int itos(unsigned char *outbuf,
unsigned long n,
const int base,
const char sign)
{
int i = 12;
int j = 0;
do {
outbuf[i] = "0123456789ABCDEF"[n % base];
i--;
n = n/base;
} while (n > 0);
if (sign != ' ') {
outbuf[0] = sign;
++j;
}
while (++i < 13) {
outbuf[j++] = outbuf[i];
}
outbuf[j] = '\0';
return j;
}
char *split_kv(char *kv,
const char delim)
{
char *p = kv;
do {
if (*p == delim) {
/*
* xxxxxxxxxxxx: xxxxxxxxxxxx\0
* ^ ^ ^
* h p p
*/
*p = '\0';
p++;
break;
}
p++;
} while (*p);
do {
/* assume that there is no 2nd ':' */
if (*p != ' ' && *p != '\t') break;
p++;
} while (*p);
return p; /* return the value */
}
void gmt_date(char *date_gmt,
const long *tmgmt)
{
struct tm tm_gmt;
tm_gmt = *gmtime(tmgmt);
/*
* The total length of data with format ("%a, %d %b %Y %H:%M:%S %Z")
* should be (29 + 1)
*
* ex. Sat, 04 Jul 2020 10:29:53 GMT\0
* ^
* |------------29-------------| 1
*/
strftime(date_gmt, 30, "%a, %d %b %Y %H:%M:%S %Z", &tm_gmt);
}
long mk_etag(char *etag, const char *file)
{
struct stat sb;
stat(file, &sb);
sprintf(etag, "\"%lu-%lu-%ld\"", sb.st_ino, sb.st_size, sb.st_mtime);
return sb.st_mtime;
}
char *find_ext(const char *file)
{
char *dot = strrchr(file, '.');
if(!dot || dot == file) return "";
return dot + 1;
}
int msleep(const long tms)
{
struct timespec ts;
int ret;
if (tms < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = tms / 1000;
ts.tv_nsec = (tms % 1000) * 1000000;
do {
ret = nanosleep(&ts, &ts);
} while (ret && errno == EINTR);
return ret;
}
int nsleep(const long tms)
{
struct timespec ts;
int ret;
if (tms < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = 0;
ts.tv_nsec = tms;
do {
ret = nanosleep(&ts, &ts);
} while (ret && errno == EINTR);
return ret;
}
long mstime()
{
struct timeval tv;
long msec;
gettimeofday(&tv, 0);
msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
return msec;
}