00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "plibc_private.h"
00026
00027 #define DEBUG_WINPROC 0
00028
00029 char szRootDir[_MAX_PATH + 1];
00030 long lRootDirLen;
00031 char szHomeDir[_MAX_PATH + 2];
00032 long lHomeDirLen;
00033 char szUser[261] = "";
00034 char *_pszOrg = NULL, *_pszApp = NULL;
00035 OSVERSIONINFO theWinVersion;
00036 unsigned int uiSockCount = 0;
00037 Winsock *pSocks;
00038 HANDLE hSocksLock;
00039 unsigned int uiMappingsCount = 0;
00040 TMapping *pMappings;
00041 HANDLE hMappingsLock;
00042 TPanicProc __plibc_panic = NULL;
00043 int iInit = 0;
00044
00045 static HINSTANCE hIphlpapi, hAdvapi;
00046
00047 BOOL __win_IsHandleMarkedAsBlocking(SOCKET hHandle)
00048 {
00049 BOOL bBlocking;
00050 unsigned int uiIndex;
00051
00052 bBlocking = TRUE;
00053 WaitForSingleObject(hSocksLock, INFINITE);
00054 for(uiIndex = 0; uiIndex <= uiSockCount; uiIndex++)
00055 {
00056 if (pSocks[uiIndex].s == hHandle)
00057 {
00058 bBlocking = pSocks[uiIndex].bBlocking;
00059 break;
00060 }
00061 }
00062 ReleaseMutex(hSocksLock);
00063
00064 return bBlocking;
00065 }
00066
00067 void __win_SetHandleBlockingMode(SOCKET s, BOOL bBlocking)
00068 {
00069 unsigned int uiIndex = 0;
00070 int bFound = 0;
00071
00072 WaitForSingleObject(hSocksLock, INFINITE);
00073
00074 for(uiIndex = 0; uiIndex <= uiSockCount; uiIndex++)
00075 {
00076 if (pSocks[uiIndex].s == s)
00077 {
00078 bFound = 1;
00079 break;
00080 }
00081 }
00082
00083 if (bFound)
00084 pSocks[uiIndex].bBlocking = bBlocking;
00085 else
00086 {
00087 uiIndex = 0;
00088
00089 while(TRUE)
00090 {
00091 if (pSocks[uiIndex].s == -1)
00092 {
00093 pSocks[uiIndex].s = s;
00094 pSocks[uiIndex].bBlocking = bBlocking;
00095 }
00096 if (uiIndex == uiSockCount)
00097 {
00098 uiSockCount++;
00099 pSocks = (Winsock *) realloc(pSocks, (uiSockCount + 1) * sizeof(Winsock));
00100 pSocks[uiSockCount].s = -1;
00101
00102 break;
00103 }
00104 uiIndex++;
00105 }
00106 }
00107 ReleaseMutex(hSocksLock);
00108 }
00109
00110 void __win_DiscardHandleBlockingMode(SOCKET s)
00111 {
00112 unsigned int uiIndex;
00113
00114 WaitForSingleObject(hSocksLock, INFINITE);
00115 for(uiIndex = 0; uiIndex <= uiSockCount; uiIndex++)
00116 if (pSocks[uiIndex].s == s)
00117 pSocks[uiIndex].s = -1;
00118 ReleaseMutex(hSocksLock);
00119 }
00120
00125 int _win_isSocketValid(int s)
00126 {
00127 long l;
00128 return ioctlsocket(s, FIONREAD, &l) != SOCKET_ERROR && _get_osfhandle(s) == -1;
00129 }
00130
00135 void __plibc_panic_default(int err, char *szMsg)
00136 {
00137 #if DEBUG_WINPROC
00138 if(err == INT_MAX)
00139 fputs(stderr, szMsg);
00140 #endif
00141 }
00142
00150 int plibc_init(char *pszOrg, char *pszApp)
00151 {
00152 long lRet;
00153 WSADATA wsaData;
00154 enum {ROOT, USER, HOME} eAction = ROOT;
00155 UINT uiCP;
00156 char szLang[11] = "LANG=";
00157 LCID locale;
00158
00159 if (iInit)
00160 return ERROR_SUCCESS;
00161
00162 __plibc_panic = __plibc_panic_default;
00163
00164
00165 if((lRet = _plibc_DetermineRootDir()) == ERROR_SUCCESS)
00166 {
00167 DWORD dwSize = 261;
00168
00169 eAction = USER;
00170 GetUserName(szUser, &dwSize);
00171
00172 eAction = HOME;
00173 lRet = _plibc_DetermineHomeDir();
00174 }
00175
00176 if(lRet != ERROR_SUCCESS)
00177 {
00178 char *pszMsg, *pszMsg2;
00179 char szPanic[1001];
00180 long lMem;
00181
00182 lMem =
00183 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00184 FORMAT_MESSAGE_FROM_SYSTEM |
00185 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lRet, 0,
00186 (LPTSTR) & pszMsg, 0, NULL);
00187
00188 pszMsg2 = (char *) malloc(lMem + 1);
00189 strcpy(pszMsg2, pszMsg);
00190 if(pszMsg2[lMem - 2] == '\r')
00191 pszMsg2[lMem - 2] = 0;
00192
00193 _win_snprintf(szPanic, 1000, "Cannot determine %s (%s)\n",
00194 eAction == ROOT ? "root directory" :
00195 "home directory", pszMsg2);
00196 szPanic[1000] = 0;
00197 __plibc_panic(1, szPanic);
00198
00199 LocalFree(pszMsg);
00200 free(pszMsg2);
00201
00202 return lRet;
00203 }
00204
00205
00206 if (WSAStartup(257, &wsaData) != 0)
00207 {
00208 __plibc_panic(2, "Cannot initialize Winsock");
00209
00210 return GetLastError();
00211 }
00212
00213
00214 pSocks = (Winsock *) malloc(sizeof(Winsock) + (uiSockCount + 1));
00215 pSocks[0].s = -1;
00216 hSocksLock = CreateMutex(NULL, FALSE, NULL);
00217
00218
00219 pMappings = (TMapping *) malloc(sizeof(TMapping));
00220 pMappings[0].pStart = NULL;
00221 hMappingsLock = CreateMutex(NULL, FALSE, NULL);
00222
00223
00224 _fmode = _O_BINARY;
00225
00226
00227 theWinVersion.dwOSVersionInfoSize = sizeof(theWinVersion);
00228 GetVersionEx(&theWinVersion);
00229
00230
00231 uiCP = GetACP();
00232 SetConsoleCP(uiCP);
00233 SetConsoleOutputCP(uiCP);
00234 setlocale( LC_ALL, ".OCP" );
00235
00236
00237 locale = GetThreadLocale();
00238 GetLocaleInfo(locale, LOCALE_SISO3166CTRYNAME, szLang + 5, 3);
00239 szLang[7] = '_';
00240 GetLocaleInfo(locale, LOCALE_SISO639LANGNAME, szLang + 8, 3);
00241 putenv(szLang);
00242
00243
00244 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
00245
00246 _pszOrg = strdup(pszOrg);
00247 _pszApp = strdup(pszApp);
00248
00249 iInit = 1;
00250
00251 return ERROR_SUCCESS;
00252 }
00253
00257 void plibc_shutdown()
00258 {
00259 if (!iInit)
00260 return;
00261
00262 WSACleanup();
00263 free(pSocks);
00264 CloseHandle(hSocksLock);
00265
00266 free(pMappings);
00267 CloseHandle(hMappingsLock);
00268
00269 FreeLibrary(hIphlpapi);
00270 FreeLibrary(hAdvapi);
00271
00272 CoUninitialize();
00273
00274 free(_pszOrg);
00275 free(_pszApp);
00276
00277 iInit = 0;
00278 }
00279
00285 void plibc_set_panic_proc(TPanicProc proc)
00286 {
00287 __plibc_panic = proc;
00288 }
00289
00290 int IsWinNT()
00291 {
00292 return theWinVersion.dwPlatformId == VER_PLATFORM_WIN32_NT;
00293 }
00294
00295