-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathglib.cpp
More file actions
443 lines (397 loc) · 10.9 KB
/
glib.cpp
File metadata and controls
443 lines (397 loc) · 10.9 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**************************************************************************************************
* General library
* (c) Etherjuice
*
**************************************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
using namespace std;
#include "cstr.h"
#include "glib.h"
//#define DEBUGFILE_GLIB "/var/www/ramdisk/debug_glib.log"
//#include "debug.h"
// Generate TIMESTAMP for log files
// Returns:
// 2017-01-20 20:42:56
char *timestamp(char *ts, size_t max_sz)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
snprintf(ts, max_sz, "%04d-%02d-%02d %02d:%02d:%02d", 2000+tm->tm_year-100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
return ts;
}
// Generates timestamp for elapsed time calculation
// Returns:
// yy-mm-dd hh:mm:ss
char *timestamp_record(char *ts, size_t ts_max_sz)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
snprintf(ts, ts_max_sz, "%02d-%02d-%02d %02d:%02d:%02d",
tm->tm_year-100,
tm->tm_mon,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
return ts;
} // timestamp_record()
// input
// ts = "yy-mm-dd hh:mm:ss"
char *timestamp_elapsed(char *ts, char* elapsed, size_t max_sz)
{
elapsed[0]='\0';
if(strlen(ts) == 17)
{
char ts_now[24];
char sdate[16], stime[16];
char sh[4], sm[4], ss[4];
char a[4], b[4], c[4];
// -----------------------------------------
// tm1= time now
timestamp_record(ts_now, sizeof(ts_now));
sscanf(ts_now, "%s %s", sdate, stime);
// date
cstr_replace(sdate, '-', ' ');
sscanf(sdate, "%s %s %s", a, b, c);
unsigned long d1= atoi(b) * 30 + atoi(c);
// time
cstr_replace(stime, ':', ' ');
sscanf(stime, "%s %s %s", sh, sm, ss);
unsigned long tm1= ((unsigned long)atoi(sh) * 60L + (unsigned long)atoi(sm)) * 60L + (unsigned long)atoi(ss);
// -----------------------------------------
// tm0 = time input ts= yy-mm-dd hh:mm:ss
sscanf(ts, "%s %s", sdate, stime);
// date
cstr_replace(sdate, '-', ' ');
sscanf(sdate, "%s %s %s", a, b, c);
unsigned long d0= atoi(b) * 30 + atoi(c);
// time
cstr_replace(stime, ':', ' ');
sscanf(stime, "%s %s %s", sh, sm, ss);
unsigned long tm0= ((unsigned long)atoi(sh) * 60L + (unsigned long)atoi(sm)) * 60L + (unsigned long)atoi(ss);
unsigned long t= tm1 - tm0;
if(tm0 > tm1) t= tm1 + 24 * 60L * 60L - tm0;
unsigned long h, m, s;
s= (unsigned long) (t % 60L);
m= (unsigned long) ((t / 60L) % 60L);
h= (unsigned long) (t / (60L * 60L));
char s1[8], s2[8]; s1[0]='\0'; s2[0]='\0';
// days
//if( (d1-d0) > 1 ) snprintf(s1, sizeof(s1), "%lu ", (d1-d0-1));
//else if(h>24) snprintf(s1, sizeof(s1), "%lu ", h / 24);
if(d1>d0 || h>24)
{
unsigned long ddss= d1 * 24L * 3600L + tm1 - d0 * 24L * 3600L - tm0;
snprintf(s1, sizeof(s1), "%lu ", ddss / (24L * 3600L) );
}
// hours
if(h) snprintf(s2, sizeof(s2), "%02lu ", h % 24);
snprintf(elapsed, max_sz, "%s%s%02lu %02lu", s1, s2, m, s);
fprintf(stdout,"\nelapsed= %s", elapsed);
#ifdef _HADEBUG_H_
char line[128];
snprintf(line, sizeof(line),"tm1= %lu tm0= %lu t= %lu d= %lu", tm1, tm0, t, (d1-d0));
trace2file(DEBUGFILE_GLIB, line);
fprintf(stdout,"\n%s", line);
snprintf(line, sizeof(line),"ts_now= %s ts= %s", ts_now, ts);
trace2file(DEBUGFILE_GLIB, line);
fprintf(stdout,"\n%s", line);
#endif
}
return elapsed;
} // timestamp_elapsed()
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Data record
// ------------------------------------------------------------------------------------------------
int logWriteTS(const char *ts, const char *text, char* filename)
{
if(filename[0]=='\0') return -1;
//char ts[64];
FILE *fp = fopen(filename, "r+");
// CREATE - If file does not exist then create it
if(fp == NULL)
{
// create file
fp = fopen(filename, "w");
if(fp == NULL) {
fprintf(stdout, "[ERROR] logWrite: failed to create file %s\n", filename);
fflush(stdout);
return -1;
}
file_mode_change(filename, "zx-11a:zx-11a", "666" );
}
// File is open for update
if (fp != NULL) {
//timestamp(ts, sizeof(ts)-1);
fseek(fp, 0L, SEEK_END);
// WRITE <timestamp><text><\n>
fwrite(ts, sizeof(char), strlen(ts), fp);
fwrite(" ", sizeof(char), 1, fp);
size_t filelength = fwrite(text, sizeof(char), strlen(text), fp);
fwrite("\n", sizeof(char), 1, fp);
fclose(fp);
return (int) filelength;
}
return -1;
}
int fileRead(const char*filename, char*memptr, unsigned int ufsz)
{
FILE *fp = fopen(filename, "r");
if ( fp != NULL )
{
int n=0;
unsigned int i= 0;
do {
n= fread (&memptr[i], sizeof(char), ufsz - i, fp);
if(n>0) i += (size_t) n;
} while (n>0 && i<ufsz);
memptr[i]='\0';
fclose(fp);
return (int) i;
}
return -1;
}
// ------------------------------------------------------------------------------------------------
// FILE
// ------------------------------------------------------------------------------------------------
// File management and manipulation library
// File functions
int file_size(const char *filename)
{
int filelength= 0;
long size;
FILE *fp = fopen(filename, "r");
if (fp != NULL) {
if (fseek(fp, 0L, SEEK_END) == 0)
size= ftell(fp);
// this is the normal flow ...
if(size<=0) {
fprintf(stdout, "\nSEEK_END size %ld", size);
fflush(stdout);
fclose(fp);
return (int) size;
}
// ... but try to read something to double check - the other checks does not work always
if (fseek(fp, 0L, SEEK_SET) == 0) {
char buffer[2];
size_t sz= fread(buffer, sizeof(char), 1, fp);
if(sz==0) {
fprintf(stdout, "\nfile_size sz %d", sz);
fflush(stdout);
fclose(fp);
return -1;
}
}
else {
fprintf(stdout, "\nSEEK_SET");
fflush(stdout);
fclose(fp);
return -1;
}
filelength= (int) size;
fclose(fp);
return filelength;
}
else return -1;
}
int file_getcontent(const char *filename, char *buffer, size_t max)
{
int filelength= 0;
FILE *fp = fopen(filename, "r");
if (fp != NULL) {
/* Go to the end of the file. */
if (fseek(fp, 0L, SEEK_END) == 0) {
if ((filelength = ftell(fp)) > 0) {
/* Go back to the start of the file. */
if (fseek(fp, 0L, SEEK_SET) == 0) {
/* Read the entire file into memory. */
size_t newLen = fread(buffer, sizeof(char), filelength> (int)max? max : filelength, fp);
filelength= newLen;
}
}
}
fclose(fp);
}
else return -1;
return filelength;
}
bool file_is(const char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp != NULL) {
fclose(fp);
return true;
}
return false;
}
bool isNumber(char *s)
{
unsigned int i=0;
if(s[i]!='\0' && (s[i]=='-' || s[i]=='+')) i++;
for (; i<strlen(s) && isdigit(s[i]); i++);
return !(i<strlen(s));
}
int file_mode_change(const char *filename, const char *user, const char *mode)
{
if(fork() == 0) {
execl("/bin/chown", "chown", user, filename, (char *)0);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
if(fork() == 0) {
execl("/bin/chmod", "chmod", mode, filename, (char *)0);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
return 0;
}
}
return -1;
}
int file_copy(const char *filename, const char *filename_copy)
{
if(fork() == 0) {
execl("/bin/cp", "cp", filename, filename_copy, (char *)0);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
return 0;
}
return -1;
}
// filename - full file name including path
int file_rotate(char *filename)
{
fprintf(stdout, "\n");
char path[32];
path[0]='\0';
char name[32];
name[0]='\0';
char ext[32];
ext[0]='\0';
int pa= 0, pb;
while( (pb=cstr_find((char*)filename, (char*)"/", pa)) >= 0) pa= pb+1;
if(pa>0) cstr_sub( (char*)filename, path, 0, pa-1);
// fprintf(stdout, "path %s\n", path);
int i= cstr_find( (char*)&filename[pa], (char*)".");
if(i>0) cstr_sub( (char*)&filename[pa], name, 0, i-1);
// fprintf(stdout, "name %s\n", name);
if(i>0) strcpy( ext, (char*)&filename[pa+i+1]);
// fprintf(stdout, "ext %s\n", ext);
char sdate[32];
timestamp(sdate, sizeof(sdate));
// fprintf(stdout, "i= %d %s\n", i, timestamp(sdate, sizeof(sdate)) );
cstr_replace(sdate, ' ', '_');
cstr_replace(sdate, '-', '\0');
cstr_replace(sdate, ':', '\0');
// fprintf(stdout, "%s\n", sdate );
char str[128];
snprintf(str, sizeof(str), "%s%s_%s.%s", path, name, sdate, ext);
fprintf(stdout, "FILE ROTATE %s\n", str );
if(file_copy(filename, str) <0) return -1;
FILE *fp = fopen(filename, "w");
if (fp != NULL) {
fclose(fp);
return 0;
}
return -1;
}
int file_delete(const char *filename)
{
/*
if(fork() == 0) {
execl("/bin/rm", "rm", filename, (char *)0);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
return 0;
}
return -1;
*/
char str[128];
str[0]='\0';
FILE *in;
snprintf(str, sizeof(str), "sudo rm %s", filename);
if(!(in = popen(str, "r"))){
return -1;
}
else {
while(fgets(str, sizeof(str), in)!=NULL);
//str[n]='\0';
pclose(in);
}
fprintf(stdout, str);
return 0;
}
// ------------------------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// -------------------------- LOG -----------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
char defaultfilename[128] = {0};
int logInit(const char *filename, bool create)
{
defaultfilename[0]='\0';
snprintf(defaultfilename, sizeof(defaultfilename),"%s", filename);
if(filename[0]=='\0') return -1;
if(create)
{
FILE *fp = fopen(filename, "w");
if(fp == NULL)
{
fprintf(stdout, "[ERROR] logWrite: failed to create file %s\n", filename);
fflush(stdout);
return -1;
}
else
{
fclose(fp);
file_mode_change(filename, "zx-11a:zx-11a", "666" );
}
}
return 0;
}
int logWrite(const char *text, char* filename)
{
if(filename[0]=='\0') return -1;
char ts[64];
FILE *fp = fopen(filename, "r+");
// CREATE - If file does not exist then create it
if(fp == NULL)
{
// create file
fp = fopen(filename, "w");
if(fp == NULL) {
fprintf(stdout, "[ERROR] logWrite: failed to create file %s\n", filename);
fflush(stdout);
return -1;
}
file_mode_change(filename, "zx-11a:zx-11a", "666" );
}
// File is open for update
if (fp != NULL) {
timestamp(ts, sizeof(ts)-1);
fseek(fp, 0L, SEEK_END);
// WRITE <timestamp><text><\n>
fwrite(ts, sizeof(char), strlen(ts), fp);
fwrite(" ", sizeof(char), 1, fp);
size_t filelength = fwrite(text, sizeof(char), strlen(text), fp);
fwrite("\n", sizeof(char), 1, fp);
fclose(fp);
return (int) filelength;
}
return -1;
}
/* END OF FILE */