Adding upstream version 6.02~pre8+dfsg.
[syslinux-debian/hramrach.git] / com32 / hdt / hdt-util.c
blobb8d743d34c1e8973e3c7e56a67098bbc55fd70ee
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
12 * conditions:
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 * -----------------------------------------------------------------------
29 #include <stdio.h>
30 #include <string.h>
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);
39 int mib = b >> 10;
40 int gib = mib >> 10;
41 int tib = gib >> 10;
43 if (tib > 0)
44 sprintf(buffer, "%3d.%02d TiB", tib,sub(gib));
45 else if (gib > 0)
46 sprintf(buffer, "%3d.%02d GiB", gib,sub(mib));
47 else if (mib > 0)
48 sprintf(buffer, "%3d.%02d MiB", mib,sub(b));
49 else
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;
60 if (*size > 1000) {
61 *size = *size / 1000;
62 strlcpy(unit, "MB", 2);
63 if (*size > 1000) {
64 *previous_size = *size;
65 *size = *size / 1000;
66 strlcpy(previous_unit, unit, 2);
67 strlcpy(unit, "GB", 2);
68 if (*size > 1000) {
69 *previous_size = *size;
70 *size = *size / 1000;
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);
84 int mib = b / 1000;
85 int gib = mib / 1000;
86 int tib = gib / 1000;
88 if (tib > 0)
89 sprintf(buffer, "%3d.%02d TB", tib,sub_dec(gib));
90 else if (gib > 0)
91 sprintf(buffer, "%3d.%02d GB", gib,sub_dec(mib));
92 else if (mib > 0)
93 sprintf(buffer, "%3d.%02d MB", mib,sub_dec(b));
94 else
95 sprintf(buffer, "%d B", b);