home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / file39a.zip / src / print.c < prev    next >
C/C++ Source or Header  |  1993-07-30  |  4KB  |  186 lines

  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.  
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #if __STDC__
  32. # include <stdarg.h>
  33. #else
  34. # include <varargs.h>
  35. #endif
  36. #include <stdlib.h>
  37. #ifndef MSC
  38. #include <unistd.h>
  39. #endif
  40. #include "file.h"
  41.  
  42. #ifndef lint
  43. static char *moduleid =
  44.     "@(#)$Id: print.c,v 1.18 93/02/19 14:22:47 ian Exp $";
  45. #endif  /* lint */
  46.  
  47. void
  48. mdump(m)
  49. struct magic *m;
  50. {
  51.     static char *offs[] = {  "absolute", "offset", 
  52.                  "indirect", "indirect-offset" };
  53.     static char *typ[] = {   "invalid", "byte", "short", "invalid",
  54.                  "long", "string", "date", "beshort",
  55.                  "belong", "bedate", "leshort", "lelong",
  56.                  "ledate" };
  57. #ifdef MSC
  58.     (void) fprintf(stderr, "[%s,%ld,%s,%s%c,",
  59. #else
  60.     (void) fprintf(stderr, "[%s,%d,%s,%s%c,",
  61. #endif
  62.         (m->flag >= 0 && m->flag < 4 ? offs[m->flag]: "*bad*"),
  63.         m->offset,
  64. #ifdef OS2         /* bugfix, not OS/2 specific */
  65.         (m->type >= 0 && m->type < sizeof(typ)/sizeof(typ[0]) ?
  66. #else
  67.         (m->type >= 0 && m->type < 7 ?
  68. #endif
  69.                 typ[(unsigned char) m->type] : "*bad*"),
  70.         m->reln & MASK ? "&" : "",
  71.         m->reln & ~MASK);
  72.  
  73.         if (m->flag & INDIR)
  74. #ifdef MSC
  75.         (void) fprintf(stderr, "(%s,%ld)",
  76. #else
  77.         (void) fprintf(stderr, "(%s,%d)",
  78. #endif
  79.         (m->in.type >= 0 && 
  80.         m->in.type < 6 ? typ[(unsigned char) m->in.type] : "*bad*"),
  81.         m->in.offset);
  82.  
  83.     if (m->type == STRING)
  84.         showstr(m->value.s);
  85.     else
  86.         (void) fprintf(stderr, "%d",m->value.l);
  87.     (void) fprintf(stderr, ",%s", m->desc);
  88.     (void) fputs("]\n", stderr);
  89. }
  90.  
  91. /*
  92.  * ckfputs - futs, but with error checking
  93.  * ckfprintf - fprintf, but with error checking
  94.  */
  95. void
  96. ckfputs(str, fil)     
  97.     const char *str;
  98.     FILE *fil;
  99. {
  100.     if (fputs(str,fil) == EOF)
  101.         error("write failed.\n");
  102. }
  103.  
  104. /*VARARGS*/
  105. void
  106. #if __STDC__
  107. ckfprintf(FILE *f, const char *fmt, ...)
  108. #else
  109. ckfprintf(va_alist)
  110.     va_dcl
  111. #endif
  112. {
  113.     va_list va;
  114. #if __STDC__
  115.     va_start(va, fmt);
  116. #else
  117.     FILE *f;
  118.     const char *fmt;
  119.     va_start(va);
  120.     f = va_arg(va, FILE *);
  121.     fmt = va_arg(va, const char *);
  122. #endif
  123.     (void) vfprintf(f, fmt, va);
  124.     if (ferror(f))
  125.         error("write failed.\n");
  126.     va_end(va);
  127. }
  128.  
  129. /*
  130.  * error - print best error message possible and exit
  131.  */
  132. /*VARARGS*/
  133. void
  134. #if __STDC__
  135. error(const char *f, ...)
  136. #else
  137. error(va_alist)
  138.     va_dcl
  139. #endif
  140. {
  141.     va_list va;
  142. #if __STDC__
  143.     va_start(va, f);
  144. #else
  145.     const char *f;
  146.     va_start(va);
  147.     f = va_arg(va, const char *);
  148. #endif
  149.     /* cuz we use stdout for most, stderr here */
  150.     (void) fflush(stdout); 
  151.  
  152.     if (progname != NULL) 
  153.         (void) fprintf(stderr, "%s: ", progname);
  154.     (void) vfprintf(stderr, f, va);
  155.     va_end(va);
  156.     exit(1);
  157. }
  158.  
  159. /*VARARGS*/
  160. void
  161. #if __STDC__
  162. magwarn(const char *f, ...)
  163. #else
  164. magwarn(va_alist)
  165.     va_dcl
  166. #endif
  167. {
  168.     va_list va;
  169. #if __STDC__
  170.     va_start(va, f);
  171. #else
  172.     const char *f;
  173.     va_start(va);
  174.     f = va_arg(va, const char *);
  175. #endif
  176.     /* cuz we use stdout for most, stderr here */
  177.     (void) fflush(stdout); 
  178.  
  179.     if (progname != NULL) 
  180.         (void) fprintf(stderr, "%s: %s, %d: ", 
  181.                    progname, magicfile, lineno);
  182.     (void) vfprintf(stderr, f, va);
  183.     va_end(va);
  184.     fputc('\n', stderr);
  185. }
  186.