forked from yhtsnda/httping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
129 lines (99 loc) · 2.13 KB
/
utils.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
/* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
#define _GNU_SOURCE
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <libintl.h>
#include "error.h"
#include "utils.h"
double get_ts(void)
{
struct timeval ts;
struct timezone tz;
if (gettimeofday(&ts, &tz) == -1)
error_exit(gettext("gettimeofday failed"));
return (double)ts.tv_sec + ((double)ts.tv_usec)/1000000.0 + (double)(tz.tz_minuteswest * 60);
}
void split_string(const char *in, const char *split, char ***list, int *list_n)
{
char *copy = strdup(in), *pc = copy;
int split_len = strlen(split);
for(;;)
{
char *term = strstr(pc, split);
if (term)
*term = 0x00;
*list = (char **)realloc(*list, (*list_n + 1) * sizeof(char *));
(*list)[*list_n] = strdup(pc);
(*list_n)++;
if (!term)
break;
pc = term + split_len;
}
free(copy);
}
void free_splitted_string(char **list, int n)
{
int index=0;
for(index=0; index<n; index++)
free(list[index]);
free(list);
}
void str_add(char **to, const char *what, ...)
{
int len_to = *to ? strlen(*to) : 0;
char *buffer = NULL;
int len_what = 0;
va_list ap;
/* FIXME: should aprintf here: does solaris have aprintf? */
va_start(ap, what);
len_what = vasprintf(&buffer, what, ap);
va_end(ap);
*to = (char *)realloc(*to, len_to + len_what + 1);
memcpy(&(*to)[len_to], buffer, len_what);
(*to)[len_to + len_what] = 0x00;
free(buffer);
}
char * format_value(double value, int digits_sig, int digits_nsig, char abbreviate)
{
char *out = NULL, *mul = "";
double a = fabs(value);
double div = 1.0;
if (!abbreviate)
{
}
else if (a >= GIGA)
{
div = GIGA;
mul = "G";
}
else if (a >= MEGA)
{
div = MEGA;
mul = "M";
}
else if (a >= KILO)
{
div = KILO;
mul = "k";
}
if (mul[0])
digits_sig--;
(void)asprintf(&out, "%*.*f%s", digits_sig, digits_nsig, value / div, mul);
return out;
}
void myusleep(useconds_t v)
{
int s = v / 1000000;
if (s)
sleep(s);
v %= 1000000;
if (v)
usleep(v);
}