/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
#include <stdio.h>
#if _WIN32_
#include <winsock.h>
LPCSTR WSErrorDescription(int iErrorCode);
#endif

int main() {
#if _WIN32_
    WORD wVersionRequested = MAKEWORD(1,1);
    WSADATA wsaData;
    int rc;
#endif
    char name[30];

#if _WIN32_
    rc = WSAStartup(wVersionRequested, &wsaData);
    if (!rc) {
#endif
       if(!gethostname(name, sizeof(name)))
         printf("hostname = %s\n", name);
       else 
         printf("gethostname() failed\n");
#if _WIN32_
    }
    else
       fprintf(stderr,"\nWSAStartup() error (%d) %s\n", rc, WSErrorDescription(rc));
    WSACleanup();
#endif
}


#if _WIN32_
typedef struct tagWSAEDESCRIPTION {
        int iErrorCode;
        char *szDescription;
}WSAEDESCRIPTION;

WSAEDESCRIPTION ErrorDescriptions[] = {
        WSAEINTR,
        "WSAEINTR: Interrupted function call",
        WSAEBADF,
        "WSAEBADF: Bad file number",
        WSAEACCES,
        "WSAEACCES: Permission denied",
        WSAEFAULT,
        "WSAEFAULT: Address fault",
        WSAEINVAL,
        "WSAEINVAL: Invalid argument",
        WSAEMFILE,
        "WSAEMFILE: Too many open files",
        WSAEWOULDBLOCK,
        "WSAEWOULDBLOCK: Resource temporarily unavailable (Would block)",
        WSAEINPROGRESS,
        "WSAEINPROGRESS: Operation now in progress",
        WSAEALREADY,
        "WSAEALREADY: Operation already in progress",
        WSAENOTSOCK,
        "WSAENOTSOCK: Socket operation on non-socket",
        WSAEDESTADDRREQ,
        "WSAEDESTADDRREQ: Destination address required",
        WSAEMSGSIZE,
        "WSAEMSGSIZE: Message too long",
        WSAEPROTOTYPE,
        "WSAEPROTOTYPE: Protocol wrong type for socket",
        WSAENOPROTOOPT,
        "WSAENOPROTOOPT: Bad protocol option",
        WSAEPROTONOSUPPORT,
        "WSAEPROTONOSUPPORT: Protocol not supported",
        WSAESOCKTNOSUPPORT,
        "WSAESOCKTNOSUPPORT: Socket type not supported",
        WSAEOPNOTSUPP,
        "WSAEOPNOTSUPP: Operation not supported",
        WSAEPFNOSUPPORT,
        "WSAEPFNOSUPPORT: Protocol family not supported",
        WSAEAFNOSUPPORT,
        "WSAEAFNOSUPPORT: Address family not supported by protocol family",
        WSAEADDRINUSE,
        "WSAEADDRINUSE: Address already in use",
        WSAEADDRNOTAVAIL,
        "WSAEADDRNOTAVAIL: Cannot assign requested address",
        WSAENETDOWN,
        "WSAENETDOWN: Network is down",
        WSAENETUNREACH,
        "WSAENETUNREACH: Network is unreachable",
        WSAENETRESET,
        "WSAENETRESET: Network dropped connection on reset",
        WSAECONNABORTED,
        "WSAECONNABORTED: Software caused connection abort",
        WSAECONNRESET,
        "WSAECONNRESET: Connection reset by peer",
        WSAENOBUFS,
        "WSAENOBUFS: No buffer space available",
        WSAEISCONN,
        "WSAEISCONN: Socket is already connected",
        WSAENOTCONN,
        "WSAENOTCONN: Socket is not connected",
        WSAESHUTDOWN,
        "WSAESHUTDOWN: Cannot send after socket shutdown",
        WSAETOOMANYREFS,
        "WSAETOOMANYREFS: Too many references",
        WSAETIMEDOUT,
        "WSAETIMEDOUT: Connection timed out",
        WSAECONNREFUSED,
        "WSAECONNREFUSED: Connection refused",
        WSAELOOP,
        "WSAELOOP: Loop",
        WSAENAMETOOLONG,
        "WSAENAMETOOLONG: Name too long",
        WSAEHOSTDOWN,
        "WSAEHOSTDOWN: Host is down",
        WSAEHOSTUNREACH,
        "WSAEHOSTUNREACH: No route to host",
        WSAENOTEMPTY,
        "WSAENOTEMPTY: Directory not empty",
        WSAEPROCLIM,
        "WSAEPROCLIM: Too many processes",
        WSAEUSERS,
        "WSAEUSERS: Too many users",
        WSAEDQUOT,
        "WSAEDQUOT: Disk quota",
        WSAESTALE,
        "WSAESTALE: Stale",
        WSAEREMOTE,
        "WSAEREMOTE: Remote",
        WSASYSNOTREADY,
        "WSASYSNOTREADY: Network subsystem is unavailable",
        WSAVERNOTSUPPORTED,
        "WSAVERNOTSUPPORTED: WINSOCK.DLL version out of range",
        WSANOTINITIALISED,
        "WSANOTINITIALISED: Successful WSAStartup() not yet performed",
        WSAEDISCON,
        "WSAEDISCON: Graceful shutdown in progress",
        WSAHOST_NOT_FOUND,
        "WSAHOST_NOT_FOUND: Host not found",
        WSATRY_AGAIN,
        "WSATRY_AGAIN: Non-authoritative host not found",
        WSANO_RECOVERY,
        "WSANO_RECOVERY: Non-recoverable error",
        WSANO_DATA,
        "WSANO_DATA: Valid name, no data record of requested type"
};

//
// WSErrorDescription()
//
LPCSTR WSErrorDescription(int iErrorCode)
{
        #define NUMELEMENTS sizeof(ErrorDescriptions) / sizeof(ErrorDescriptions[0])
        int iNdx;

        for (iNdx = 0; iNdx < NUMELEMENTS; iNdx++)
        {
                if (ErrorDescriptions[iNdx].iErrorCode == iErrorCode)
                        return(ErrorDescriptions[iNdx].szDescription);
        }
        return("Unknown error");
}
#endif
