]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/convert.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 #include "platform_defs.h"
19 for (len
= 0, tmp
= val
; tmp
> 0; tmp
= tmp
/ base
)
21 return len
== 0 ? 1 : len
;
25 * Convert string to int64_t, set errno if the conversion fails or
26 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
27 * prior to conversion so you can check for bad inputs by examining
28 * errno immediately after the call.
39 i
= strtoll(s
, &sp
, base
);
41 * If the input would over or underflow, return the clamped
42 * value and let the user check errno. If we went all the
43 * way to the end of the input, return the converted value;
46 if (errno
|| (*sp
== '\0' && sp
!= s
))
49 /* Not all the input was consumed, return error. */
55 * Convert string to int32_t, set errno if the conversion fails or
56 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
57 * prior to conversion so you can check for bad inputs by examining
58 * errno immediately after the call.
70 if (i
> INT32_MAX
|| i
< INT32_MIN
) {
78 * Convert string to int16_t, set errno if the conversion fails or
79 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
80 * prior to conversion so you can check for bad inputs by examining
81 * errno immediately after the call.
93 if (i
> INT16_MAX
|| i
< INT16_MIN
) {
101 * Convert string to uint64_t, set errno if the conversion fails or
102 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
103 * prior to conversion so you can check for bad inputs by examining
104 * errno immediately after the call.
111 unsigned long long i
;
115 i
= strtoull(s
, &sp
, base
);
117 * If the input would over or underflow, return the clamped
118 * value and let the user check errno. If we went all the
119 * way to the end of the input, return the converted value;
120 * errno will be zero.
122 if (errno
|| (*sp
== '\0' && sp
!= s
))
125 /* Not all the input was consumed, return error. */
131 * Convert string to uint32_t, set errno if the conversion fails or
132 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
133 * prior to conversion so you can check for bad inputs by examining
134 * errno immediately after the call.
143 i
= cvt_u64(s
, base
);
146 if (i
> UINT32_MAX
) {
154 * Convert string to uint16_t, set errno if the conversion fails or
155 * doesn't fit. Does not allow unit specifiers. Sets errno to zero
156 * prior to conversion so you can check for bad inputs by examining
157 * errno immediately after the call.
166 i
= cvt_u64(s
, base
);
169 if (i
> UINT16_MAX
) {
176 #define EXABYTES(x) ((long long)(x) << 60)
177 #define PETABYTES(x) ((long long)(x) << 50)
178 #define TERABYTES(x) ((long long)(x) << 40)
179 #define GIGABYTES(x) ((long long)(x) << 30)
180 #define MEGABYTES(x) ((long long)(x) << 20)
181 #define KILOBYTES(x) ((long long)(x) << 10)
193 i
= strtoll(s
, &sp
, 0);
194 if (i
== 0 && sp
== s
)
205 return i
* blocksize
;
207 return i
* sectorsize
;
224 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
225 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
226 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
227 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
228 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
229 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
240 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
242 if (value
>= EXABYTES(1)) {
243 fmt
= precise
? "%.f EiB" : "%.3f EiB";
244 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
245 } else if (value
>= PETABYTES(1)) {
246 fmt
= precise
? "%.f PiB" : "%.3f PiB";
247 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
248 } else if (value
>= TERABYTES(1)) {
249 fmt
= precise
? "%.f TiB" : "%.3f TiB";
250 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
251 } else if (value
>= GIGABYTES(1)) {
252 fmt
= precise
? "%.f GiB" : "%.3f GiB";
253 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
254 } else if (value
>= MEGABYTES(1)) {
255 fmt
= precise
? "%.f MiB" : "%.3f MiB";
256 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
257 } else if (value
>= KILOBYTES(1)) {
258 fmt
= precise
? "%.f KiB" : "%.3f KiB";
259 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
261 snprintf(str
, size
, "%f bytes", value
);
265 #define MINUTES_TO_SECONDS(m) ((m) * 60)
266 #define HOURS_TO_SECONDS(h) ((h) * MINUTES_TO_SECONDS(60))
267 #define DAYS_TO_SECONDS(d) ((d) * HOURS_TO_SECONDS(24))
268 #define WEEKS_TO_SECONDS(w) ((w) * DAYS_TO_SECONDS(7))
277 i
= strtoul(s
, &sp
, 0);
278 if (i
== 0 && sp
== s
)
282 if ((*sp
== 'm' && sp
[1] == '\0') ||
283 (strcmp(sp
, "minutes") == 0) ||
284 (strcmp(sp
, "minute") == 0))
285 return MINUTES_TO_SECONDS(i
);
286 if ((*sp
== 'h' && sp
[1] == '\0') ||
287 (strcmp(sp
, "hours") == 0) ||
288 (strcmp(sp
, "hour") == 0))
289 return HOURS_TO_SECONDS(i
);
290 if ((*sp
== 'd' && sp
[1] == '\0') ||
291 (strcmp(sp
, "days") == 0) ||
292 (strcmp(sp
, "day") == 0))
293 return DAYS_TO_SECONDS(i
);
294 if ((*sp
== 'w' && sp
[1] == '\0') ||
295 (strcmp(sp
, "weeks") == 0) ||
296 (strcmp(sp
, "week") == 0))
297 return WEEKS_TO_SECONDS(i
);
302 * Convert from arbitrary user strings into a numeric ID.
303 * If it's all numeric, we convert that inplace, else we do
304 * the name lookup, and return the found identifier.
312 unsigned long prid_long
;
316 * Allow either a full numeric or a valid projectname, even
317 * if it starts with a digit.
319 prid_long
= strtoul(project
, &sp
, 10);
320 if (*project
!= '\0' && *sp
== '\0') {
321 if ((prid_long
== ULONG_MAX
&& errno
== ERANGE
)
322 || (prid_long
> (prid_t
)-1))
324 return (prid_t
)prid_long
;
326 prj
= getprnam(project
);
337 unsigned long uid_long
;
340 uid_long
= strtoul(user
, &sp
, 10);
341 if (sp
!= user
&& *sp
== '\0') {
342 if ((uid_long
== ULONG_MAX
&& errno
== ERANGE
)
343 || (uid_long
> (uid_t
)-1))
345 return (uid_t
)uid_long
;
347 pwd
= getpwnam(user
);
358 unsigned long gid_long
;
361 gid_long
= strtoul(group
, &sp
, 10);
362 if (sp
!= group
&& *sp
== '\0') {
363 if ((gid_long
== ULONG_MAX
&& errno
== ERANGE
)
364 || (gid_long
> (gid_t
)-1))
366 return (gid_t
)gid_long
;
368 grp
= getgrnam(group
);