perror.c 813 B

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