Back to index

courier  0.68.2
strsize.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "config.h"
00008 #endif
00009 #include      "numlib.h"
00010 #include      <string.h>
00011 
00012 
00013 static void cat_n(char *buf, unsigned long n)
00014 {
00015 char    bb[NUMBUFSIZE+1];
00016 char    *p=bb+sizeof(bb)-1;
00017 
00018         *p=0;
00019         do
00020         {
00021                 *--p = "0123456789"[n % 10];
00022                 n=n/10;
00023         } while (n);
00024         strcat(buf, p);
00025 }
00026 
00027 char *libmail_str_sizekb(unsigned long n, char *sizebuf)
00028 {
00029         /* If size is less than 1K bytes, display it as 0.xK */
00030 
00031         if (n < 1024)
00032         {
00033                 strcpy(sizebuf, "0.");
00034                 cat_n(sizebuf, (int)(10 * n / 1024 ));
00035                 strcat(sizebuf, "K");
00036         }
00037         /* If size is less than 1 meg, display is as xK */
00038 
00039         else if (n < 1024 * 1024)
00040         {
00041                 *sizebuf=0;
00042                 cat_n(sizebuf, (unsigned long)(n+512)/1024);
00043                 strcat(sizebuf, "K");
00044         }
00045 
00046         /* Otherwise, display in megabytes */
00047 
00048         else
00049         {
00050         unsigned long nm=(double)n / (1024.0 * 1024.0) * 10;
00051 
00052                 *sizebuf=0;
00053                 cat_n( sizebuf, nm / 10);
00054                 strcat(sizebuf, ".");
00055                 cat_n( sizebuf, nm % 10);
00056                 strcat(sizebuf, "M");
00057         }
00058 
00059        return (sizebuf);
00060 }
00061