diff options
Diffstat (limited to 'contrib/file/print.c')
-rw-r--r-- | contrib/file/print.c | 273 |
1 files changed, 149 insertions, 124 deletions
diff --git a/contrib/file/print.c b/contrib/file/print.c index e7c8098d699e1..7992f5c578f7e 100644 --- a/contrib/file/print.c +++ b/contrib/file/print.c @@ -1,39 +1,39 @@ /* - * Copyright (c) Ian F. Darwin 1986-1995. - * Software written by Ian F. Darwin and others; - * maintained 1995-present by Christos Zoulas and others. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice immediately at the beginning of the file, without modification, - * this list of conditions, and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -/* * print.c - debugging printout routines + * + * Copyright (c) Ian F. Darwin, 1987. + * Written by Ian F. Darwin. + * + * This software is not subject to any license of the American Telephone + * and Telegraph Company or of the Regents of the University of California. + * + * Permission is granted to anyone to use this software for any purpose on + * any computer system, and to alter it and redistribute it freely, subject + * to the following restrictions: + * + * 1. The author is not responsible for the consequences of use of this + * software, no matter how awful, even if they arise from flaws in it. + * + * 2. The origin of this software must not be misrepresented, either by + * explicit claim or by omission. Since few users ever read sources, + * credits must appear in the documentation. + * + * 3. Altered versions must be plainly marked as such, and must not be + * misrepresented as being the original software. Since few users + * ever read sources, credits must appear in the documentation. + * + * 4. This notice may not be removed or altered. */ #include "file.h" #include <stdio.h> #include <errno.h> #include <string.h> -#include <stdarg.h> +#ifdef __STDC__ +# include <stdarg.h> +#else +# include <varargs.h> +#endif #include <stdlib.h> #ifdef HAVE_UNISTD_H #include <unistd.h> @@ -41,47 +41,38 @@ #include <time.h> #ifndef lint -FILE_RCSID("@(#)$Id: print.c,v 1.50 2006/03/02 22:07:53 christos Exp $") +FILE_RCSID("@(#)$Id: print.c,v 1.31 2000/08/05 17:36:49 christos Exp $") #endif /* lint */ #define SZOF(a) (sizeof(a) / sizeof(a[0])) -#ifndef COMPILE_ONLY -protected void -file_mdump(struct magic *m) +void +mdump(m) + struct magic *m; { - private const char *typ[] = { FILE_FORMAT_NAME }; - private const char optyp[] = { FILE_OPS }; - + static const char *typ[] = { "invalid", "byte", "short", "invalid", + "long", "string", "date", "beshort", + "belong", "bedate", "leshort", "lelong", + "ledate" }; (void) fputc('[', stderr); (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7), m->offset); - if (m->flag & INDIR) { - (void) fprintf(stderr, "(%s,", - /* Note: type is unsigned */ - (m->in_type < SZOF(typ)) ? - typ[m->in_type] : "*bad*"); - if (m->in_op & FILE_OPINVERSE) - (void) fputc('~', stderr); - (void) fprintf(stderr, "%c%d),", - ((m->in_op&0x7F) < SZOF(optyp)) ? - optyp[m->in_op&0x7F] : '?', - m->in_offset); - } + if (m->flag & INDIR) + (void) fprintf(stderr, "(%s,%d),", + /* Note: in.type is unsigned */ + (m->in.type < SZOF(typ)) ? + typ[m->in.type] : "*bad*", + m->in.offset); + (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "", /* Note: type is unsigned */ (m->type < SZOF(typ)) ? typ[m->type] : "*bad*"); - if (m->mask_op & FILE_OPINVERSE) - (void) fputc('~', stderr); - if (m->mask) { - if ((m->mask_op & 0x7F) < SZOF(optyp)) - fputc(optyp[m->mask_op&0x7F], stderr); - else - fputc('?', stderr); - if (FILE_STRING != m->type || FILE_PSTRING != m->type) - (void) fprintf(stderr, "%.8x", m->mask); + if (m->mask != ~((uint32)0)) { + if(STRING != m->type) + (void) fprintf(stderr, " & %.8x", m->mask); else { + (void) fputc('/', stderr); if (m->mask & STRING_IGNORE_LOWERCASE) (void) fputc(CHAR_IGNORE_LOWERCASE, stderr); if (m->mask & STRING_COMPACT_BLANK) @@ -96,37 +87,31 @@ file_mdump(struct magic *m) if (m->reln != 'x') { switch (m->type) { - case FILE_BYTE: - case FILE_SHORT: - case FILE_LONG: - case FILE_LESHORT: - case FILE_LELONG: - case FILE_MELONG: - case FILE_BESHORT: - case FILE_BELONG: + case BYTE: + case SHORT: + case LONG: + case LESHORT: + case LELONG: + case BESHORT: + case BELONG: (void) fprintf(stderr, "%d", m->value.l); break; - case FILE_PSTRING: - case FILE_STRING: - case FILE_REGEX: - case FILE_BESTRING16: - case FILE_LESTRING16: - case FILE_SEARCH: - file_showstr(stderr, m->value.s, m->vallen); - break; - case FILE_DATE: - case FILE_LEDATE: - case FILE_BEDATE: - case FILE_MEDATE: - (void)fprintf(stderr, "%s,", - file_fmttime(m->value.l, 1)); + case STRING: + showstr(stderr, m->value.s, -1); break; - case FILE_LDATE: - case FILE_LELDATE: - case FILE_BELDATE: - case FILE_MELDATE: - (void)fprintf(stderr, "%s,", - file_fmttime(m->value.l, 0)); + case DATE: + case LEDATE: + case BEDATE: + { + time_t t = m->value.l; + char *rt, *pp = ctime(&t); + + if ((rt = strchr(pp, '\n')) != NULL) + *rt = '\0'; + (void) fprintf(stderr, "%s,", pp); + if (rt) + *rt = '\n'; + } break; default: (void) fputs("*bad*", stderr); @@ -135,59 +120,99 @@ file_mdump(struct magic *m) } (void) fprintf(stderr, ",\"%s\"]\n", m->desc); } + +/* + * ckfputs - futs, but with error checking + * ckfprintf - fprintf, but with error checking + */ +void +ckfputs(str, fil) + const char *str; + FILE *fil; +{ + if (fputs(str,fil) == EOF) + error("write failed.\n"); +} + +/*VARARGS*/ +void +#ifdef __STDC__ +ckfprintf(FILE *f, const char *fmt, ...) +#else +ckfprintf(va_alist) + va_dcl #endif +{ + va_list va; +#ifdef __STDC__ + va_start(va, fmt); +#else + FILE *f; + const char *fmt; + va_start(va); + f = va_arg(va, FILE *); + fmt = va_arg(va, const char *); +#endif + (void) vfprintf(f, fmt, va); + if (ferror(f)) + error("write failed.\n"); + va_end(va); +} +/* + * error - print best error message possible and exit + */ /*VARARGS*/ -protected void -file_magwarn(struct magic_set *ms, const char *f, ...) +void +#ifdef __STDC__ +error(const char *f, ...) +#else +error(va_alist) + va_dcl +#endif { va_list va; +#ifdef __STDC__ va_start(va, f); - +#else + const char *f; + va_start(va); + f = va_arg(va, const char *); +#endif /* cuz we use stdout for most, stderr here */ (void) fflush(stdout); - (void) fprintf(stderr, "%s, %lu: Warning ", ms->file, - (unsigned long)ms->line); + if (progname != NULL) + (void) fprintf(stderr, "%s: ", progname); (void) vfprintf(stderr, f, va); va_end(va); - fputc('\n', stderr); + exit(1); } -protected const char * -file_fmttime(uint32_t v, int local) +/*VARARGS*/ +void +#ifdef __STDC__ +magwarn(const char *f, ...) +#else +magwarn(va_alist) + va_dcl +#endif { - char *pp, *rt; - time_t t = (time_t)v; - struct tm *tm; - - if (local) { - pp = ctime(&t); - } else { -#ifndef HAVE_DAYLIGHT - private int daylight = 0; -#ifdef HAVE_TM_ISDST - private time_t now = (time_t)0; - - if (now == (time_t)0) { - struct tm *tm1; - (void)time(&now); - tm1 = localtime(&now); - if (tm1 == NULL) - return "*Invalid time*"; - daylight = tm1->tm_isdst; - } -#endif /* HAVE_TM_ISDST */ -#endif /* HAVE_DAYLIGHT */ - if (daylight) - t += 3600; - tm = gmtime(&t); - if (tm == NULL) - return "*Invalid time*"; - pp = asctime(tm); - } + va_list va; +#ifdef __STDC__ + va_start(va, f); +#else + const char *f; + va_start(va); + f = va_arg(va, const char *); +#endif + /* cuz we use stdout for most, stderr here */ + (void) fflush(stdout); - if ((rt = strchr(pp, '\n')) != NULL) - *rt = '\0'; - return pp; + if (progname != NULL) + (void) fprintf(stderr, "%s: %s, %d: ", + progname, magicfile, lineno); + (void) vfprintf(stderr, f, va); + va_end(va); + fputc('\n', stderr); } |