Home | History | Annotate | Download | only in e2p
      1 /*
      2  * percent.c		- Take percentage of a number
      3  *
      4  * Copyright (C) 2006  Theodore Ts'o <tytso (at) mit.edu>
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Library
      8  * General Public License, version 2.
      9  * %End-Header%
     10  */
     11 
     12 #include "e2p.h"
     13 
     14 #include <stdlib.h>
     15 
     16 /*
     17  * We work really hard to calculate this accurately, while avoiding
     18  * an overflow.  "Is there a hyphen in anal-retentive?"  :-)
     19  */
     20 unsigned int e2p_percent(int percent, unsigned int base)
     21 {
     22 	unsigned int mask = ~((1 << (sizeof(unsigned int) - 1) * 8) - 1);
     23 
     24 	if (!percent)
     25 		return 0;
     26 	if (100 % percent == 0)
     27 		return base / (100 / percent);
     28 	if (mask & base)
     29 		return (base / 100) * percent;
     30 	return base * percent / 100;
     31 }
     32 
     33 #ifdef DEBUG
     34 #include <unistd.h>
     35 #include <stdio.h>
     36 
     37 main(int argc, char **argv)
     38 {
     39 	unsigned int base;
     40 	int percent;
     41 	char *p;
     42 	int log_block_size = 0;
     43 
     44 	if (argc != 3) {
     45 		fprintf(stderr, "Usage: %s percent base\n", argv[0]);
     46 		exit(1);
     47 	}
     48 
     49 	percent = strtoul(argv[1], &p, 0);
     50 	if (p[0] && p[1]) {
     51 		fprintf(stderr, "Bad percent: %s\n", argv[1]);
     52 		exit(1);
     53 	}
     54 
     55 	base = strtoul(argv[2], &p, 0);
     56 	if (p[0] && p[1]) {
     57 		fprintf(stderr, "Bad base: %s\n", argv[2]);
     58 		exit(1);
     59 	}
     60 
     61 	printf("%d percent of %u is %u.\n", percent, base,
     62 	       e2p_percent(percent, base));
     63 
     64 	exit(0);
     65 }
     66 #endif
     67