PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bin/sharutils/lib/error.c

http://github.com/Barrett17/Haiku-services-branch
C | 174 lines | 124 code | 21 blank | 29 comment | 16 complexity | a07ad05185079742ffadec0b1d2affd2 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.0, LGPL-2.1, BSD-2-Clause, ISC, Apache-2.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, Unlicense, BSD-3-Clause, LGPL-3.0
  1. /* error.c -- error handler for noninteractive utilities
  2. Copyright (C) 1990, 91, 92, 93, 94, 1995
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #if HAVE_VPRINTF || HAVE_DOPRNT
  22. # if __STDC__
  23. # include <stdarg.h>
  24. # define VA_START(args, lastarg) va_start(args, lastarg)
  25. # else
  26. # include <varargs.h>
  27. # define VA_START(args, lastarg) va_start(args)
  28. # endif
  29. #else
  30. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  31. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  32. #endif
  33. #if STDC_HEADERS
  34. # include <stdlib.h>
  35. # include <string.h>
  36. #else
  37. void exit ();
  38. #endif
  39. //#include <libintl.h>
  40. #define _(str) (str)
  41. /* If NULL, error will flush stdout, then print on stderr the program
  42. name, a colon and a space. Otherwise, error will call this
  43. function without parameters instead. */
  44. void (*error_print_progname) () = NULL;
  45. /* The calling program should define program_name and set it to the
  46. name of the executing program. */
  47. extern char *program_name;
  48. #if HAVE_STRERROR
  49. char *strerror ();
  50. #else
  51. static char *
  52. private_strerror (errnum)
  53. int errnum;
  54. {
  55. extern char *sys_errlist[];
  56. extern int sys_nerr;
  57. if (errnum > 0 && errnum <= sys_nerr)
  58. return sys_errlist[errnum];
  59. return _("Unknown system error");
  60. }
  61. #define strerror private_strerror
  62. #endif
  63. /* Print the program name and error message MESSAGE, which is a printf-style
  64. format string with optional args.
  65. If ERRNUM is nonzero, print its corresponding system error message.
  66. Exit with status STATUS if it is nonzero. */
  67. /* VARARGS */
  68. void
  69. #if defined(VA_START) && __STDC__
  70. error (int status, int errnum, const char *message, ...)
  71. #else
  72. error (status, errnum, message, va_alist)
  73. int status;
  74. int errnum;
  75. char *message;
  76. va_dcl
  77. #endif
  78. {
  79. #ifdef VA_START
  80. va_list args;
  81. #endif
  82. if (error_print_progname)
  83. (*error_print_progname) ();
  84. else
  85. {
  86. fflush (stdout);
  87. fprintf (stderr, "%s: ", program_name);
  88. }
  89. #ifdef VA_START
  90. VA_START (args, message);
  91. # if HAVE_VPRINTF
  92. vfprintf (stderr, message, args);
  93. # else
  94. _doprnt (message, args, stderr);
  95. # endif
  96. va_end (args);
  97. #else
  98. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  99. #endif
  100. if (errnum)
  101. fprintf (stderr, ": %s", strerror (errnum));
  102. putc ('\n', stderr);
  103. fflush (stderr);
  104. if (status)
  105. exit (status);
  106. }
  107. void
  108. #if defined(VA_START) && __STDC__
  109. error_with_loc (int status, int errnum, const char *file_name,
  110. size_t line_number, const char *message, ...)
  111. #else
  112. error_with_loc (status, errnum, file_name, line_number, message, va_alist)
  113. int status;
  114. int errnum;
  115. char *file_name;
  116. size_t line_number;
  117. char *message;
  118. va_dcl
  119. #endif
  120. {
  121. #ifdef VA_START
  122. va_list args;
  123. #endif
  124. if (error_print_progname)
  125. (*error_print_progname) ();
  126. else
  127. {
  128. fflush (stdout);
  129. fprintf (stderr, "%s:", program_name);
  130. }
  131. if (file_name != NULL)
  132. fprintf (stderr, "%s:%d: ", file_name, line_number);
  133. #ifdef VA_START
  134. VA_START (args, message);
  135. # if HAVE_VPRINTF
  136. vfprintf (stderr, message, args);
  137. # else
  138. _doprnt (message, args, stderr);
  139. # endif
  140. va_end (args);
  141. #else
  142. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  143. #endif
  144. if (errnum)
  145. fprintf (stderr, ": %s", strerror (errnum));
  146. putc ('\n', stderr);
  147. fflush (stderr);
  148. if (status)
  149. exit (status);
  150. }