file_cmds-60.tar.gz
[apple/file_cmds.git] / file / print.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* $OpenBSD: print.c,v 1.3 1997/02/09 23:58:32 millert Exp $ */
25
26 /*
27 * print.c - debugging printout routines
28 *
29 * Copyright (c) Ian F. Darwin, 1987.
30 * Written by Ian F. Darwin.
31 *
32 * This software is not subject to any license of the American Telephone
33 * and Telegraph Company or of the Regents of the University of California.
34 *
35 * Permission is granted to anyone to use this software for any purpose on
36 * any computer system, and to alter it and redistribute it freely, subject
37 * to the following restrictions:
38 *
39 * 1. The author is not responsible for the consequences of use of this
40 * software, no matter how awful, even if they arise from flaws in it.
41 *
42 * 2. The origin of this software must not be misrepresented, either by
43 * explicit claim or by omission. Since few users ever read sources,
44 * credits must appear in the documentation.
45 *
46 * 3. Altered versions must be plainly marked as such, and must not be
47 * misrepresented as being the original software. Since few users
48 * ever read sources, credits must appear in the documentation.
49 *
50 * 4. This notice may not be removed or altered.
51 */
52
53 #include <stdio.h>
54 #include <errno.h>
55 #include <string.h>
56 #if __STDC__
57 # include <stdarg.h>
58 #else
59 # include <varargs.h>
60 #endif
61 #include <stdlib.h>
62 #include <unistd.h>
63 #include <time.h>
64 #include "file.h"
65
66 #ifndef lint
67 #if 0
68 static char *moduleid = "$OpenBSD: print.c,v 1.3 1997/02/09 23:58:32 millert Exp $";
69 #endif
70 #endif /* lint */
71
72 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
73
74 void
75 mdump(m)
76 struct magic *m;
77 {
78 static char *typ[] = { "invalid", "byte", "short", "invalid",
79 "long", "string", "date", "beshort",
80 "belong", "bedate", "leshort", "lelong",
81 "ledate" };
82 (void) fputc('[', stderr);
83 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
84 m->offset);
85
86 if (m->flag & INDIR)
87 (void) fprintf(stderr, "(%s,%d),",
88 (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
89 typ[(unsigned char) m->in.type] :
90 "*bad*",
91 m->in.offset);
92
93 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
94 (m->type >= 0 && m->type < SZOF(typ)) ?
95 typ[(unsigned char) m->type] :
96 "*bad*");
97 if (m->mask != ~0L)
98 (void) fprintf(stderr, " & %.8x", m->mask);
99
100 (void) fprintf(stderr, ",%c", m->reln);
101
102 if (m->reln != 'x') {
103 switch (m->type) {
104 case BYTE:
105 case SHORT:
106 case LONG:
107 case LESHORT:
108 case LELONG:
109 case BESHORT:
110 case BELONG:
111 (void) fprintf(stderr, "%d", m->value.l);
112 break;
113 case STRING:
114 showstr(stderr, m->value.s, -1);
115 break;
116 case DATE:
117 case LEDATE:
118 case BEDATE:
119 {
120 char *rt, *pp = ctime((time_t*) &m->value.l);
121 if ((rt = strchr(pp, '\n')) != NULL)
122 *rt = '\0';
123 (void) fprintf(stderr, "%s,", pp);
124 if (rt)
125 *rt = '\n';
126 }
127 break;
128 default:
129 (void) fputs("*bad*", stderr);
130 break;
131 }
132 }
133 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
134 }
135
136 /*
137 * ckfputs - futs, but with error checking
138 * ckfprintf - fprintf, but with error checking
139 */
140 void
141 ckfputs(str, fil)
142 const char *str;
143 FILE *fil;
144 {
145 if (fputs(str,fil) == EOF)
146 error("write failed.\n");
147 }
148
149 /*VARARGS*/
150 void
151 #if __STDC__
152 ckfprintf(FILE *f, const char *fmt, ...)
153 #else
154 ckfprintf(va_alist)
155 va_dcl
156 #endif
157 {
158 va_list va;
159 #if __STDC__
160 va_start(va, fmt);
161 #else
162 FILE *f;
163 const char *fmt;
164 va_start(va);
165 f = va_arg(va, FILE *);
166 fmt = va_arg(va, const char *);
167 #endif
168 (void) vfprintf(f, fmt, va);
169 if (ferror(f))
170 error("write failed.\n");
171 va_end(va);
172 }
173
174 /*
175 * error - print best error message possible and exit
176 */
177 /*VARARGS*/
178 void
179 #if __STDC__
180 error(const char *f, ...)
181 #else
182 error(va_alist)
183 va_dcl
184 #endif
185 {
186 va_list va;
187 #if __STDC__
188 va_start(va, f);
189 #else
190 const char *f;
191 va_start(va);
192 f = va_arg(va, const char *);
193 #endif
194 /* cuz we use stdout for most, stderr here */
195 (void) fflush(stdout);
196
197 if (progname != NULL)
198 (void) fprintf(stderr, "%s: ", progname);
199 (void) vfprintf(stderr, f, va);
200 va_end(va);
201 exit(1);
202 }
203
204 /*VARARGS*/
205 void
206 #if __STDC__
207 magwarn(const char *f, ...)
208 #else
209 magwarn(va_alist)
210 va_dcl
211 #endif
212 {
213 va_list va;
214 #if __STDC__
215 va_start(va, f);
216 #else
217 const char *f;
218 va_start(va);
219 f = va_arg(va, const char *);
220 #endif
221 /* cuz we use stdout for most, stderr here */
222 (void) fflush(stdout);
223
224 if (progname != NULL)
225 (void) fprintf(stderr, "%s: %s, %d: ",
226 progname, magicfile, lineno);
227 (void) vfprintf(stderr, f, va);
228 va_end(va);
229 fputc('\n', stderr);
230 }