aboutsummaryrefslogtreecommitdiffhomepage
path: root/contrib/notmuch-deliver/numlib/strsize.c
blob: 0a7dcaa8d78fef0a45717e491ace63f967353fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
** Copyright 2001 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	"numlib.h"
#include	<string.h>

static const char rcsid[]="$Id: strsize.c,v 1.2 2003/01/05 04:01:17 mrsam Exp $";

static void cat_n(char *buf, unsigned long n)
{
char    bb[NUMBUFSIZE+1];
char    *p=bb+sizeof(bb)-1;

        *p=0;
        do
        {
                *--p = "0123456789"[n % 10];
                n=n/10;
        } while (n);
        strcat(buf, p);
}

char *libmail_str_sizekb(unsigned long n, char *sizebuf)
{
        /* If size is less than 1K bytes, display it as 0.xK */

        if (n < 1024)
        {
                strcpy(sizebuf, "0.");
                cat_n(sizebuf, (int)(10 * n / 1024 ));
                strcat(sizebuf, "K");
        }
        /* If size is less than 1 meg, display is as xK */

        else if (n < 1024 * 1024)
        {
                *sizebuf=0;
                cat_n(sizebuf, (unsigned long)(n+512)/1024);
                strcat(sizebuf, "K");
        }

        /* Otherwise, display in megabytes */

        else
        {
        unsigned long nm=(double)n / (1024.0 * 1024.0) * 10;

                *sizebuf=0;
                cat_n( sizebuf, nm / 10);
                strcat(sizebuf, ".");
                cat_n( sizebuf, nm % 10);
                strcat(sizebuf, "M");
        }

	return (sizebuf);
}