print.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include <stdio.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #if __STDC__
  31. # include <stdarg.h>
  32. #else
  33. # include <varargs.h>
  34. #endif
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <time.h>
  38. #include "file.h"
  39. #ifndef lint
  40. static char *moduleid =
  41. "@(#)$Id: print.c,v 1.22 1995/05/20 22:09:21 christos Exp $";
  42. #endif /* lint */
  43. #define SZOF(a) (sizeof(a) / sizeof(a[0]))
  44. void
  45. mdump(m)
  46. struct magic *m;
  47. {
  48. static char *typ[] = { "invalid", "byte", "short", "invalid",
  49. "long", "string", "date", "beshort",
  50. "belong", "bedate", "leshort", "lelong",
  51. "ledate" };
  52. (void) fputc('[', stderr);
  53. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  54. m->offset);
  55. if (m->flag & INDIR)
  56. (void) fprintf(stderr, "(%s,%ld),",
  57. (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
  58. typ[(unsigned char) m->in.type] :
  59. "*bad*",
  60. m->in.offset);
  61. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  62. (m->type >= 0 && m->type < SZOF(typ)) ?
  63. typ[(unsigned char) m->type] :
  64. "*bad*");
  65. if (m->mask != ~0L)
  66. (void) fprintf(stderr, " & %.8lx", m->mask);
  67. (void) fprintf(stderr, ",%c", m->reln);
  68. if (m->reln != 'x') {
  69. switch (m->type) {
  70. case BYTE:
  71. case SHORT:
  72. case LONG:
  73. case LESHORT:
  74. case LELONG:
  75. case BESHORT:
  76. case BELONG:
  77. (void) fprintf(stderr, "%ld", m->value.l);
  78. break;
  79. case STRING:
  80. showstr(stderr, m->value.s, -1);
  81. break;
  82. case DATE:
  83. case LEDATE:
  84. case BEDATE:
  85. {
  86. char *rt, *pp = ctime((time_t*) &m->value.l);
  87. if ((rt = strchr(pp, '\n')) != NULL)
  88. *rt = '\0';
  89. (void) fprintf(stderr, "%s,", pp);
  90. if (rt)
  91. *rt = '\n';
  92. }
  93. break;
  94. default:
  95. (void) fputs("*bad*", stderr);
  96. break;
  97. }
  98. }
  99. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  100. }
  101. /*
  102. * ckfputs - futs, but with error checking
  103. * ckfprintf - fprintf, but with error checking
  104. */
  105. void
  106. ckfputs(str, fil)
  107. const char *str;
  108. FILE *fil;
  109. {
  110. if (fputs(str,fil) == EOF)
  111. error("write failed.\n");
  112. }
  113. /*VARARGS*/
  114. void
  115. #if __STDC__
  116. ckfprintf(FILE *f, const char *fmt, ...)
  117. #else
  118. ckfprintf(va_alist)
  119. va_dcl
  120. #endif
  121. {
  122. va_list va;
  123. #if __STDC__
  124. va_start(va, fmt);
  125. #else
  126. FILE *f;
  127. const char *fmt;
  128. va_start(va);
  129. f = va_arg(va, FILE *);
  130. fmt = va_arg(va, const char *);
  131. #endif
  132. (void) vfprintf(f, fmt, va);
  133. if (ferror(f))
  134. error("write failed.\n");
  135. va_end(va);
  136. }
  137. /*
  138. * error - print best error message possible and exit
  139. */
  140. /*VARARGS*/
  141. void
  142. #if __STDC__
  143. error(const char *f, ...)
  144. #else
  145. error(va_alist)
  146. va_dcl
  147. #endif
  148. {
  149. va_list va;
  150. #if __STDC__
  151. va_start(va, f);
  152. #else
  153. const char *f;
  154. va_start(va);
  155. f = va_arg(va, const char *);
  156. #endif
  157. /* cuz we use stdout for most, stderr here */
  158. (void) fflush(stdout);
  159. if (progname != NULL)
  160. (void) fprintf(stderr, "%s: ", progname);
  161. (void) vfprintf(stderr, f, va);
  162. va_end(va);
  163. exit(1);
  164. }
  165. /*VARARGS*/
  166. void
  167. #if __STDC__
  168. magwarn(const char *f, ...)
  169. #else
  170. magwarn(va_alist)
  171. va_dcl
  172. #endif
  173. {
  174. va_list va;
  175. #if __STDC__
  176. va_start(va, f);
  177. #else
  178. const char *f;
  179. va_start(va);
  180. f = va_arg(va, const char *);
  181. #endif
  182. /* cuz we use stdout for most, stderr here */
  183. (void) fflush(stdout);
  184. if (progname != NULL)
  185. (void) fprintf(stderr, "%s: %s, %d: ",
  186. progname, magicfile, lineno);
  187. (void) vfprintf(stderr, f, va);
  188. va_end(va);
  189. fputc('\n', stderr);
  190. }