forked from markfasheh/duperemove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
154 lines (137 loc) · 3.17 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
146
147
148
149
150
151
152
153
154
/*
* util.c
*
* Copyright (C) 2014 SUSE except where noted. All rights reserved.
*
* Code taken from btrfs-progs/util.c is:
* Copyright (C) 2007 Oracle. All rights reserved.
* Copyright (C) 2008 Morey Roof. All rights reserved.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Authors: Mark Fasheh <mfasheh@suse.de>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <inttypes.h>
#include <execinfo.h>
#include <sys/time.h>
#include "debug.h"
#include "util.h"
int human_readable = 0;
uint64_t parse_size(char *s)
{
int i;
char c;
uint64_t mult = 1;
for (i = 0; s && s[i] && isdigit(s[i]); i++) ;
if (!i) {
fprintf(stderr, "ERROR: size value is empty\n");
exit(50);
}
if (s[i]) {
c = tolower(s[i]);
switch (c) {
case 'e':
mult *= 1024;
/* fallthrough */
case 'p':
mult *= 1024;
/* fallthrough */
case 't':
mult *= 1024;
/* fallthrough */
case 'g':
case 'G':
mult *= 1024;
/* fallthrough */
case 'm':
case 'M':
mult *= 1024;
/* fallthrough */
case 'k':
case 'K':
mult *= 1024;
/* fallthrough */
case 'b':
break;
default:
fprintf(stderr, "ERROR: Unknown size descriptor "
"'%c'\n", c);
exit(1);
}
}
if (s[i] && s[i+1]) {
fprintf(stderr, "ERROR: Illegal suffix contains "
"character '%c' in wrong position\n",
s[i+1]);
exit(51);
}
return strtoull(s, NULL, 10) * mult;
}
static char *size_strs[] = { "", "K", "M", "G", "T", "P", "E"};
int pretty_size_snprintf(uint64_t size, char *str, size_t str_bytes)
{
uint32_t num_divs = 0;
float fraction;
if (str_bytes == 0)
return 0;
if (!human_readable)
return snprintf(str, str_bytes, "%"PRIu64, size);
if (size < 1024){
fraction = size;
num_divs = 0;
} else {
uint64_t last_size = size;
num_divs = 0;
while(size >= 1024){
last_size = size;
size /= 1024;
num_divs ++;
}
if (num_divs >= ARRAY_SIZE(size_strs)) {
str[0] = '\0';
return -1;
}
fraction = (float)last_size / 1024;
}
return snprintf(str, str_bytes, "%.1f%s", fraction,
size_strs[num_divs]);
}
void print_stack_trace(void)
{
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;
trace_size = backtrace(trace, 16);
messages = backtrace_symbols(trace, trace_size);
printf("[stack trace follows]\n");
for (i=0; i < trace_size; i++)
printf("%s\n", messages[i]);
free(messages);
}
void record_start(struct elapsed_time *e, const char *name)
{
e->name = name;
gettimeofday(&e->start, NULL);
}
static void record_end(struct elapsed_time *e)
{
gettimeofday(&e->end, NULL);
e->elapsed = (e->end.tv_sec - e->start.tv_sec) +
((e->end.tv_usec - e->start.tv_usec) / 1000000.0F);
}
void record_end_print(struct elapsed_time *e)
{
record_end(e);
printf("%s took %fs\n", e->name, e->elapsed);
}