print.c 5.6 KB

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