2 * Copyright (C) 2007, 2008 Nokia Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * This file contains various common stuff used by UBI utilities.
22 * Authors: Artem Bityutskiy
26 #define PROGRAM_NAME "ubiutils"
29 #include <sys/types.h>
38 * get_multiplier - convert size specifier to an integer multiplier.
39 * @str: the size specifier string
41 * This function parses the @str size specifier, which may be one of
42 * 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive
43 * size multiplier in case of success and %-1 in case of failure.
45 static int get_multiplier(const char *str
)
50 /* Remove spaces before the specifier */
51 while (*str
== ' ' || *str
== '\t')
54 if (!strcmp(str
, "KiB"))
56 if (!strcmp(str
, "MiB"))
58 if (!strcmp(str
, "GiB"))
59 return 1024 * 1024 * 1024;
65 * ubiutils_get_bytes - convert a string containing amount of bytes into an
67 * @str: string to convert
69 * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB'
70 * size specifiers. Returns positive amount of bytes in case of success and %-1
73 long long ubiutils_get_bytes(const char *str
)
76 long long bytes
= strtoull(str
, &endp
, 0);
78 if (endp
== str
|| bytes
< 0) {
79 fprintf(stderr
, "incorrect amount of bytes: \"%s\"\n", str
);
84 int mult
= get_multiplier(endp
);
87 fprintf(stderr
, "bad size specifier: \"%s\" - "
88 "should be 'KiB', 'MiB' or 'GiB'\n", endp
);
98 * ubiutils_print_bytes - print bytes.
99 * @bytes: variable to print
100 * @bracket: whether brackets have to be put or not
102 * This is a helper function which prints amount of bytes in a human-readable
103 * form, i.e., it prints the exact amount of bytes following by the approximate
104 * amount of Kilobytes, Megabytes, or Gigabytes, depending on how big @bytes
107 void ubiutils_print_bytes(long long bytes
, int bracket
)
116 printf("%lld bytes", bytes
);
118 if (bytes
> 1024 * 1024 * 1024)
119 printf("%s%.1f GiB", p
, (double)bytes
/ (1024 * 1024 * 1024));
120 else if (bytes
> 1024 * 1024)
121 printf("%s%.1f MiB", p
, (double)bytes
/ (1024 * 1024));
122 else if (bytes
> 1024 && bytes
!= 0)
123 printf("%s%.1f KiB", p
, (double)bytes
/ 1024);
132 * ubiutils_print_text - print text and fold it.
133 * @stream: file stream to print to
134 * @text: text to print
135 * @width: maximum allowed text width
137 * Print text and fold it so that each line would not have more then @width
140 void ubiutils_print_text(FILE *stream
, const char *text
, int width
)
147 fprintf(stream
, "%s\n", text
);
153 while (!isspace(p
[pos
])) {
160 fprintf(stream
, "%s\n", line
);
165 while (pos
< width
) {
176 fprintf(stream
, "%s\n", line
);
179 while (p
[pos
] && isspace(p
[pos
]))
185 * ubiutils_srand - randomly seed the standard pseudo-random generator.
187 * This helper function seeds the standard libc pseudo-random generator with a
188 * more or less random value to make sure the 'rand()' call does not return the
189 * same sequence every time UBI utilities run. Returns zero in case of success
190 * and a %-1 in case of error.
192 int ubiutils_srand(void)
199 * Just assume that a combination of the PID + current time is a
200 * reasonably random number.
202 if (gettimeofday(&tv
, &tz
))
205 seed
= (unsigned int)tv
.tv_sec
;
206 seed
+= (unsigned int)tv
.tv_usec
;