int main ( int argc, char ** argv ) /* give subnet as argument */ { struct addrinfo * servinfo, hints; int rv, dot_count, num, result; char * ip; ip = (char *) Malloc ( 15 * sizeof (char) ); if ( argc != 2 ) { fprintf ( stderr, "Usage: %s subnet/ip/hostname\n", argv[0] ); /* spit out an error if not correctly invoked */ return 1; } dot_count = strncnt ( argv[1], sizeof argv[1], '.' ); /* count dots in argv[1] */ if ( dot_count == 2 ) /* user passed a subnet */ { /* generate ip addresses in subnet */ for ( num = 0; num < 256; num++ ) { snprintf ( ip, 15, "%s.%d", argv[1], num ); memset ( &hints, 0, sizeof hints ); /* setup hints */ hints.ai_family = AF_INET; /* IPV4 */ hints.ai_socktype = SOCK_STREAM; /* just to commit */ if ( (rv = getaddrinfo ( ip, NULL, &hints, &servinfo )) != 0 ) { fprintf ( stderr, "getaddrinfo: %s\n", gai_strerror(rv) ); /* getaddrinfo fails */ return 1; } result = scan ( servinfo ); /* scan the address */ display ( result, ip ); } } else /* user passed an ip-address or a hostname */ { /* resolve the ip/hostname using getaddrinfo() */ memset ( &hints, 0, sizeof hints ); /* similar(?) */ hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if ( (rv = getaddrinfo ( argv[1], NULL, &hints, &servinfo )) != 0 ) { fprintf ( stderr, "getaddrinfo: %s\n", gai_strerror(rv) ); return 1; } result = scan ( servinfo ); display ( result, argv[1] ); } return 0; }
static int __cdecl __crtLCMapStringA_stat( _locale_t plocinfo, LPCWSTR LocaleName, DWORD dwMapFlags, LPCSTR lpSrcStr, int cchSrc, LPSTR lpDestStr, int cchDest, int code_page, BOOL bError ) { /* * LCMapString will map past NULL. Must find NULL if in string * before cchSrc characters. */ if (cchSrc > 0) { int cchSrcCnt = strncnt(lpSrcStr, cchSrc); /* * Include NULL in cchSrc if lpSrcStr terminated within cchSrc bytes. */ if (cchSrcCnt < cchSrc) { cchSrc = cchSrcCnt + 1; } else { cchSrc = cchSrcCnt; } } int retval = 0; int inbuff_size; int outbuff_size; wchar_t *inwbuffer = NULL; wchar_t *outwbuffer = NULL; /* * Convert string and return the requested information. Note that * we are converting to a wide string so there is not a * one-to-one correspondence between number of wide chars in the * input string and the number of *bytes* in the buffer. However, * there had *better be* a one-to-one correspondence between the * number of wide characters and the number of multibyte characters * or the resulting mapped string will be worthless to the user. */ /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == code_page) code_page = plocinfo->locinfo->lc_codepage; /* find out how big a buffer we need (includes NULL if any) */ if ( 0 == (inbuff_size = MultiByteToWideChar( code_page, bError ? MB_PRECOMPOSED | MB_ERR_INVALID_CHARS : MB_PRECOMPOSED, lpSrcStr, cchSrc, NULL, 0 )) ) return 0; /* allocate enough space for wide chars */ inwbuffer = (wchar_t *)_calloca( inbuff_size, sizeof(wchar_t) ); if ( inwbuffer == NULL ) { return 0; } /* do the conversion */ if ( 0 == MultiByteToWideChar( code_page, MB_PRECOMPOSED, lpSrcStr, cchSrc, inwbuffer, inbuff_size) ) goto error_cleanup; /* get size required for string mapping */ if ( 0 == (retval = __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, NULL, 0)) ) goto error_cleanup; if (dwMapFlags & LCMAP_SORTKEY) { /* retval is size in BYTES */ if (0 != cchDest) { if (retval > cchDest) goto error_cleanup; /* do string mapping */ if ( 0 == __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, (LPWSTR)lpDestStr, cchDest) ) goto error_cleanup; } } else { /* retval is size in wide chars */ outbuff_size = retval; /* allocate enough space for wide chars (includes NULL if any) */ outwbuffer = (wchar_t *)_calloca( outbuff_size, sizeof(wchar_t) ); if ( outwbuffer == NULL ) { goto error_cleanup; } /* do string mapping */ if ( 0 == __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, outwbuffer, outbuff_size) ) goto error_cleanup; if (0 == cchDest) { /* get size required */ if ( 0 == (retval = WideCharToMultiByte( code_page, 0, outwbuffer, outbuff_size, NULL, 0, NULL, NULL )) ) goto error_cleanup; } else { /* convert mapping */ if ( 0 == (retval = WideCharToMultiByte( code_page, 0, outwbuffer, outbuff_size, lpDestStr, cchDest, NULL, NULL )) ) goto error_cleanup; } } error_cleanup: if ( outwbuffer != NULL ) _freea(outwbuffer); _freea(inwbuffer); return retval; }
int __cdecl __crtLCMapStringA( LCID Locale, DWORD dwMapFlags, LPCSTR lpSrcStr, int cchSrc, LPSTR lpDestStr, int cchDest, int code_page, BOOL bError ) { static int f_use = 0; /* * Look for unstubbed 'preferred' flavor. Otherwise use available flavor. * Must actually call the function to ensure it's not a stub. * (Always try wide version first so WinNT can process codepage correctly.) */ if (0 == f_use) { if (0 != LCMapStringW(0, LCMAP_LOWERCASE, L"\0", 1, NULL, 0)) f_use = USE_W; else if (0 != LCMapStringA(0, LCMAP_LOWERCASE, "\0", 1, NULL, 0)) f_use = USE_A; else return 0; } /* * LCMapString will map past NULL. Must find NULL if in string * before cchSrc characters. */ if (cchSrc > 0) cchSrc = strncnt(lpSrcStr, cchSrc); /* Use "A" version */ if (USE_A == f_use) { return LCMapStringA( Locale, dwMapFlags, lpSrcStr, cchSrc, lpDestStr, cchDest ); } /* Use "W" version */ if (USE_W == f_use) { int retval; int inbuff_size; int outbuff_size; wchar_t *inwbuffer; wchar_t *outwbuffer; /* * Convert string and return the requested information. Note that * we are converting to a wide string so there is not a * one-to-one correspondence between number of wide chars in the * input string and the number of *bytes* in the buffer. However, * there had *better be* a one-to-one correspondence between the * number of wide characters and the number of multibyte characters * or the resulting mapped string will be worthless to the user. */ /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == code_page) code_page = __lc_codepage; /* find out how big a buffer we need (includes NULL if any) */ if ( 0 == (inbuff_size = MultiByteToWideChar( code_page, bError ? MB_PRECOMPOSED | MB_ERR_INVALID_CHARS : MB_PRECOMPOSED, lpSrcStr, cchSrc, NULL, 0 )) ) return 0; /* allocate enough space for wide chars */ __try { inwbuffer = (wchar_t *)_alloca( inbuff_size * sizeof(wchar_t) ); } __except(EXCEPTION_EXECUTE_HANDLER) { inwbuffer = NULL; } if ( inwbuffer == NULL ) return 0; /* do the conversion */ if ( 0 == MultiByteToWideChar( code_page, MB_PRECOMPOSED, lpSrcStr, cchSrc, inwbuffer, inbuff_size) ) return 0; /* get size required for string mapping */ if ( 0 == (retval = LCMapStringW( Locale, dwMapFlags, inwbuffer, inbuff_size, NULL, 0 )) ) return 0; if (dwMapFlags & LCMAP_SORTKEY) { /* retval is size in BYTES */ if (0 != cchDest) { if (retval > cchDest) return 0; /* do string mapping */ if ( 0 == LCMapStringW( Locale, dwMapFlags, inwbuffer, inbuff_size, (LPWSTR)lpDestStr, cchDest ) ) return 0; } } else { /* retval is size in wide chars */ outbuff_size = retval; /* allocate enough space for wide chars (includes NULL if any) */ __try { outwbuffer = (wchar_t *)_alloca( outbuff_size * sizeof(wchar_t) ); } __except(EXCEPTION_EXECUTE_HANDLER) { outwbuffer = NULL; } if ( outwbuffer == NULL ) return 0; /* do string mapping */ if ( 0 == LCMapStringW( Locale, dwMapFlags, inwbuffer, inbuff_size, outwbuffer, outbuff_size ) ) return 0; if (0 == cchDest) { /* get size required */ if ( 0 == (retval = WideCharToMultiByte( code_page, WC_COMPOSITECHECK | WC_SEPCHARS, outwbuffer, outbuff_size, NULL, 0, NULL, NULL )) ) return 0; } else { /* convert mapping */ if ( 0 == (retval = WideCharToMultiByte( code_page, WC_COMPOSITECHECK | WC_SEPCHARS, outwbuffer, outbuff_size, lpDestStr, cchDest, NULL, NULL )) ) return 0; } } return retval; }