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