home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / PNTERR.C < prev    next >
C/C++ Source or Header  |  1993-09-25  |  4KB  |  116 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       p n t e r r . c                                              */
  3. /*                                                                    */
  4. /*       Report error message from NT error library                   */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: pnterr.c 1.1 1993/09/25 03:02:37 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: pnterr.c $
  24.  * Revision 1.1  1993/09/25  03:02:37  ahd
  25.  * Initial revision
  26.  *
  27.  * Revision 1.1  1993/09/24  21:43:27  dmwatt
  28.  * Initial revision
  29.  *
  30.  *
  31.  */
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*                        System include files                        */
  35. /*--------------------------------------------------------------------*/
  36.  
  37. #include <windows.h>
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <time.h>
  43. #include <io.h>
  44. #include <errno.h>
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    UUPC/extended include files                     */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include "lib.h"
  51. #include "pnterr.h"
  52.  
  53. currentfile();
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*    p N T e r r                                                     */
  57. /*                                                                    */
  58. /*    Perform a perror() with logging                                 */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. void pNTErr(const size_t lineno,
  62.              const char *fname,
  63.              const char *prefix,
  64.              DWORD rc)
  65. {
  66.    char buf[BUFSIZ];
  67.    static boolean recursion  = FALSE;
  68.    int l;
  69.    boolean redirect = ((logfile != stdout) && !isatty(fileno(stdout)));
  70.  
  71.    DWORD xrc;
  72.    xrc = FormatMessage(
  73.                 FORMAT_MESSAGE_FROM_SYSTEM,
  74.                 NULL, rc, LANG_USER_DEFAULT, buf, BUFSIZ, NULL);
  75.  
  76.    if ( xrc == 0 )
  77.    {
  78.  
  79.       if ( ! recursion )
  80.       {
  81.          recursion = TRUE;
  82.          printNTerror( "FormatMessage", xrc );
  83.          recursion = FALSE;
  84.       } /* recursion */
  85.  
  86.       sprintf(buf, "NT API error %u in %s at line %d, cannot find message",
  87.                    (unsigned int) rc,
  88.                    fname,
  89.                    lineno );
  90.  
  91.    } /* if ( xrc == 0 ) */
  92.  
  93. /*--------------------------------------------------------------------*/
  94. /*    Drop extra new from error message if we have room in our        */
  95. /*    small buffer                                                    */
  96. /*--------------------------------------------------------------------*/
  97.  
  98.    l = strlen( buf );
  99.  
  100.    if (( buf[l-1] == '\n') & (l < sizeof buf ))
  101.       buf[l-1] = '\0';          /* Drop extra newline from string      */
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*           Display the message with option file location            */
  105. /*--------------------------------------------------------------------*/
  106.  
  107.    printmsg(2,"NT API error %d in %s at line %d ...",
  108.             (int) rc, fname, lineno );
  109.  
  110.    printmsg(0,"%s: %s", prefix, buf);
  111.  
  112.    if ( redirect )
  113.       fprintf(stdout,"%s: %s\n", prefix, buf);
  114.  
  115. } /* pNTErr */
  116.