dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / string / fmtnum.c
blob14db553969807b7fba46e5b7a8dfd1d57e4b66a1
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * Glenn Fowler
25 * AT&T Research
27 * return scaled number n
28 * string width is 5 chars or less
29 * if m>1 then n divided by m before scaling
32 #include <ast.h>
34 char*
35 fmtnum(register unsigned long n, int m)
37 register int i;
38 register unsigned long r;
39 char* buf;
40 int z;
42 char suf[2];
44 if (m > 1)
46 r = n;
47 n /= m;
48 r -= n;
50 else
51 r = 0;
52 suf[1] = 0;
53 if (n < 1024)
54 suf[0] = 0;
55 else if (n < 1024 * 1024)
57 suf[0] = 'k';
58 r = ((n % 1024) * 100) / 1024;
59 n /= 1024;
61 else if (n < 1024 * 1024 * 1024)
63 suf[0] = 'm';
64 r = ((n % (1024 * 1024)) * 100) / (1024 * 1024);
65 n /= 1024 * 1024;
67 else
69 suf[0] = 'g';
70 r = ((n % (1024 * 1024 * 1024)) * 100) / (1024 * 1024 * 1024);
71 n /= 1024 * 1024 * 1024;
73 if (r)
75 if (n >= 100)
76 r = 0;
77 else if (n >= 10)
79 i = 1;
80 if (r >= 10)
81 r /= 10;
83 else
84 i = 2;
86 buf = fmtbuf(z = 8);
87 if (r)
88 sfsprintf(buf, z, "%lu.%0*lu%s", n, i, r, suf);
89 else
90 sfsprintf(buf, z, "%lu%s", n, suf);
91 return buf;