reindent-20030715
[openafs.git] / src / util / test / b32.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <stdio.h>
17 #if !defined(AFS_NT40_ENV)
18 main()
19 {
20     printf("b32 not required for this operating system.\n");
21     exit(1);
22 }
23 #else
24
25 #include "afsutil.h"
26
27 char *prog = "b32";
28
29 void
30 Usage(void)
31 {
32     printf("Usage: %s -s n [n ...] (converts int to base 32 string)\n", prog);
33     printf("Usage: %s -i s [s ...] (converts base 32 string to int)\n", prog);
34     printf("Usage: %s -c n [n ...] (converts to base 32 and back)\n", prog);
35     printf
36         ("Usage: %s -r low high inc (verify converter using range and inc)\n",
37          prog);
38     exit(1);
39 }
40
41 void btoi(int ac, char **av);
42 void itob(int ac, char **av);
43 void check(int ac, char **av);
44 void verifyRange(int ac, char **av);
45
46
47 main(int ac, char **av)
48 {
49     if (ac < 3)
50         Usage();
51
52     if (!strcmp(av[1], "-s"))
53         itob(ac, av);
54     else if (!strcmp(av[1], "-i"))
55         btoi(ac, av);
56     else if (!strcmp(av[1], "-c"))
57         check(ac, av);
58     else if (!strcmp(av[1], "-r"))
59         verifyRange(ac, av);
60     else
61         Usage();
62
63     exit(0);
64 }
65
66 void
67 btoi(int ac, char **av)
68 {
69     int i;
70
71     if (ac == 3)
72         printf("%d\n", base32_to_int(av[2]));
73     else {
74         for (i = 2; i < ac; i++)
75             printf("%s: %d\n", av[i], base32_to_int(av[i]));
76     }
77 }
78
79 void
80 itob(int ac, char **av)
81 {
82     int i;
83     b32_string_t str;
84
85     if (ac == 3)
86         printf("%s\n", int_to_base32(str, atoi(av[2])));
87     else {
88         for (i = 2; i < ac; i++)
89             printf("%d: %s\n", atoi(av[i]), int_to_base32(str, atoi(av[i])));
90     }
91 }
92
93 void
94 check(int ac, char **av)
95 {
96     int i;
97     int in, out;
98     b32_string_t str;
99
100     printf("%10s %10s %10s\n", "input", "base32", "output");
101     for (i = 2; i < ac; i++) {
102         in = atoi(av[i]);
103         (void)int_to_base32(str, in);
104         out = base32_to_int(str);
105         printf("%10d %10s %10d\n", in, str, out);
106     }
107 }
108
109 #define PROGRESS 1000000
110 void
111 verifyRange(int ac, char **av)
112 {
113     unsigned int inc, low, high;
114     int n;
115     unsigned int in, out;
116     b32_string_t str;
117
118     if (ac != 5)
119         Usage();
120
121     low = (unsigned int)atoi(av[2]);
122     high = (unsigned int)atoi(av[3]);
123     inc = (unsigned int)atoi(av[4]);
124
125     n = 0;
126     for (in = low; in <= high; in += inc) {
127         n++;
128         if (n % PROGRESS == 0)
129             printf(" %d", n);
130         (void)int_to_base32(str, in);
131         out = base32_to_int(str);
132         if (in != out) {
133             printf("\n\nERROR: in=%u, str='%s', out=%u\n", in, str, out);
134             exit(1);
135         }
136     }
137     printf("\nCOMPLETE - no errors found in range %u,%u,%u\n", low, high,
138            inc);
139 }
140
141
142 #endif /* !NT */