/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Research25*26* return scaled number n27* string width is 5 chars or less28* if m>1 then n divided by m before scaling29*/3031#include <ast.h>3233char*34fmtnum(register unsigned long n, int m)35{36register int i;37register unsigned long r;38char* buf;39int z;4041char suf[2];4243if (m > 1)44{45r = n;46n /= m;47r -= n;48}49else50r = 0;51suf[1] = 0;52if (n < 1024)53suf[0] = 0;54else if (n < 1024 * 1024)55{56suf[0] = 'k';57r = ((n % 1024) * 100) / 1024;58n /= 1024;59}60else if (n < 1024 * 1024 * 1024)61{62suf[0] = 'm';63r = ((n % (1024 * 1024)) * 100) / (1024 * 1024);64n /= 1024 * 1024;65}66else67{68suf[0] = 'g';69r = ((n % (1024 * 1024 * 1024)) * 100) / (1024 * 1024 * 1024);70n /= 1024 * 1024 * 1024;71}72if (r)73{74if (n >= 100)75r = 0;76else if (n >= 10)77{78i = 1;79if (r >= 10)80r /= 10;81}82else83i = 2;84}85buf = fmtbuf(z = 8);86if (r)87sfsprintf(buf, z, "%lu.%0*lu%s", n, i, r, suf);88else89sfsprintf(buf, z, "%lu%s", n, suf);90return buf;91}929394