-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
75 lines (59 loc) · 1.69 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
#include "util.h"
void read_file(char **buffer, const char *filename)
{
char *contents;
size_t filesize;
FILE *file;
// open the file
file = fopen(filename, "rb");
// search the file size
fseek(file, 0, SEEK_END);
filesize = ftell(file);
// rewind
rewind(file);
// allocate memory block and copy bits into memory
contents = (char *)malloc(filesize * sizeof(char));
fread(contents, sizeof(char), filesize, file);
// don't forget the 0x0 at the end of the string
contents[filesize-1] = 0;
// finish
fclose(file);
(*buffer) = contents;
}
void NOT_IMPLEMENTED(const char *filename, int line, const char *function)
{
printf("%s:%d:%s(): NOT IMPLEMENTED\n", filename, line, function);
abort();
}
int __indent_size = 0;
struct node_s {
char *string;
struct node_s *next;
};
void INFUNC(const char *function)
{
//char *indent = " ";
fprintf(stderr, "%*s" "+ %s()\n", __indent_size, " ", function);
__indent_size += 4;
}
void SUCCESSED(const char *function)
{
__indent_size -= 4;
fprintf(stderr, "%*s" "- %s()\n", __indent_size, " ", function);
}
int time_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;
return (diff<0);
}
void time_print(struct timeval *tv)
{
char buffer[30];
time_t curtime;
printf("%d.%06d", (int)tv->tv_sec, (int)tv->tv_usec);
curtime = tv->tv_sec;
strftime(buffer, 30, "%m-%d-%Y %T", localtime(&curtime));
printf(" = %s.%06d\n", buffer, (int)tv->tv_usec);
}