warn.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* $Source: /u/mark/src/pax/RCS/warn.c,v $
  2. *
  3. * $Revision: 1.2 $
  4. *
  5. * warn.c - miscellaneous user warning routines
  6. *
  7. * DESCRIPTION
  8. *
  9. * These routines provide the user with various forms of warning
  10. * and informational messages.
  11. *
  12. * AUTHOR
  13. *
  14. * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  15. *
  16. * Sponsored by The USENIX Association for public distribution.
  17. *
  18. * Copyright (c) 1989 Mark H. Colburn.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms are permitted
  22. * provided that the above copyright notice is duplicated in all such
  23. * forms and that any documentation, advertising materials, and other
  24. * materials related to such distribution and use acknowledge that the
  25. * software was developed * by Mark H. Colburn and sponsored by The
  26. * USENIX Association.
  27. *
  28. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  29. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31. *
  32. * $Log: warn.c,v $
  33. * Revision 1.2 89/02/12 10:06:15 mark
  34. * 1.2 release fixes
  35. *
  36. * Revision 1.1 88/12/23 18:02:40 mark
  37. * Initial revision
  38. *
  39. */
  40. #ifndef lint
  41. static char *ident = "$Id: warn.c,v 1.2 89/02/12 10:06:15 mark Exp $";
  42. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  43. #endif /* ! lint */
  44. /* Headers */
  45. #include "pax.h"
  46. /* Function Prototypes */
  47. #ifdef __STDC__
  48. static void prsize(FILE *, OFFSET);
  49. #else /* !__STDC__ */
  50. static void prsize();
  51. #endif /* __STDC__ */
  52. /* warnarch - print an archive-related warning message and offset
  53. *
  54. * DESCRIPTION
  55. *
  56. * Present the user with an error message and an archive offset at
  57. * which the error occured. This can be useful for diagnosing or
  58. * fixing damaged archives.
  59. *
  60. * PARAMETERS
  61. *
  62. * char *msg - A message string to be printed for the user.
  63. * OFFSET adjust - An adjustment which is added to the current
  64. * archive position to tell the user exactly where
  65. * the error occurred.
  66. */
  67. #ifdef __STDC__
  68. void warnarch(char *msg, OFFSET adjust)
  69. #else
  70. void warnarch(msg, adjust)
  71. char *msg;
  72. OFFSET adjust;
  73. #endif
  74. {
  75. fprintf(stderr, "%s: [offset ", myname);
  76. prsize(stderr, total - adjust);
  77. fprintf(stderr, "]: %s\n", msg);
  78. }
  79. /* strerror - return pointer to appropriate system error message
  80. *
  81. * DESCRIPTION
  82. *
  83. * Get an error message string which is appropriate for the setting
  84. * of the errno variable.
  85. *
  86. * RETURNS
  87. *
  88. * Returns a pointer to a string which has an appropriate error
  89. * message for the present value of errno. The error message
  90. * strings are taken from sys_errlist[] where appropriate. If an
  91. * appropriate message is not available in sys_errlist, then a
  92. * pointer to the string "Unknown error (errno <errvalue>)" is
  93. * returned instead.
  94. */
  95. #ifdef __STDC__
  96. char *strerror(void)
  97. #else
  98. char *strerror()
  99. #endif
  100. {
  101. #ifdef _POSIX_SOURCE
  102. #undef strerror
  103. return (strerror(errno));
  104. #else
  105. static char msg[40]; /* used for "Unknown error" messages */
  106. if (errno > 0 && errno < sys_nerr) {
  107. return (sys_errlist[errno]);
  108. }
  109. sprintf(msg, "Unknown error (errno %d)", errno);
  110. return (msg);
  111. #endif
  112. }
  113. /* prsize - print a file offset on a file stream
  114. *
  115. * DESCRIPTION
  116. *
  117. * Prints a file offset to a specific file stream. The file offset is
  118. * of the form "%dm+%dk+%d", where the number preceeding the "m" and
  119. * the "k" stand for the number of Megabytes and the number of
  120. * Kilobytes, respectivley, which have been processed so far.
  121. *
  122. * PARAMETERS
  123. *
  124. * FILE *stream - Stream which is to be used for output
  125. * OFFSET size - Current archive position to be printed on the output
  126. * stream in the form: "%dm+%dk+%d".
  127. *
  128. */
  129. #ifdef __STDC__
  130. static void prsize(FILE *stream, OFFSET size)
  131. #else
  132. static void prsize(stream, size)
  133. FILE *stream; /* stream which is used for output */
  134. OFFSET size; /* current archive position to be printed */
  135. #endif
  136. {
  137. OFFSET n;
  138. if (n = (size / (1024L * 1024L))) {
  139. fprintf(stream, "%ldm+", n);
  140. size -= n * 1024L * 1024L;
  141. }
  142. if (n = (size / 1024L)) {
  143. fprintf(stream, "%ldk+", n);
  144. size -= n * 1024L;
  145. }
  146. fprintf(stream, "%ld", size);
  147. }
  148. /* fatal - print fatal message and exit
  149. *
  150. * DESCRIPTION
  151. *
  152. * Fatal prints the program's name along with an error message, then
  153. * exits the program with a non-zero return code.
  154. *
  155. * PARAMETERS
  156. *
  157. * char *why - description of reason for termination
  158. *
  159. * RETURNS
  160. *
  161. * Returns an exit code of 1 to the parent process.
  162. */
  163. #ifdef __STDC__
  164. void fatal(char *why)
  165. #else
  166. void fatal(why)
  167. char *why; /* description of reason for termination */
  168. #endif
  169. {
  170. fprintf(stderr, "%s: %s\n", myname, why);
  171. exit(1);
  172. }
  173. /* warn - print a warning message
  174. *
  175. * DESCRIPTION
  176. *
  177. * Print an error message listing the program name, the actual error
  178. * which occurred and an informational message as to why the error
  179. * occurred on the standard error device. The standard error is
  180. * flushed after the error is printed to assure that the user gets
  181. * the message in a timely fasion.
  182. *
  183. * PARAMETERS
  184. *
  185. * char *what - Pointer to string describing what failed.
  186. * char *why - Pointer to string describing why did it failed.
  187. */
  188. #ifdef __STDC__
  189. void warn(char *what, char *why)
  190. #else
  191. void warn(what, why)
  192. char *what; /* message as to what the error was */
  193. char *why; /* explanation why the error occurred */
  194. #endif
  195. {
  196. fprintf(stderr, "%s: %s : %s\n", myname, what, why);
  197. fflush(stderr);
  198. }