This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / usr.bin / file / print.c
CommitLineData
15637ed4
RG
1/*
2 * print.c - debugging printout routines
3 *
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
6 *
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
9 *
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
13 *
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
16 *
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
20 *
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits must appear in the documentation.
24 *
25 * 4. This notice may not be removed or altered.
26 */
27
28#include <stdio.h>
29#include <errno.h>
78ed81a3 30#include <string.h>
31#if __STDC__
32# include <stdarg.h>
33#else
34# include <varargs.h>
35#endif
36#include <stdlib.h>
37#include <unistd.h>
15637ed4
RG
38#include "file.h"
39
78ed81a3 40#ifndef lint
41static char *moduleid =
42 "@(#)print.c,v 1.2 1993/06/10 00:38:17 jtc Exp";
43#endif /* lint */
15637ed4 44
78ed81a3 45void
15637ed4
RG
46mdump(m)
47struct magic *m;
48{
78ed81a3 49 static char *offs[] = { "absolute", "offset",
50 "indirect", "indirect-offset" };
51 static char *typ[] = { "invalid", "byte", "short", "invalid",
52 "long", "string", "date", "beshort",
53 "belong", "bedate", "leshort", "lelong",
54 "ledate" };
55 (void) fprintf(stderr, "[%s,%d,%s,%s%c,",
56 (m->flag >= 0 && m->flag < 4 ? offs[m->flag]: "*bad*"),
15637ed4 57 m->offset,
78ed81a3 58 (m->type >= 0 && m->type < 13 ?
59 typ[(unsigned char) m->type] : "*bad*"),
60 m->reln & MASK ? "&" : "",
61 m->reln & ~MASK);
62 if (m->flag & INDIR)
63 (void) fprintf(stderr, "(%s,%d)",
64 (m->in.type >= 0 &&
65 m->in.type < 6 ? typ[(unsigned char) m->in.type] : "*bad*"),
66 m->in.offset);
67
15637ed4
RG
68 if (m->type == STRING)
69 showstr(m->value.s);
70 else
78ed81a3 71 (void) fprintf(stderr, "%d",m->value.l);
72 (void) fprintf(stderr, ",%s", m->desc);
73 (void) fputs("]\n", stderr);
15637ed4
RG
74}
75
76/*
78ed81a3 77 * ckfputs - futs, but with error checking
78 * ckfprintf - fprintf, but with error checking
15637ed4 79 */
78ed81a3 80void
81ckfputs(str, fil)
82 const char *str;
83 FILE *fil;
84{
85 if (fputs(str,fil) == EOF)
86 error("write failed.\n");
87}
88
15637ed4
RG
89/*VARARGS*/
90void
78ed81a3 91#if __STDC__
92ckfprintf(FILE *f, const char *fmt, ...)
93#else
94ckfprintf(va_alist)
95 va_dcl
96#endif
15637ed4 97{
78ed81a3 98 va_list va;
99#if __STDC__
100 va_start(va, fmt);
101#else
102 FILE *f;
103 const char *fmt;
104 va_start(va);
105 f = va_arg(va, FILE *);
106 fmt = va_arg(va, const char *);
107#endif
108 (void) vfprintf(f, fmt, va);
109 if (ferror(f))
110 error("write failed.\n");
111 va_end(va);
15637ed4
RG
112}
113
78ed81a3 114/*
115 * error - print best error message possible and exit
116 */
15637ed4 117/*VARARGS*/
78ed81a3 118void
119#if __STDC__
120error(const char *f, ...)
121#else
122error(va_alist)
123 va_dcl
124#endif
15637ed4 125{
78ed81a3 126 va_list va;
127#if __STDC__
128 va_start(va, f);
129#else
130 const char *f;
131 va_start(va);
132 f = va_arg(va, const char *);
133#endif
134 /* cuz we use stdout for most, stderr here */
135 (void) fflush(stdout);
15637ed4 136
78ed81a3 137 if (progname != NULL)
138 (void) fprintf(stderr, "%s: ", progname);
139 (void) vfprintf(stderr, f, va);
140 va_end(va);
141 exit(1);
142}
15637ed4 143
78ed81a3 144/*VARARGS*/
145void
146#if __STDC__
147magwarn(const char *f, ...)
148#else
149magwarn(va_alist)
150 va_dcl
151#endif
152{
153 va_list va;
154#if __STDC__
155 va_start(va, f);
156#else
157 const char *f;
158 va_start(va);
159 f = va_arg(va, const char *);
160#endif
15637ed4
RG
161 /* cuz we use stdout for most, stderr here */
162 (void) fflush(stdout);
163
78ed81a3 164 if (progname != NULL)
165 (void) fprintf(stderr, "%s: %s, %d: ",
166 progname, magicfile, lineno);
167 (void) vfprintf(stderr, f, va);
168 va_end(va);
169 fputc('\n', stderr);
15637ed4 170}