This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Check return values in the test programs run by syslfs.t and lfs.t
[perl5.git] / generate_uudmap.c
index 31aba75..2c3e24a 100644 (file)
@@ -1,3 +1,8 @@
+/* Originally this program just generated uudmap.h
+   However, when we later wanted to generate bitcount.h, it was easier to
+   refactor it and keep the same name, than either alternative - rename it,
+   or duplicate all of the Makefile logic for a second program.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 /* If it turns out that we need to make this conditional on config.sh derived
@@ -7,6 +12,37 @@
    "hello world" won't port easily to it.  */
 #include <errno.h>
 
+void output_block_to_file(const char *progname, const char *filename,
+                         const char *block, size_t count) {
+  FILE *const out = fopen(filename, "w");
+
+  if (!out) {
+    fprintf(stderr, "%s: Could not open '%s': %s\n", progname, filename,
+           strerror(errno));
+    exit(1);
+  }
+
+  fputs("{\n    ", out);
+  while (count--) {
+    fprintf(out, "%d", *block);
+    block++;
+    if (count) {
+      fputs(", ", out);
+      if (!(count & 15)) {
+       fputs("\n    ", out);
+      }
+    }
+  }
+  fputs("\n}\n", out);
+
+  if (fclose(out)) {
+    fprintf(stderr, "%s: Could not close '%s': %s\n", progname, filename,
+           strerror(errno));
+    exit(1);
+  }
+}
+
+
 static const char PL_uuemap[]
 = "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
 
@@ -14,20 +50,14 @@ typedef unsigned char U8;
 
 /* This will ensure it is all zeros.  */
 static char PL_uudmap[256];
+static char PL_bitcount[256];
 
 int main(int argc, char **argv) {
   size_t i;
-  char *p;
-  FILE *uudmap_out;
-
-  if (argc < 2 || argv[1][0] == '\0') {
-    fprintf(stderr, "Usage: %s uudemap.h\n", argv[0]);
-    return 1;
-  }
+  int bits;
 
-  if (!(uudmap_out = fopen(argv[1], "w"))) {
-    fprintf(stderr, "%s: Could not open '%s': %s\n", argv[0], argv[1],
-           strerror(errno));
+  if (argc < 3 || argv[1][0] == '\0' || argv[2][0] == '\0') {
+    fprintf(stderr, "Usage: %s uudemap.h bitcount.h\n", argv[0]);
     return 1;
   }
 
@@ -39,28 +69,21 @@ int main(int argc, char **argv) {
    */
   PL_uudmap[(U8)' '] = 0;
 
-  i = sizeof(PL_uudmap);
-  p = PL_uudmap;
-
-  fputs("{\n    ", uudmap_out);
-  while (i--) {
-    fprintf(uudmap_out, "%d", *p);
-    p++;
-    if (i) {
-      fputs(", ", uudmap_out);
-      if (!(i & 15)) {
-       fputs("\n    ", uudmap_out);
-      }
-    }
-  }
-  fputs("\n}\n", uudmap_out);
+  output_block_to_file(argv[0], argv[1], PL_uudmap, sizeof(PL_uudmap));
 
-  if (fclose(uudmap_out)) {
-    fprintf(stderr, "%s: Could not close '%s': %s\n", argv[0], argv[1],
-           strerror(errno));
-    return 1;
+  for (bits = 1; bits < 256; bits++) {
+    if (bits & 1)      PL_bitcount[bits]++;
+    if (bits & 2)      PL_bitcount[bits]++;
+    if (bits & 4)      PL_bitcount[bits]++;
+    if (bits & 8)      PL_bitcount[bits]++;
+    if (bits & 16)     PL_bitcount[bits]++;
+    if (bits & 32)     PL_bitcount[bits]++;
+    if (bits & 64)     PL_bitcount[bits]++;
+    if (bits & 128)    PL_bitcount[bits]++;
   }
 
+  output_block_to_file(argv[0], argv[2], PL_bitcount, sizeof(PL_bitcount));
+
   return 0;
 }