print.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.34 2001/08/07 16:01:26 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", "pstring", "ldate", "beldate",
  53. "leldate" };
  54. static const char optyp[] = { '@', '&', '|', '^', '+', '-',
  55. '*', '/', '%' };
  56. (void) fputc('[', stderr);
  57. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  58. m->offset);
  59. if (m->flag & INDIR) {
  60. (void) fprintf(stderr, "(%s,",
  61. /* Note: type is unsigned */
  62. (m->in_type < SZOF(typ)) ?
  63. typ[m->in_type] : "*bad*");
  64. if (m->in_op & OPINVERSE)
  65. (void) fputc('~', stderr);
  66. (void) fprintf(stderr, "%c%d),",
  67. ((m->in_op&0x7F) < SZOF(optyp)) ?
  68. optyp[m->in_op&0x7F] : '?',
  69. m->in_offset);
  70. }
  71. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  72. /* Note: type is unsigned */
  73. (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
  74. if (m->mask_op & OPINVERSE)
  75. (void) fputc('~', stderr);
  76. if (m->mask) {
  77. ((m->mask_op&0x7F) < SZOF(optyp)) ?
  78. (void) fputc(optyp[m->mask_op&0x7F], stderr) :
  79. (void) fputc('?', stderr);
  80. if(STRING != m->type || PSTRING != m->type)
  81. (void) fprintf(stderr, "%.8x", m->mask);
  82. else {
  83. if (m->mask & STRING_IGNORE_LOWERCASE)
  84. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  85. if (m->mask & STRING_COMPACT_BLANK)
  86. (void) fputc(CHAR_COMPACT_BLANK, stderr);
  87. if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
  88. (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
  89. stderr);
  90. }
  91. }
  92. (void) fprintf(stderr, ",%c", m->reln);
  93. if (m->reln != 'x') {
  94. switch (m->type) {
  95. case BYTE:
  96. case SHORT:
  97. case LONG:
  98. case LESHORT:
  99. case LELONG:
  100. case BESHORT:
  101. case BELONG:
  102. (void) fprintf(stderr, "%d", m->value.l);
  103. break;
  104. case STRING:
  105. case PSTRING:
  106. showstr(stderr, m->value.s, -1);
  107. break;
  108. case DATE:
  109. case LEDATE:
  110. case BEDATE:
  111. (void)fprintf(stderr, "%s,", fmttime(m->value.l, 1));
  112. break;
  113. case LDATE:
  114. case LELDATE:
  115. case BELDATE:
  116. (void)fprintf(stderr, "%s,", fmttime(m->value.l, 0));
  117. break;
  118. default:
  119. (void) fputs("*bad*", stderr);
  120. break;
  121. }
  122. }
  123. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  124. }
  125. /*
  126. * ckfputs - fputs, but with error checking
  127. * ckfprintf - fprintf, but with error checking
  128. */
  129. void
  130. ckfputs(str, fil)
  131. const char *str;
  132. FILE *fil;
  133. {
  134. if (fputs(str,fil) == EOF)
  135. error("write failed.\n");
  136. }
  137. /*VARARGS*/
  138. void
  139. #ifdef __STDC__
  140. ckfprintf(FILE *f, const char *fmt, ...)
  141. #else
  142. ckfprintf(va_alist)
  143. va_dcl
  144. #endif
  145. {
  146. va_list va;
  147. #ifdef __STDC__
  148. va_start(va, fmt);
  149. #else
  150. FILE *f;
  151. const char *fmt;
  152. va_start(va);
  153. f = va_arg(va, FILE *);
  154. fmt = va_arg(va, const char *);
  155. #endif
  156. (void) vfprintf(f, fmt, va);
  157. if (ferror(f))
  158. error("write failed.\n");
  159. va_end(va);
  160. }
  161. /*
  162. * error - print best error message possible and exit
  163. */
  164. /*VARARGS*/
  165. void
  166. #ifdef __STDC__
  167. error(const char *f, ...)
  168. #else
  169. error(va_alist)
  170. va_dcl
  171. #endif
  172. {
  173. va_list va;
  174. #ifdef __STDC__
  175. va_start(va, f);
  176. #else
  177. const char *f;
  178. va_start(va);
  179. f = va_arg(va, const char *);
  180. #endif
  181. /* cuz we use stdout for most, stderr here */
  182. (void) fflush(stdout);
  183. if (progname != NULL)
  184. (void) fprintf(stderr, "%s: ", progname);
  185. (void) vfprintf(stderr, f, va);
  186. va_end(va);
  187. exit(1);
  188. }
  189. /*VARARGS*/
  190. void
  191. #ifdef __STDC__
  192. magwarn(const char *f, ...)
  193. #else
  194. magwarn(va_alist)
  195. va_dcl
  196. #endif
  197. {
  198. va_list va;
  199. #ifdef __STDC__
  200. va_start(va, f);
  201. #else
  202. const char *f;
  203. va_start(va);
  204. f = va_arg(va, const char *);
  205. #endif
  206. /* cuz we use stdout for most, stderr here */
  207. (void) fflush(stdout);
  208. if (progname != NULL)
  209. (void) fprintf(stderr, "%s: %s, %d: ",
  210. progname, magicfile, lineno);
  211. (void) vfprintf(stderr, f, va);
  212. va_end(va);
  213. fputc('\n', stderr);
  214. }
  215. char *
  216. fmttime(v, local)
  217. long v;
  218. int local;
  219. {
  220. char *pp, *rt;
  221. time_t t = (time_t)v;
  222. struct tm *tm;
  223. if (local) {
  224. pp = ctime(&t);
  225. } else {
  226. #ifndef HAVE_DAYLIGHT
  227. static int daylight = 0;
  228. #ifdef HAVE_TM_ISDST
  229. static time_t now = (time_t)0;
  230. if (now == (time_t)0) {
  231. struct tm *tm1;
  232. (void)time(&now);
  233. tm1 = localtime(&now);
  234. daylight = tm1->tm_isdst;
  235. }
  236. #endif /* HAVE_TM_ISDST */
  237. #endif /* HAVE_DAYLIGHT */
  238. if (daylight)
  239. t += 3600;
  240. tm = gmtime(&t);
  241. pp = asctime(tm);
  242. }
  243. if ((rt = strchr(pp, '\n')) != NULL)
  244. *rt = '\0';
  245. return pp;
  246. }