From a2935ca47af48571ce6608355eecb95e1b69133a Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Thu, 16 Sep 1993 20:49:29 +0000 Subject: [PATCH] Use calloc() to allocate magic entries to avoid unitialized memory spurious errors. Change showstr to print to a file and the ability to print both counted and null terminated strings --- src/apprentice.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/apprentice.c b/src/apprentice.c index 11eaff1a..561f1787 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -33,7 +33,7 @@ #ifndef lint static char *moduleid = - "@(#)$Id: apprentice.c,v 1.16 1993/02/19 14:22:42 ian Exp $"; + "@(#)$Id: apprentice.c,v 1.17 1993/09/16 20:49:29 christos Exp $"; #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -67,7 +67,7 @@ int check; /* non-zero? checking-only run. */ } maxmagic = MAXMAGIS; - if ((magic = (struct magic *) malloc(sizeof(struct magic) * maxmagic)) + if ((magic = (struct magic *) calloc(sizeof(struct magic), maxmagic)) == NULL) { (void) fprintf(stderr, "%s: Out of memory.\n", progname); if (check) @@ -474,47 +474,57 @@ int c; * Print a string containing C character escapes. */ void -showstr(s) +showstr(fp, s, len) +FILE *fp; const char *s; +int len; { register char c; - while((c = *s++) != '\0') { + for (;;) { + c = *s++; + if (len == -1) { + if (c == '\0') + break; + } + else { + if (len-- == 0) + break; + } if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */ - putchar(c); + (void) fputc(c, fp); else { - putchar('\\'); + (void) fputc('\\', fp); switch (c) { case '\n': - putchar('n'); + (void) fputc('n', fp); break; case '\r': - putchar('r'); + (void) fputc('r', fp); break; case '\b': - putchar('b'); + (void) fputc('b', fp); break; case '\t': - putchar('t'); + (void) fputc('t', fp); break; case '\f': - putchar('f'); + (void) fputc('f', fp); break; case '\v': - putchar('v'); + (void) fputc('v', fp); break; default: - printf("%.3o", c & 0377); + (void) fprintf(fp, "%.3o", c & 0377); break; } } } - putchar('\t'); } -- 2.40.0