summaryrefslogtreecommitdiff
path: root/generate_uudmap.c
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2009-05-20 07:53:20 +0200
committerNicholas Clark <nick@ccl4.org>2009-05-20 07:56:39 +0200
commit869053c868a03539389422a7a28502818825a940 (patch)
treeb56a4f124c3037b97d90724903afd939197ca0cf /generate_uudmap.c
parent79f8582810dc6de265f284db348dc30d7d359e3f (diff)
downloadperl-869053c868a03539389422a7a28502818825a940.tar.gz
Break out the file output code from generate_uudmap's main() to allow re-use.
Diffstat (limited to 'generate_uudmap.c')
-rw-r--r--generate_uudmap.c61
1 files changed, 32 insertions, 29 deletions
diff --git a/generate_uudmap.c b/generate_uudmap.c
index 31aba75d86..27b142a64c 100644
--- a/generate_uudmap.c
+++ b/generate_uudmap.c
@@ -7,6 +7,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[\\]^_";
@@ -17,20 +48,12 @@ static char PL_uudmap[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;
}
- if (!(uudmap_out = fopen(argv[1], "w"))) {
- fprintf(stderr, "%s: Could not open '%s': %s\n", argv[0], argv[1],
- strerror(errno));
- return 1;
- }
-
for (i = 0; i < sizeof(PL_uuemap) - 1; ++i)
PL_uudmap[(U8)PL_uuemap[i]] = (char)i;
/*
@@ -39,27 +62,7 @@ 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);
-
- if (fclose(uudmap_out)) {
- fprintf(stderr, "%s: Could not close '%s': %s\n", argv[0], argv[1],
- strerror(errno));
- return 1;
- }
+ output_block_to_file(argv[0], argv[1], PL_uudmap, sizeof(PL_uudmap));
return 0;
}