print.c 4.4 KB

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