print.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <string.h>
  30. #ifdef __STDC__
  31. # include <stdarg.h>
  32. #else
  33. # include <varargs.h>
  34. #endif
  35. #include <stdlib.h>
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #include <time.h>
  40. #ifndef lint
  41. FILE_RCSID("@(#)$Id: print.c,v 1.29 1999/10/31 22:23:04 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 const 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,%d),",
  57. /* Note: in.type is unsigned */
  58. (m->in.type < SZOF(typ)) ?
  59. typ[m->in.type] : "*bad*",
  60. m->in.offset);
  61. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  62. /* Note: type is unsigned */
  63. (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
  64. if (m->mask != ~((uint32)0))
  65. (void) fprintf(stderr, " & %.8x", m->mask);
  66. (void) fprintf(stderr, ",%c", m->reln);
  67. if (m->reln != 'x') {
  68. switch (m->type) {
  69. case BYTE:
  70. case SHORT:
  71. case LONG:
  72. case LESHORT:
  73. case LELONG:
  74. case BESHORT:
  75. case BELONG:
  76. (void) fprintf(stderr, "%d", m->value.l);
  77. break;
  78. case STRING:
  79. showstr(stderr, m->value.s, -1);
  80. break;
  81. case DATE:
  82. case LEDATE:
  83. case BEDATE:
  84. {
  85. time_t t = m->value.l;
  86. char *rt, *pp = ctime(&t);
  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. #ifdef __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. #ifdef __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. #ifdef __STDC__
  143. error(const char *f, ...)
  144. #else
  145. error(va_alist)
  146. va_dcl
  147. #endif
  148. {
  149. va_list va;
  150. #ifdef __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. #ifdef __STDC__
  168. magwarn(const char *f, ...)
  169. #else
  170. magwarn(va_alist)
  171. va_dcl
  172. #endif
  173. {
  174. va_list va;
  175. #ifdef __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. }