PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/UnxUtils/findutils-4.1/lib/error.c

http://rtoss.googlecode.com/
C | 119 lines | 78 code | 14 blank | 27 comment | 9 complexity | e84c46ce89cb3693a1d19d8f700e6e33 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0, GPL-2.0
  1. /* error.c -- error handler for noninteractive utilities
  2. Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  15. #ifdef HAVE_CONFIG_H
  16. #include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #if HAVE_VPRINTF || HAVE_DOPRNT
  20. # if __STDC__
  21. # include <stdarg.h>
  22. # define VA_START(args, lastarg) va_start(args, lastarg)
  23. # else
  24. # include <varargs.h>
  25. # define VA_START(args, lastarg) va_start(args)
  26. # endif
  27. #else
  28. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  29. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  30. #endif
  31. #if STDC_HEADERS
  32. # include <stdlib.h>
  33. # include <string.h>
  34. #else
  35. void exit ();
  36. #endif
  37. /* If NULL, error will flush stdout, then print on stderr the program
  38. name, a colon and a space. Otherwise, error will call this
  39. function without parameters instead. */
  40. void (*error_print_progname) () = NULL;
  41. /* The calling program should define program_name and set it to the
  42. name of the executing program. */
  43. extern char *program_name;
  44. #if HAVE_STRERROR
  45. char *strerror ();
  46. #else
  47. static char *
  48. private_strerror (errnum)
  49. int errnum;
  50. {
  51. extern char *sys_errlist[];
  52. extern int sys_nerr;
  53. if (errnum > 0 && errnum <= sys_nerr)
  54. return sys_errlist[errnum];
  55. return "Unknown system error";
  56. }
  57. #define strerror private_strerror
  58. #endif
  59. /* Print the program name and error message MESSAGE, which is a printf-style
  60. format string with optional args.
  61. If ERRNUM is nonzero, print its corresponding system error message.
  62. Exit with status STATUS if it is nonzero. */
  63. /* VARARGS */
  64. void
  65. #if defined(VA_START) && __STDC__
  66. error (int status, int errnum, const char *message, ...)
  67. #else
  68. error (status, errnum, message, va_alist)
  69. int status;
  70. int errnum;
  71. char *message;
  72. va_dcl
  73. #endif
  74. {
  75. #ifdef VA_START
  76. va_list args;
  77. #endif
  78. if (error_print_progname)
  79. (*error_print_progname) ();
  80. else
  81. {
  82. fflush (stdout);
  83. fprintf (stderr, "%s: ", program_name);
  84. }
  85. #ifdef VA_START
  86. VA_START (args, message);
  87. # if HAVE_VPRINTF
  88. vfprintf (stderr, message, args);
  89. # else
  90. _doprnt (message, args, stderr);
  91. # endif
  92. va_end (args);
  93. #else
  94. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  95. #endif
  96. if (errnum)
  97. fprintf (stderr, ": %s", strerror (errnum));
  98. putc ('\n', stderr);
  99. fflush (stderr);
  100. if (status)
  101. exit (status);
  102. }