error.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*====================================================================*
  2. *
  3. * signed error (signed status, errno_t number, char const * format, ...);
  4. *
  5. * error.h
  6. *
  7. * custom implementation of GNU error() function for systems
  8. * that do not have it; this version always returns -1;
  9. *
  10. * Motley Tools by Charles Maier;
  11. * Copyright (c) 2001-2006 by Charles Maier Associates;
  12. * Licensed under the Internet Software Consortium License;
  13. *
  14. *--------------------------------------------------------------------*/
  15. #ifndef ERROR_SOURCE
  16. #define ERROR_SOURCE
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <string.h>
  21. #include "../tools/types.h"
  22. #include "../tools/error.h"
  23. #ifdef __GNUC__
  24. __attribute__ ((format (printf, 3, 4)))
  25. #endif
  26. signed error (signed status, errno_t number, char const * format, ...)
  27. {
  28. extern char const *program_name;
  29. if ((program_name) && (*program_name))
  30. {
  31. fprintf (stderr, "%s: ", program_name);
  32. }
  33. #if 1
  34. if ((format) && (*format))
  35. {
  36. va_list arglist;
  37. va_start (arglist, format);
  38. vfprintf (stderr, format, arglist);
  39. va_end (arglist);
  40. }
  41. if (number)
  42. {
  43. fprintf (stderr, ": %s", strerror (number));
  44. }
  45. #else
  46. if (number)
  47. {
  48. fprintf (stderr, "%s: ", strerror (number));
  49. }
  50. if ((format) && (*format))
  51. {
  52. va_list arglist;
  53. va_start (arglist, format);
  54. vfprintf (stderr, format, arglist);
  55. va_end (arglist);
  56. }
  57. #endif
  58. fprintf (stderr, "\n");
  59. fflush (stderr);
  60. if (status)
  61. {
  62. exit (status);
  63. }
  64. return (-1);
  65. }
  66. #endif