-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
145 lines (128 loc) · 2.58 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
/*
* util.c
*
* Author: Kazuya Goto <[email protected]>
* Created on May 17, 2006
* Last modified: Jun 24, 2013
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <sys/resource.h>
#include <sys/time.h>
#include "util.h"
void *emalloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
perror("malloc");
fprintf(stderr, "malloc of %lu bytes failed\n", size);
exit(2);
}
return ptr;
}
void *erealloc(void *ptr, size_t size)
{
void *rptr;
rptr = realloc(ptr, size);
if (rptr == NULL) {
perror("realloc");
fprintf(stderr, "realloc of %lu bytes failed\n", size);
exit(2);
}
return rptr;
}
char *estrdup(const char *source)
{
char *dest;
dest = (char *) emalloc((strlen(source)+1) * sizeof(char));
return strcpy(dest, source);
}
FILE *efopen(const char *path, const char *mode)
{
FILE *fp;
fp = fopen(path, mode);
if (fp == NULL) {
perror("fopen");
exit(2);
}
return fp;
}
FILE *etmpfile(void)
{
/* char tmpname[64]; */
FILE *tmp_file;
/*
strcpy(tmpname, progname());
strcat(tmpname, ".tmp");
tmp_file = efopen(tmpname, "w+");
*/
tmp_file = tmpfile();
if (tmp_file == NULL) {
perror("tmpfile");
exit(2);
}
return tmp_file;
}
enum { BUFSIZE = 4096 };
void file_copy(FILE *from_file, FILE *to_file)
{
size_t rbytes;
char tmpbuf[BUFSIZE];
while ((rbytes = fread(tmpbuf, 1, sizeof(tmpbuf), from_file)) > 0)
fwrite(tmpbuf, 1, rbytes, to_file);
}
static char *name;
void setprogname(const char *str)
{
const char *p;
/* strcpy(progname, basename(str)); */
if ((p = strrchr(str, '/')) == NULL &&
(p = strrchr(str, '\\')) == NULL)
p = str;
else
p++;
name = (char *) malloc((strlen(p)+1) * sizeof(char));
if (name == NULL) {
perror("in setprogname()");
exit(2);
}
strcpy(name, p);
}
const char *progname(void)
{
return name;
}
void print_log(FILE *fp, const char *format, ...)
{
time_t t;
static char date_str[32];
static char buf[1024];
va_list ap;
int ret;
time(&t);
strcpy(date_str, ctime(&t));
*strchr(date_str, '\n') = '\0';
snprintf(buf, 1023, "%s: %s\n", date_str, format);
va_start(ap, format);
ret = vfprintf(fp, buf, ap);
va_end(ap);
fflush(fp);
}
double get_cputime(void)
{
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return (double) rusage.ru_utime.tv_sec +
((double) rusage.ru_utime.tv_usec) * 1.0e-6;
}
double get_wtime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) tv.tv_sec + ((double) tv.tv_usec) * 1.0e-6;
}