home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / PWSERR.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  9KB  |  198 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       p w s e r r . c                                              */
  3. /*                                                                    */
  4. /*       Report error message from Windows sockets error list         */
  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: pwserr.c 1.3 1993/10/03 20:37:34 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: pwserr.c $
  24.  * Revision 1.3  1993/10/03  20:37:34  ahd
  25.  * Correct FAR pointer references
  26.  *
  27.  * Revision 1.2  1993/10/02  23:45:16  ahd
  28.  * Delete unneeded function
  29.  *
  30.  * Revision 1.1  1993/10/02  23:12:35  dmwatt
  31.  * Initial revision
  32.  *
  33.  */
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*                        System include files                        */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. #include <stdio.h>
  40. #include <io.h>
  41.  
  42. #include "winsock.h"       // So we search local directories
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*                    UUPC/extended include files                     */
  46. /*--------------------------------------------------------------------*/
  47.  
  48. #include "lib.h"           // Cause we always include it!
  49.  
  50. /*--------------------------------------------------------------------*/
  51. /*                     Local function prototypes                      */
  52. /*--------------------------------------------------------------------*/
  53.  
  54. const char *LookupWSError(int err);
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                          Local variables                           */
  58. /*--------------------------------------------------------------------*/
  59.  
  60. typedef struct wserr {
  61.         int errno;
  62.         const char UUFAR *str;
  63. } WSERR;
  64.  
  65. static WSERR wsErrors[] = {
  66.   { WSABASEERR,         "[0] No Error" },
  67.   { WSAEINTR,           "[10004] Interrupted system call" },
  68.   { WSAEBADF,           "[10009] Bad file number" },
  69.   { WSAEACCES,          "[10013] Permission denied" },
  70.   { WSAEFAULT,          "[10014] Bad address" },
  71.   { WSAEINVAL,          "[10022] Invalid argument" },
  72.   { WSAEMFILE,          "[10024] Too many open files" },
  73.   { WSAEWOULDBLOCK,     "[10035] Operation would block" },
  74.   { WSAEINPROGRESS,     "[10036] Operation now in progress" },
  75.   { WSAEALREADY,        "[10037] Operation already in progress" },
  76.   { WSAENOTSOCK,        "[10038] Socket operation on non-socket" },
  77.   { WSAEDESTADDRREQ,    "[10039] Destination address required" },
  78.   { WSAEMSGSIZE,        "[10040] Message too long" },
  79.   { WSAEPROTOTYPE,      "[10041] Protocol wrong type for socket" },
  80.   { WSAENOPROTOOPT,     "[10042] Bad protocol option" },
  81.   { WSAEPROTONOSUPPORT, "[10043] Protocol not supported" },
  82.   { WSAESOCKTNOSUPPORT, "[10044] Socket type not supported" },
  83.   { WSAEOPNOTSUPP,      "[10045] Operation not supported on socket" },
  84.   { WSAEPFNOSUPPORT,    "[10046] Protocol family not supported" },
  85.   { WSAEAFNOSUPPORT,    "[10047] Address family not supported by protocol family" },
  86.   { WSAEADDRINUSE,      "[10048] Address already in use" },
  87.   { WSAEADDRNOTAVAIL,   "[10049] Can't assign requested address" },
  88.   { WSAENETDOWN,        "[10050] Network is down" },
  89.   { WSAENETUNREACH,     "[10051] Network is unreachable" },
  90.   { WSAENETRESET,       "[10052] Net dropped connection or reset" },
  91.   { WSAECONNABORTED,    "[10053] Software caused connection abort" },
  92.   { WSAECONNRESET,      "[10054] Connection reset by peer" },
  93.   { WSAENOBUFS,         "[10055] No buffer space available" },
  94.   { WSAEISCONN,         "[10056] Socket is already connected" },
  95.   { WSAENOTCONN,        "[10057] Socket is not connected" },
  96.   { WSAESHUTDOWN,       "[10058] Can't send after socket shutdown" },
  97.   { WSAETOOMANYREFS,    "[10059] Too many references, can't splice" },
  98.   { WSAETIMEDOUT,       "[10060] Connection timed out" },
  99.   { WSAECONNREFUSED,    "[10061] Connection refused" },
  100.   { WSAELOOP,           "[10062] Too many levels of symbolic links" },
  101.   { WSAENAMETOOLONG,    "[10063] File name too long" },
  102.   { WSAEHOSTDOWN,       "[10064] Host is down" },
  103.   { WSAEHOSTUNREACH,    "[10065] No Route to Host" },
  104.   { WSAENOTEMPTY,       "[10066] Directory not empty" },
  105.   { WSAEPROCLIM,        "[10067] Too many processes" },
  106.   { WSAEUSERS,          "[10068] Too many users" },
  107.   { WSAEDQUOT,          "[10069] Disc Quota Exceeded" },
  108.   { WSAESTALE,          "[10070] Stale NFS file handle" },
  109.   { WSAEREMOTE,         "[10071] Too many levels of remote in path" },
  110.   { WSASYSNOTREADY,     "[10091] Network SubSystem is unavailable" },
  111.   { WSAVERNOTSUPPORTED, "[10092] WINSOCK DLL Version out of range" },
  112.   { WSANOTINITIALISED,  "[10093] Successful WSASTARTUP not yet performed" },
  113.   { WSAHOST_NOT_FOUND,  "[11001] Host not found" },
  114.   { WSATRY_AGAIN,       "[11002] Non-Authoritative Host not found" },
  115.   { WSANO_RECOVERY,     "[11003] Non-Recoverable errors: FORMERR, REFUSED, NOTIMP" },
  116.   { WSANO_DATA,         "[11004] Valid name, no data record of requested type" },
  117.   { -1,                                 NULL }
  118. };
  119.  
  120. /* Copies string corresponding to the error code provided    */
  121. /* into buf, maximum length len. Returns length actually     */
  122. /* copied to buffer, or zero if error code is unknown.       */
  123. /* String resources should be present for each error code    */
  124. /* using the value of the code as the string ID (except for  */
  125. /* error = 0, which is mapped to WSABASEERR to keep it with  */
  126. /* the others). The DLL is free to use any string IDs that   */
  127. /* are less than WSABASEERR for its own use. The LibMain     */
  128. /* procedure of the DLL is presumed to have saved its        */
  129. /* HINSTANCE in the global variable hInst.                   */
  130.  
  131. /*--------------------------------------------------------------------*/
  132. /*                    UUPC/extended include files                     */
  133. /*--------------------------------------------------------------------*/
  134.  
  135. #include "lib.h"
  136. #include "pwserr.h"
  137.  
  138. /*--------------------------------------------------------------------*/
  139. /*    p W S e r r                                                     */
  140. /*                                                                    */
  141. /*    Perform a perror() with logging                                 */
  142. /*--------------------------------------------------------------------*/
  143.  
  144. void pWSErr(const size_t lineno,
  145.              const char *fname,
  146.              const char *prefix,
  147.              int rc)
  148. {
  149.    boolean redirect = ((logfile != stdout) && !isatty(fileno(stdout)));
  150.  
  151.    const char *msg;
  152.  
  153.    msg = LookupWSError(rc);
  154.  
  155.    if (msg == NULL) {
  156.       printmsg(0, "Invalid error sent to pWSErr: %d", rc);
  157.       return;
  158.    }
  159.  
  160. /*--------------------------------------------------------------------*/
  161. /*           Display the message with option file location            */
  162. /*--------------------------------------------------------------------*/
  163.  
  164.    printmsg(2,"Windows sockets error %d in %s at line %d ...",
  165.             (int) rc, fname, lineno );
  166.  
  167. #ifdef _Windows
  168.    printmsg(0,"%s: %Fs", prefix, msg);
  169. #else
  170.    printmsg(0,"%s: %s", prefix, msg);
  171. #endif
  172.  
  173.    if ( redirect )
  174. #ifdef _Windows
  175.      fprintf(stdout,"%s: %Fs\n", prefix, msg);
  176. #else
  177.      fprintf(stdout,"%s: %s\n", prefix, msg);
  178. #endif
  179.  
  180. } /* pWSErr */
  181.  
  182. /*--------------------------------------------------------------------*/
  183. /*       L o o k u p W S E r r o r                                    */
  184. /*                                                                    */
  185. /*       Look up an error in our list of strings                      */
  186. /*--------------------------------------------------------------------*/
  187.  
  188. const char *LookupWSError(int err)
  189. {
  190.    WSERR *pwsErr = wsErrors;
  191.  
  192.    while (pwsErr->errno != -1 && pwsErr->errno != err)
  193.       pwsErr++;
  194.  
  195.    return pwsErr->str;
  196.  
  197. } /* LookupWSError */
  198.