From a9cee145d7b7d91eb5342a2ab117e32e4fbf3f92 Mon Sep 17 00:00:00 2001 From: hjp Date: Fri, 30 Aug 1996 12:25:22 +0000 Subject: [PATCH] Initial release. --- fix2var/Makefile | 9 +++++ fix2var/fix2var.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 fix2var/Makefile create mode 100644 fix2var/fix2var.c diff --git a/fix2var/Makefile b/fix2var/Makefile new file mode 100644 index 0000000..80edcf6 --- /dev/null +++ b/fix2var/Makefile @@ -0,0 +1,9 @@ +BINDIR=/usr/local/bin +CC = gcc +CFLAGS = -Wall -ansi -pedantic -O2 -g +fix2var: fix2var.o + $(CC) $^ -lant -o $@ + +install: $(BINDIR)/fix2var +$(BINDIR)/%: % + install $< $@ diff --git a/fix2var/fix2var.c b/fix2var/fix2var.c new file mode 100644 index 0000000..2b7acb0 --- /dev/null +++ b/fix2var/fix2var.c @@ -0,0 +1,94 @@ +char fix2var_c_rcs_id[] = + "$Id: fix2var.c,v 1.1 1996-08-30 12:25:22 hjp Exp $"; +/* + * fix2var - convert fixed length record data to line oriented format + * + * This program simply copies fixed length chunks from a file to + * stdout. Each chunk is terminated with a '\n' character. + * Optionally trailing white space can be stripped. + * + * $Log: fix2var.c,v $ + * Revision 1.1 1996-08-30 12:25:22 hjp + * Initial release. + * + */ +#include +#include +#include +#include +#include +#include + +char *cmnd; + +unsigned long width = 80; +int strip = 1; + +void usage(void) { + fprintf(stderr, "Usage: %s [-l width] [file ...]\n", cmnd); + exit(1); +} + + +void fix2var(const char *filename) { + FILE *fp; + char *buf = emalloc(width+1); + size_t rc; + int i; + + if (strcmp(filename, "-") == 0) { + fp = stdin; + } else { + fp = efopen(filename, "r"); + } + while ((rc = fread (buf, 1, width, fp)) != 0 && (rc != (size_t)-1)) { + if (rc < width) { + fprintf(stderr, "%s: warning: short record (%lu bytes)" + " encountered\n", cmnd, (unsigned long)rc); + } + assert (rc <= INT_MAX); + if (strip) { + for (i = rc - 1; i >= 0 && isspace(buf[i]); i--); + buf[i+1] = '\0'; + } else { + buf[rc] = '\0'; + } + puts(buf); + } + + fclose(fp); + free(buf); +} + + +int main(int argc, char **argv) { + int c; + int i; + char *p; + + cmnd = argv[0]; + + while ((c = getopt(argc, argv, "sw:")) != EOF) { + switch (c) { + case 's': + strip= 1; + break; + case 'w': + width= strtoul(optarg, &p, 0); + if (width == 0 || *p) usage(); + break; + case '?': + usage(); + default: + assert(0); + } + } + if (optind == argc) { + fix2var("-"); + } else { + for (i = optind; i < argc; i++) { + fix2var(argv[i]); + } + } + return 0; +}