Skip to content

Commit b7cc9f8

Browse files
mstorsjogitster
authored andcommitted
Refactor winsock initialization into a separate function
The winsock library must be initialized. Since gethostbyname() is the first function that calls into winsock, it was overridden to do the initialization. This refactoring helps the next patch, where other functions can be called earlier. Signed-off-by: Martin Storsjo <[email protected]> Acked-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1fdffa6 commit b7cc9f8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

compat/mingw.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,16 +903,25 @@ char **make_augmented_environ(const char *const *vars)
903903
return env;
904904
}
905905

906-
/* this is the first function to call into WS_32; initialize it */
907-
#undef gethostbyname
908-
struct hostent *mingw_gethostbyname(const char *host)
906+
static void ensure_socket_initialization(void)
909907
{
910908
WSADATA wsa;
909+
static int initialized = 0;
910+
911+
if (initialized)
912+
return;
911913

912914
if (WSAStartup(MAKEWORD(2,2), &wsa))
913915
die("unable to initialize winsock subsystem, error %d",
914916
WSAGetLastError());
915917
atexit((void(*)(void)) WSACleanup);
918+
initialized = 1;
919+
}
920+
921+
#undef gethostbyname
922+
struct hostent *mingw_gethostbyname(const char *host)
923+
{
924+
ensure_socket_initialization();
916925
return gethostbyname(host);
917926
}
918927

0 commit comments

Comments
 (0)