print.c 4.4 KB

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