Break out the file output code from generate_uudmap's main() to allow re-use.
[p5sagit/p5-mst-13.2.git] / generate_uudmap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 /* If it turns out that we need to make this conditional on config.sh derived
4    values, it might be easier just to rip out the use of strerrer().  */
5 #include <string.h>
6 /* If a platform doesn't support errno.h, it's probably so strange that
7    "hello world" won't port easily to it.  */
8 #include <errno.h>
9
10 void output_block_to_file(const char *progname, const char *filename,
11                           const char *block, size_t count) {
12   FILE *const out = fopen(filename, "w");
13
14   if (!out) {
15     fprintf(stderr, "%s: Could not open '%s': %s\n", progname, filename,
16             strerror(errno));
17     exit(1);
18   }
19
20   fputs("{\n    ", out);
21   while (count--) {
22     fprintf(out, "%d", *block);
23     block++;
24     if (count) {
25       fputs(", ", out);
26       if (!(count & 15)) {
27         fputs("\n    ", out);
28       }
29     }
30   }
31   fputs("\n}\n", out);
32
33   if (fclose(out)) {
34     fprintf(stderr, "%s: Could not close '%s': %s\n", progname, filename,
35             strerror(errno));
36     exit(1);
37   }
38 }
39
40
41 static const char PL_uuemap[]
42 = "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
43
44 typedef unsigned char U8;
45
46 /* This will ensure it is all zeros.  */
47 static char PL_uudmap[256];
48
49 int main(int argc, char **argv) {
50   size_t i;
51
52   if (argc < 2 || argv[1][0] == '\0') {
53     fprintf(stderr, "Usage: %s uudemap.h\n", argv[0]);
54     return 1;
55   }
56
57   for (i = 0; i < sizeof(PL_uuemap) - 1; ++i)
58     PL_uudmap[(U8)PL_uuemap[i]] = (char)i;
59   /*
60    * Because ' ' and '`' map to the same value,
61    * we need to decode them both the same.
62    */
63   PL_uudmap[(U8)' '] = 0;
64
65   output_block_to_file(argv[0], argv[1], PL_uudmap, sizeof(PL_uudmap));
66
67   return 0;
68 }
69
70