d9898ee8 |
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 | static const char rcsid[]="$Id: strsize.c,v 1.2 2003/01/05 04:01:17 mrsam Exp $"; |
13 | |
14 | static void cat_n(char *buf, unsigned long n) |
15 | { |
16 | char bb[NUMBUFSIZE+1]; |
17 | char *p=bb+sizeof(bb)-1; |
18 | |
19 | *p=0; |
20 | do |
21 | { |
22 | *--p = "0123456789"[n % 10]; |
23 | n=n/10; |
24 | } while (n); |
25 | strcat(buf, p); |
26 | } |
27 | |
28 | char *libmail_str_sizekb(unsigned long n, char *sizebuf) |
29 | { |
30 | /* If size is less than 1K bytes, display it as 0.xK */ |
31 | |
32 | if (n < 1024) |
33 | { |
34 | strcpy(sizebuf, "0."); |
35 | cat_n(sizebuf, (int)(10 * n / 1024 )); |
36 | strcat(sizebuf, "K"); |
37 | } |
38 | /* If size is less than 1 meg, display is as xK */ |
39 | |
40 | else if (n < 1024 * 1024) |
41 | { |
42 | *sizebuf=0; |
43 | cat_n(sizebuf, (unsigned long)(n+512)/1024); |
44 | strcat(sizebuf, "K"); |
45 | } |
46 | |
47 | /* Otherwise, display in megabytes */ |
48 | |
49 | else |
50 | { |
51 | unsigned long nm=(double)n / (1024.0 * 1024.0) * 10; |
52 | |
53 | *sizebuf=0; |
54 | cat_n( sizebuf, nm / 10); |
55 | strcat(sizebuf, "."); |
56 | cat_n( sizebuf, nm % 10); |
57 | strcat(sizebuf, "M"); |
58 | } |
59 | |
60 | return (sizebuf); |
61 | } |
62 | |