xref: /openbsd-src/lib/libutil/fmt_scaled.3 (revision 51914d64007640868175c5e596da1bfb3bbc4edb)
1*51914d64Sjca.\"	$OpenBSD: fmt_scaled.3,v 1.8 2016/07/16 16:10:44 jca Exp $
2f353b857Sian.\" Copyright (c) 2001, 2003 Ian Darwin.  All rights reserved.
3f353b857Sian.\"
4f353b857Sian.\" Redistribution and use in source and binary forms, with or without
5f353b857Sian.\" modification, are permitted provided that the following conditions
6f353b857Sian.\" are met:
7f353b857Sian.\" 1. Redistributions of source code must retain the above copyright
8f353b857Sian.\"    notice, this list of conditions and the following disclaimer.
9f353b857Sian.\" 2. Redistributions in binary form must reproduce the above copyright
10f353b857Sian.\"    notice, this list of conditions and the following disclaimer in the
11f353b857Sian.\"    documentation and/or other materials provided with the distribution.
12f353b857Sian.\" 3. The name of the author may not be used to endorse or promote products
13f353b857Sian.\"    derived from this software without specific prior written permission.
14f353b857Sian.\"
15f353b857Sian.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f353b857Sian.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f353b857Sian.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f353b857Sian.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19f353b857Sian.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f353b857Sian.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f353b857Sian.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f353b857Sian.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f353b857Sian.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f353b857Sian.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f353b857Sian.\"
26*51914d64Sjca.Dd $Mdocdate: July 16 2016 $
27f353b857Sian.Dt FMT_SCALED 3
28f353b857Sian.Os
29f353b857Sian.Sh NAME
30f353b857Sian.Nm fmt_scaled ,
31f353b857Sian.Nm scan_scaled
32f353b857Sian.Nd handle numbers with a human-readable scale
33f353b857Sian.Sh SYNOPSIS
3486f9d4cdStedu.In util.h
35f353b857Sian.Ft int
36f353b857Sian.Fn scan_scaled "char *number_w_scale" "long long *result"
37f353b857Sian.Ft int
38f353b857Sian.Fn fmt_scaled "long long number" "char *result"
39f353b857Sian.Sh DESCRIPTION
40f353b857SianThe
41f353b857Sian.Fn scan_scaled
42f353b857Sianfunction scans the given number and looks for a terminal scale multiplier
43f353b857Sianof B, K, M, G, T, P or E
44f353b857Sian.Pq in either upper or lower case
45f353b857Sianfor Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, Exabyte
46f353b857Sian.Po computed using powers of two, i.e., Megabyte = 1024*1024
47f353b857Sian.Pc .
48f353b857SianThe number can have a decimal point, as in 1.5K, which returns 1536
49f353b857Sian.Pq 1024+512 .
50f353b857SianIf no scale factor is found, B is assumed.
51f353b857Sian.Pp
52f353b857SianThe
53f353b857Sian.Fn fmt_scaled
54f353b857Sianfunction formats a number for display using the same
55f353b857Sian"human-readable" format, that is, a number with one of the above scale factors.
56f353b857SianNumbers will be printed with a maximum of four digits (preceded by
57f353b857Siana minus sign if the value is negative); values such
58f353b857Sianas 0B, 100B, 1023B, 1K, 1.5K, 5.5M, and so on, will be generated.
59f353b857SianThe
60f353b857Sian.Qq result
61f353b857Sianbuffer must be allocated with at least
62f353b857Sian.Dv FMT_SCALED_STRSIZE
63f353b857Sianbytes.
640cbef5eaSderaadtThe result will be left-justified in the given space, and NUL-terminated.
65f353b857Sian.Sh RETURN VALUES
66f353b857SianThe
67f353b857Sian.Fn scan_scaled
68f353b857Sianand
69f353b857Sian.Fn fmt_scaled
70f353b857Sianfunctions
71f353b857Sianreturn 0 on success.
72f353b857SianIn case of error, they return \-1, leave
73f353b857Sian.Va *result
74f353b857Sianas is, and set
75f353b857Sian.Va errno
76f353b857Sianto one of the following values:
77f353b857Sian.Dv ERANGE
78f353b857Sianif the input string represents a number that is too large to represent.
79f353b857Sian.Dv EINVAL
80f353b857Sianif an unknown character was used as scale factor, or
81f353b857Sianif the input to
82f353b857Sian.Fn scan_scaled
83f353b857Sianwas malformed, e.g., too many '.' characters.
84f353b857Sian.Sh EXAMPLES
85f353b857Sian.Bd -literal -offset indent
86f353b857Sianchar *cinput = "1.5K";
87f353b857Sianlong long result;
88f0b1c391Sjmcif (scan_scaled(cinput, &result) == 0)
89*51914d64Sjca	printf("%s -> %lld\en", cinput, result);
90f353b857Sianelse
91f353b857Sian	fprintf(stderr, "%s - invalid\en", cinput);
92f353b857Sian
93f353b857Sianchar buf[FMT_SCALED_STRSIZE];
94f353b857Sianlong long ninput = 10483892;
95f353b857Sianif (fmt_scaled(ninput, buf) == 0)
96f353b857Sian	printf("%lld -> %s\en", ninput, buf);
97f353b857Sianelse
98f353b857Sian	fprintf(stderr, "fmt scaled failed (errno %d)", errno);
99f353b857Sian.Ed
100f353b857Sian.Sh SEE ALSO
101f353b857Sian.Xr printf 3 ,
102f353b857Sian.Xr scanf 3
103404f6687Sjmc.Sh HISTORY
104404f6687SjmcThe functions
105404f6687Sjmc.Fn fmt_scaled
106404f6687Sjmcand
107404f6687Sjmc.Fn scan_scaled
108404f6687Sjmcfirst appeared in
109404f6687Sjmc.Ox 3.4 .
110404f6687Sjmc.Sh AUTHORS
11127e95970Sschwarze.An -nosplit
11227e95970Sschwarze.An Ken Stailey
11327e95970Sschwarzewrote the first version of the code that became
114404f6687Sjmc.Fn fmt_scaled ,
115404f6687Sjmcoriginally inside
116404f6687Sjmc.Ox
117404f6687Sjmc.Xr df 1 .
11827e95970Sschwarze.An Ian Darwin
11927e95970Sschwarzeexcerpted this and made it into a library routine
12027e95970Sschwarze(with significant help from
12127e95970Sschwarze.An Paul Janzen ) ,
12227e95970Sschwarzeand wrote
123404f6687Sjmc.Fn scan_scaled .
124f353b857Sian.Sh BUGS
125f353b857SianSome of the scale factors have misleading meanings in lower case
126f353b857Sian(p for P is incorrect; p should be pico- and P for Peta-).
127f353b857SianHowever, we bend the SI rules in favor of common sense here.
128f353b857SianA person creating a disk partition of "100m" is unlikely to require
129f353b857Sian100 millibytes (i.e., 0.1 byte) of storage in the partition;
130f353b857Sian100 megabytes is the only reasonable interpretation.
131f353b857Sian.Pp
132f353b857SianCannot represent the larger scale factors on all architectures.
133f353b857Sian.Pp
134f353b857SianIgnores the current locale.
135