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 <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. FILE_RCSID("@(#)$Id: print.c,v 1.26 1998/06/27 13:57:23 christos Exp $")
  41. #endif /* lint */
  42. #define SZOF(a) (sizeof(a) / sizeof(a[0]))
  43. void
  44. mdump(m)
  45. struct magic *m;
  46. {
  47. static const char *typ[] = { "invalid", "byte", "short", "invalid",
  48. "long", "string", "date", "beshort",
  49. "belong", "bedate", "leshort", "lelong",
  50. "ledate" };
  51. (void) fputc('[', stderr);
  52. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  53. m->offset);
  54. if (m->flag & INDIR)
  55. (void) fprintf(stderr, "(%s,%d),",
  56. (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
  57. typ[(unsigned char) m->in.type] :
  58. "*bad*",
  59. m->in.offset);
  60. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  61. (m->type >= 0 && m->type < SZOF(typ)) ?
  62. typ[(unsigned char) m->type] :
  63. "*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. #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. }