| 1 | /* |
| 2 | ** Copyright 2001 Double Precision, Inc. |
| 3 | ** See COPYING for distribution information. |
| 4 | */ |
| 5 | |
| 6 | #if HAVE_CONFIG_H |
| 7 | #include "config.h" |
| 8 | #endif |
| 9 | #include "numlib.h" |
| 10 | #include <string.h> |
| 11 | |
| 12 | |
| 13 | static void cat_n(char *buf, unsigned long n) |
| 14 | { |
| 15 | char bb[NUMBUFSIZE+1]; |
| 16 | char *p=bb+sizeof(bb)-1; |
| 17 | |
| 18 | *p=0; |
| 19 | do |
| 20 | { |
| 21 | *--p = "0123456789"[n % 10]; |
| 22 | n=n/10; |
| 23 | } while (n); |
| 24 | strcat(buf, p); |
| 25 | } |
| 26 | |
| 27 | char *libmail_str_sizekb(unsigned long n, char *sizebuf) |
| 28 | { |
| 29 | /* If size is less than 1K bytes, display it as 0.xK */ |
| 30 | |
| 31 | if (n < 1024) |
| 32 | { |
| 33 | strcpy(sizebuf, "0."); |
| 34 | cat_n(sizebuf, (int)(10 * n / 1024 )); |
| 35 | strcat(sizebuf, "K"); |
| 36 | } |
| 37 | /* If size is less than 1 meg, display is as xK */ |
| 38 | |
| 39 | else if (n < 1024 * 1024) |
| 40 | { |
| 41 | *sizebuf=0; |
| 42 | cat_n(sizebuf, (unsigned long)(n+512)/1024); |
| 43 | strcat(sizebuf, "K"); |
| 44 | } |
| 45 | |
| 46 | /* Otherwise, display in megabytes */ |
| 47 | |
| 48 | else |
| 49 | { |
| 50 | unsigned long nm=(double)n / (1024.0 * 1024.0) * 10; |
| 51 | |
| 52 | *sizebuf=0; |
| 53 | cat_n( sizebuf, nm / 10); |
| 54 | strcat(sizebuf, "."); |
| 55 | cat_n( sizebuf, nm % 10); |
| 56 | strcat(sizebuf, "M"); |
| 57 | } |
| 58 | |
| 59 | return (sizebuf); |
| 60 | } |
| 61 | |