home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FERRORF.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  68 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FERRORF.C - Functions for formatted error messages
  5. */
  6.  
  7. /*
  8. **  ferrorf()
  9. **
  10. **  Prints error message with printf() formatting syntax, then a colon,
  11. **  then a message corresponding to the value of errno, then a newline.
  12. **  Output is to filehandle.
  13. **
  14. **  Public Domain by Mark R. Devlin, free usage is permitted.
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include "errors.h"
  21.  
  22. int ferrorf(FILE *filehandle, const char *format, ...)
  23. {
  24.       int vfp, fp;
  25.       va_list vargs;
  26.  
  27.       vfp = fp = 0;
  28.       va_start(vargs, format);
  29.       vfp = vfprintf(filehandle, format, vargs);
  30.       va_end(vargs);
  31.       fp = fprintf(filehandle, ": %s\n", sys_errlist[errno]);
  32.       return ((vfp==EOF || fp==EOF) ? EOF : (vfp+fp));
  33. }
  34.  
  35. /*
  36. **  cant() - An fopen() replacement with error trapping
  37. **
  38. **  Call just as you would fopen(), but make sure your exit functions are
  39. **  registered with atexit().
  40. **
  41. **  public domain by Bob Stout
  42. */
  43.  
  44. FILE *cant(char *fname, char *fmode)
  45. {
  46.       FILE *fp;
  47.  
  48.       if (NULL == (fp = fopen(fname, fmode)))
  49.       {
  50.             ferrorf(stderr, "Can't open %s", fname);
  51.             exit(EXIT_FAILURE);
  52.       }
  53.       return fp;
  54. }
  55.  
  56. #ifdef TEST
  57.  
  58. main()
  59. {
  60.       char badname[] = "????????.???";
  61.       FILE *fp;
  62.  
  63.       fp = cant(badname, "r");
  64.       return 0;
  65. }
  66.  
  67. #endif /* TEST */
  68.