-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.c
312 lines (239 loc) · 5.89 KB
/
common.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
Copyright (c) 2019 Colum Paget <colums.projects@googlemail.com>
* SPDX-License-Identifier: GPL-3.0
*/
#include "common.h"
#include <utime.h>
TConfig *Config=NULL;
int running = 0;
time_t Now;
#define FNV_INIT_VAL 2166136261
unsigned int fnv_hash(unsigned const char *key, int NoOfItems)
{
unsigned const char *p;
unsigned int h = FNV_INIT_VAL;
for (p=key; *p !='\0' ; p++ ) h = ( h * 16777619 ) ^ *p;
return(h % NoOfItems);
}
//basename but handle directory paths, that end in '/'
//we don't want to return a blank string for paths that end in '/'
const char *cbasename(const char *Path)
{
char *ptr;
if (! Path) return("");
ptr=strrchr(Path, '/');
if (ptr)
{
//this was a path ending in '/', so rewind to the next '/'
if (*(ptr+1)=='\0')
{
ptr--;
while ((ptr > Path) && (*ptr != '/')) ptr--;
}
if (*ptr=='/') return(ptr+1);
return(ptr);
}
return(Path);
}
char *rstrlcat(char *Dest, const char *Src, int SrcLen)
{
int dlen, len;
dlen=StrLen(Dest);
len=dlen + SrcLen;
Dest=realloc(Dest, len + 1);
strncpy(Dest+dlen, Src, SrcLen);
Dest[len]='\0';
return(Dest);
}
char *rstrcat(char *Dest, const char *Src)
{
return(rstrlcat(Dest, Src, StrLen(Src)));
}
char *rstrlcpy(char *Dest, const char *Src, int SrcLen)
{
if (Dest) *Dest=0;
return(rstrlcat(Dest, Src, SrcLen));
}
char *rstrcpy(char *Dest, const char *Src)
{
if (Dest) *Dest=0;
return(rstrcat(Dest,Src));
}
void strrep(char *Str, char c1, char c2)
{
char *ptr;
for (ptr=Str; *ptr != '\0'; ptr++)
{
if (*ptr==c1) *ptr=c2;
}
}
const char *advanceto(const char *ptr, char term)
{
for (; *ptr!=term; ptr++)
{
if (*ptr=='\\') ptr++;
if (*ptr=='\0') break;
}
return(ptr);
}
const char *rstrtok(const char *Str, const char *Separators, char **Token)
{
const char *ptr;
if ((! Str) || (*Str=='\0')) return(NULL);
for (ptr=Str; *ptr!='\0'; ptr++)
{
if (*ptr=='"') ptr=advanceto(ptr+1, '"');
else if (*ptr=='\'') ptr=advanceto(ptr+1, '\'');
else if (*ptr=='\\')
{
ptr++;
if (*ptr=='\0') break;
}
else if (strchr(Separators, *ptr)) break;
}
*Token=rstrlcpy(*Token, Str, ptr-Str);
if (*ptr !='\0') ptr++;
return(ptr);
}
char *rstrquot(char *RetStr, const char *Str, const char *QuoteChars)
{
const char *ptr;
RetStr=rstrcpy(RetStr, "");
ptr=Str;
while (ptr && (*ptr !='\0'))
{
if (strchr(QuoteChars, *ptr)) RetStr=rstrcat(RetStr, "\\");
RetStr=rstrlcat(RetStr, ptr, 1);
ptr++;
}
return(RetStr);
}
char *rstrunquot(char *RetStr, const char *Str)
{
const char *ptr;
RetStr=rstrcpy(RetStr, "");
ptr=Str;
while (ptr && (*ptr !='\0'))
{
if (*ptr=='\\') ptr++;
RetStr=rstrlcat(RetStr, ptr, 1);
ptr++;
}
return(RetStr);
}
void StripQuotes(char *Str)
{
int len;
if ( (*Str=='\'') || (*Str=='\"') )
{
len=strlen(Str);
Str[len-1]='\0';
memmove(Str, Str+1, len);
}
}
int FDPushBytes(int out, char *Buffer, size_t len)
{
int wrote=0, result;
while (wrote < len)
{
result=write(out, Buffer+wrote, len-wrote);
if (result < 0) break;
wrote += result;
}
return(wrote);
}
int FDCopyBytes(int in, int out)
{
char Buffer[1024];
int bytes_read, wrote;
bytes_read=read(in, Buffer, 1024);
return(FDPushBytes(out, Buffer, bytes_read));
}
void TouchFile(const char *Path)
{
struct utimbuf times;
struct stat Stat;
if (stat(Path, &Stat)==0)
{
times.actime=time(NULL);
times.modtime=Stat.st_mtime;
utime(Path, ×);
}
}
char *PathSearch(char *RetStr, const char *FileName, const char *Path)
{
char *Dir=NULL;
const char *ptr;
if (*FileName=='/') return(rstrcpy(RetStr, FileName));
RetStr=rstrcpy(RetStr, "");
ptr=rstrtok(Path, ":", &Dir);
while (ptr)
{
RetStr=rstrcpy(RetStr, Dir);
RetStr=rstrcat(RetStr, "/");
RetStr=rstrcat(RetStr, FileName);
if (access(RetStr, F_OK)==0) break;
RetStr=rstrcpy(RetStr, "");
ptr=rstrtok(ptr, ":", &Dir);
}
destroy(Dir);
return(RetStr);
}
int ParseURL(const char *URL, char **Proto, char **Host, char **Port, char **Path)
{
const char *ptr;
char *Tempstr=NULL;
ptr=strchr(URL, ':');
if (! ptr) return(FALSE);
ptr=rstrtok(URL, ":", Proto);
while (*ptr=='/') ptr++;
ptr=rstrtok(ptr, "/", &Tempstr);
ptr--; /*to get to '/' */
*Path=rstrcpy(*Path, ptr);
ptr=rstrtok(Tempstr, ":", Host);
*Port=rstrcpy(*Port, ptr);
destroy(Tempstr);
return(TRUE);
}
void MkDirPath(const char *Dir)
{
char *Token=NULL, *Path=NULL;
const char *ptr;
ptr=rstrtok(Dir, "/", &Token);
while (ptr)
{
Path=rstrcat(Path, Token);
mkdir(Path, 0700);
Path=rstrcat(Path, "/");
ptr=rstrtok(ptr, "/", &Token);
}
destroy(Token);
destroy(Path);
}
void LongFormatMetric(char *Str, int len, long value)
{
if (value > 1000000000) snprintf(Str, len, "%0.2fG", ((float) value) / 1000000000.0);
else if (value > 1000000) snprintf(Str, len, "%0.2fM", ((float) value) / 1000000.0);
else if (value > 1000) snprintf(Str, len, "%0.2fk", ((float) value) / 1000000.0);
else snprintf(Str, len, "%lu", value);
}
void Exec(const char *CmdLine)
{
const char *ptr;
char *Token=NULL, **Args=NULL;
int count=0;
ptr=rstrtok(CmdLine, " ", &Token);
while (ptr)
{
StripQuotes(Token);
Args=realloc(Args, (count+10) * sizeof(char *));
Args[count]=strdup(Token);
count++;
ptr=rstrtok(ptr, " ", &Token);
}
Args[count]=NULL;
destroy(Token);
execv(Args[0], Args);
//should never reach here
_exit(1);
}