1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Pierre-Alexandre Meyer - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * -----------------------------------------------------------------------
32 /* Computing div(x,y) */
33 #define sub(val) (((val%1024)*100)>>10)
34 #define sub_dec(val) (((val%1000)*100)/1000)
36 void sectors_to_size(int sectors
, char *buffer
)
38 int b
= (sectors
/ 2);
44 sprintf(buffer
, "%3d.%02d TiB", tib
,sub(gib
));
46 sprintf(buffer
, "%3d.%02d GiB", gib
,sub(mib
));
48 sprintf(buffer
, "%3d.%02d MiB", mib
,sub(b
));
50 sprintf(buffer
, "%d B", b
);
53 void sectors_to_size_dec(char *previous_unit
, int *previous_size
, char *unit
,
54 int *size
, int sectors
)
56 *size
= sectors
/ 2; // Converting to bytes
57 strlcpy(unit
, "KB", 2);
58 strlcpy(previous_unit
, unit
, 2);
59 *previous_size
= *size
;
62 strlcpy(unit
, "MB", 2);
64 *previous_size
= *size
;
66 strlcpy(previous_unit
, unit
, 2);
67 strlcpy(unit
, "GB", 2);
69 *previous_size
= *size
;
71 strlcpy(previous_unit
, unit
, 2);
72 strlcpy(unit
, "TB", 2);
78 /* Return the human readable size of device
79 * This function avoid disk's size rounding while
80 * not using float as they aren't currently supported */
81 void sectors_to_size_dec2(int sectors
, char *buffer
)
83 int b
= (sectors
/ 2);
89 sprintf(buffer
, "%3d.%02d TB", tib
,sub_dec(gib
));
91 sprintf(buffer
, "%3d.%02d GB", gib
,sub_dec(mib
));
93 sprintf(buffer
, "%3d.%02d MB", mib
,sub_dec(b
));
95 sprintf(buffer
, "%d B", b
);