forked from ibm-s390-linux/smc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
145 lines (128 loc) · 3.21 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
/*
* SMC Tools - Shared Memory Communication Tools
*
* Copyright IBM Corp. 2020
*
* Author(s): Guvenc Gulce <[email protected]>
*
* Userspace program for SMC Information display
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "util.h"
void print_unsup_msg(void)
{
fprintf(stderr, "Error: Kernel does not support this parameter !\n");
exit(-1);
}
void print_type_error(void) {
fprintf(stderr, "Error: You entered an invalid type. Possible values are smcd and smcr !\n");
exit(-1);
}
char* trim_space(char *str)
{
char *end;
while (isspace(*str)) {
str = str + 1;
}
/* remove trailing whitespace */
end = str + strlen((const char*)str) - 1;
while (end > str && isspace(*end)) {
end = end - 1;
}
*(end+1) = '\0';
return str;
}
int contains(const char *prfx, const char *str)
{
if (!*prfx)
return 1;
while (*str && *prfx == *str) {
prfx++;
str++;
}
return !!*prfx;
}
static void determine_mag_factor(int leading_places, char *magnitude,
double *factor)
{
if (leading_places < 7) {
*magnitude = 'K';
*factor = 1000;
}
else if (leading_places < 10) {
*magnitude = 'M';
*factor = 1000000;
}
else if (leading_places < 13) {
*magnitude = 'G';
*factor = 1000000000;
}
else {
// this is quite expensive, hence we avoid if possible
*factor = pow(1000, leading_places/3);
if (leading_places < 16)
*magnitude = 'T';
else if (leading_places < 19)
*magnitude = 'P';
else
*magnitude = '?';
}
}
static void determine_digs(int leading_places, int max_digs,
int *num_full_digs, int *num_places)
{
*num_full_digs = leading_places % 3;
if (*num_full_digs == 0)
*num_full_digs = 3;
*num_places = max_digs - *num_full_digs - 2;
if (*num_places <= 0) {
*num_places = 0;
*num_full_digs = max_digs - 1;
}
}
int get_abbreviated(uint64_t num, int max_digs, char *res)
{
int num_full_digs, leading_places;
char magnitude;
int num_places;
double factor;
char tmp[128];
if (num == 0) {
snprintf(res, max_digs + 1, "0");
return 1;
}
leading_places = sprintf(tmp, "%lld", (long long int)num);
if (leading_places < 4) {
snprintf(res, max_digs + 1, "%lu", num);
return leading_places;
}
determine_digs(leading_places, max_digs, &num_full_digs, &num_places);
determine_mag_factor(leading_places, &magnitude, &factor);
double tmpnum = num / factor;
if (tmpnum + 5 * pow(10, -1 - num_places) >= pow(10, num_full_digs)) {
if (num_places > 0)
// just strip down one decimal place,
// e.g. 9.96... with 1.1 format would result in
// 10.0 otherwise
num_places--;
else {
// indicate that we need one more leading place
// e.g. 999.872 with 3.0 format digits would result
// in 1.000K otherwise
leading_places++;
determine_digs(leading_places, max_digs, &num_full_digs, &num_places);
determine_mag_factor(leading_places, &magnitude, &factor);
tmpnum = num / factor;
}
}
snprintf(res, max_digs + 1, "%*.*lf%c", max_digs - 1, num_places, num / factor, magnitude);
return 0;
}