perror.c 793 B

1234567891011121314151617181920212223242526
  1. #include <stdio.h>
  2. #include <errno.h>
  3. /*
  4. * Manuel Novoa III Feb 2001
  5. *
  6. * Replaced old version that did write(2,...)'s with a version using
  7. * stream functions. If the program is calling perror, it's a safe
  8. * bet that printf and friends are used as well. It is also possible
  9. * that the calling program could buffer stderr, or reassign it.
  10. * Also, the old version did not conform the standards when the
  11. * passed char * was either NULL or pointed to an empty string.
  12. */
  13. void perror(__const char *str)
  14. {
  15. static const char perror_str[] = ": ";
  16. const char *sep;
  17. sep = perror_str;
  18. if (!(str && *str)) { /* Caller did not supply a prefix message */
  19. sep += 2; /* or passed an empty string. */
  20. str = sep;
  21. }
  22. fprintf(stderr, "%s%s%s\n", str, sep, strerror(errno));
  23. }