static DWORD __apxMultiSzToJvmOptions(APXHANDLE hPool, LPCSTR lpString, JavaVMOption **lppArray, DWORD nExtra) { DWORD i, n = 0, l = 0; char *buff; LPSTR p; if (lpString) { l = __apxGetMultiSzLengthA(lpString, &n); } n += nExtra; buff = apxPoolAlloc(hPool, (n + 1) * sizeof(JavaVMOption) + (l + 1)); *lppArray = (JavaVMOption *)buff; p = (LPSTR)(buff + (n + 1) * sizeof(JavaVMOption)); if (lpString) AplCopyMemory(p, lpString, l + 1); for (i = 0; i < (n - nExtra); i++) { DWORD qr = apxStrUnQuoteInplaceA(p); (*lppArray)[i].optionString = p; while (*p) p++; p++; p += qr; } return n; }
DWORD apxMultiSzToArrayA(APXHANDLE hPool, LPCSTR lpString, LPSTR **lppArray) { DWORD i, n, l; char *buff; LPSTR p; l = __apxGetMultiSzLengthA(lpString, &n); if (!n || !l) return 0; if (IS_INVALID_HANDLE(hPool)) buff = apxPoolAlloc(hPool, (n + 2) * sizeof(LPTSTR) + (l + 1)); else buff = apxAlloc((n + 2) * sizeof(LPSTR) + (l + 1) * sizeof(CHAR)); *lppArray = (LPSTR *)buff; p = (LPSTR)(buff + (n + 2) * sizeof(LPSTR)); AplCopyMemory(p, lpString, (l + 1) * sizeof(CHAR)); for (i = 0; i < n; i++) { (*lppArray)[i] = p; while (*p) p++; p++; } (*lppArray)[++i] = NULL; return n; }
LPWSTR apxExpandStrW(APXHANDLE hPool, LPCWSTR szString) { LPCWSTR p = szString; while (*p) { if (*p == L'%') { p = szString; break; } ++p; } if (p != szString) return apxPoolStrdupW(hPool, szString); else { DWORD l = ExpandEnvironmentStringsW(szString, NULL, 0); if (l) { LPWSTR rv = apxPoolAlloc(hPool, l * sizeof(WCHAR)); l = ExpandEnvironmentStringsW(szString, rv, l); if (l) return rv; else { apxFree(rv); return NULL; } } else return NULL; } }
BOOL apxAddToPathW(APXHANDLE hPool, LPCWSTR szAdd) { LPWSTR wsAdd; DWORD rc; DWORD al; rc = GetEnvironmentVariableW(L"PATH", NULL, 0); if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) return FALSE; al = lstrlenW(szAdd) + 6; if (!(wsAdd = apxPoolAlloc(hPool, (al + rc + 1) * sizeof(WCHAR)))) return FALSE; lstrcpyW(wsAdd, L"PATH="); lstrcatW(wsAdd, szAdd); lstrcatW(wsAdd, L";"); if (!GetEnvironmentVariableW(L"PATH", wsAdd + al, rc - al)) { apxLogWrite(APXLOG_MARK_SYSERR); apxFree(wsAdd); return FALSE; } SetEnvironmentVariableW(L"PATH", wsAdd + 5); _wputenv(wsAdd); apxFree(wsAdd); return TRUE; }
LPWSTR apxLogFile( APXHANDLE hPool, LPCWSTR szPath, LPCWSTR szPrefix, LPCWSTR szName) { LPWSTR sRet; WCHAR sPath[MAX_PATH+1]; WCHAR sName[MAX_PATH+1]; SYSTEMTIME sysTime; GetLocalTime(&sysTime); if (!szPath) { if (GetSystemDirectoryW(sPath, MAX_PATH) == 0) return INVALID_HANDLE_VALUE; lstrcatW(sPath, L"\\LogFiles\\"); if (!szPrefix) lstrcatW(sPath, L"Apache"); else lstrcatW(sPath, szPrefix); wsprintfW(sName, L"\\%s%04d%02d%02d.log", szName, sysTime.wYear, sysTime.wMonth, sysTime.wDay); } else { lstrcpyW(sPath, szPath); if (szPrefix) wsprintfW(sName, L"\\%s", szPrefix); else wsprintfW(sName, L"\\%s%04d%02d%02d.log", szName, sysTime.wYear, sysTime.wMonth, sysTime.wDay); } sRet = apxPoolAlloc(hPool, (MAX_PATH + 1) * sizeof(WCHAR)); /* Set default level to info */ CreateDirectoryW(sPath, NULL); lstrcpyW(sRet, sPath); lstrcatW(sRet, sName); return sRet; }
LPSTR __apxGetEnvironmentVariableA(APXHANDLE hPool, LPCSTR szName) { LPSTR szRet; DWORD rc; rc = GetEnvironmentVariableA(szName, NULL, 0); if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) return NULL; if (!(szRet = apxPoolAlloc(hPool, rc + 1))) return NULL; if (!GetEnvironmentVariableA(szName, szRet, rc)) { apxLogWrite(APXLOG_MARK_SYSERR); apxFree(szRet); return NULL; } return szRet; }
LPWSTR __apxGetEnvironmentVariableW(APXHANDLE hPool, LPCWSTR wsName) { LPWSTR wsRet; DWORD rc; rc = GetEnvironmentVariableW(wsName, NULL, 0); if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) return NULL; if (!(wsRet = apxPoolAlloc(hPool, (rc + 1) * sizeof(WCHAR)))) return NULL; if (!GetEnvironmentVariableW(wsName, wsRet, rc)) { apxLogWrite(APXLOG_MARK_SYSERR); apxFree(wsRet); return NULL; } return wsRet; }
LPSTR apxArrayToMultiSzA(APXHANDLE hPool, DWORD nArgs, LPCSTR *lpArgs) { DWORD i, l = 0; LPSTR lpSz, p; if (!nArgs) return NULL; for (i = 0; i < nArgs; i++) l += lstrlenA(lpArgs[i]); l += (nArgs + 2); p = lpSz = (LPSTR)apxPoolAlloc(hPool, l); for (i = 0; i < nArgs; i++) { lstrcpyA(p, lpArgs[i]); p += lstrlenA(lpArgs[i]); *p++ = '\0'; } *p++ = '\0'; *p++ = '\0'; return lpSz; }
BOOL apxSetEnvironmentVariable(APXHANDLE hPool, LPCTSTR szName, LPCTSTR szValue, BOOL bAppend) { LPTSTR szNew = (LPTSTR)szValue; if (bAppend) { DWORD l = GetEnvironmentVariable(szName, NULL, 0); if (l > 0) { BOOL rv; if (IS_INVALID_HANDLE(hPool)) szNew = apxAlloc(l + lstrlen(szValue) + 3); else szNew = apxPoolAlloc(hPool, l + lstrlen(szValue) + 3); GetEnvironmentVariable(szName, szNew, l + 1); lstrcat(szNew, TEXT(";")); lstrcat(szNew, szValue); rv = SetEnvironmentVariable(szName, szNew); apxFree(szNew); return rv; } } return SetEnvironmentVariable(szName, szNew); }
LPWSTR apxLogFile( APXHANDLE hPool, LPCWSTR szPath, LPCWSTR szPrefix, LPCWSTR szName, BOOL bTimeStamp, DWORD dwRotate) { LPWSTR sRet; WCHAR sPath[SIZ_PATHLEN]; WCHAR sName[SIZ_PATHLEN]; SYSTEMTIME sysTime; GetLocalTime(&sysTime); if (!szPath) { if (GetSystemDirectoryW(sPath, MAX_PATH) == 0) return INVALID_HANDLE_VALUE; lstrlcatW(sPath, MAX_PATH, L"\\LogFiles\\Apache"); } else { lstrlcpyW(sPath, MAX_PATH, szPath); } if (!szPrefix) szPrefix = L""; if (!szName) szName = L""; if (bTimeStamp) { if (dwRotate != 0 && dwRotate < 86400) wsprintfW(sName, L"\\%s%s" LOGF_EXR, szPrefix, szName, sysTime.wYear, sysTime.wMonth, sysTime.wDay, 0, 0, 0); else wsprintfW(sName, L"\\%s%s" LOGF_EXT, szPrefix, szName, sysTime.wYear, sysTime.wMonth, sysTime.wDay); } else { wsprintfW(sName, L"\\%s%s", szPrefix, szName); } sRet = apxPoolAlloc(hPool, (SIZ_PATHLEN) * sizeof(WCHAR)); /* Set default level to info */ CreateDirectoryW(sPath, NULL); lstrlcpyW(sRet, SIZ_PATHMAX, sPath); lstrlcatW(sRet, SIZ_PATHMAX, sName); return sRet; }
/* ANSI version only */ DWORD apxJavaCmdInitialize(APXHANDLE hPool, LPCWSTR szClassPath, LPCWSTR szClass, LPCWSTR szOptions, DWORD dwMs, DWORD dwMx, DWORD dwSs, LPCWSTR szCmdArgs, LPWSTR **lppArray) { DWORD i, nJVM, nCmd, nTotal, lJVM, lCmd; LPWSTR p; /* Calculate the number of all arguments */ nTotal = 0; if (szClassPath) ++nTotal; if (szClass) ++nTotal; lJVM = __apxGetMultiSzLengthW(szOptions, &nJVM); nTotal += nJVM; lCmd = __apxGetMultiSzLengthW(szCmdArgs, &nCmd); nTotal += nCmd; if (dwMs) ++nTotal; if (dwMx) ++nTotal; if (dwSs) ++nTotal; if (nTotal == 0) return 0; /* Allocate the array to store all arguments' pointers */ *lppArray = (LPWSTR *)apxPoolAlloc(hPool, (nTotal + 2) * sizeof(LPWSTR)); /* Process JVM options */ if (nJVM && lJVM) { p = (LPWSTR)apxPoolAlloc(hPool, (lJVM + 1) * sizeof(WCHAR)); AplCopyMemory(p, szOptions, (lJVM + 1) * sizeof(WCHAR) + sizeof(WCHAR)); for (i = 0; i < nJVM; i++) { (*lppArray)[i] = p; while (*p) p++; p++; } } /* Process the 3 extra JVM options */ if (dwMs) { p = (LPWSTR)apxPoolAlloc(hPool, 64 * sizeof(WCHAR)); wsprintfW(p, L"-Xms%dm", dwMs); (*lppArray)[i++] = p; } if (dwMx) { p = (LPWSTR)apxPoolAlloc(hPool, 64 * sizeof(WCHAR)); wsprintfW(p, L"-Xmx%dm", dwMx); (*lppArray)[i++] = p; } if (dwSs) { p = (LPWSTR)apxPoolAlloc(hPool, 64 * sizeof(WCHAR)); wsprintfW(p, L"-Xss%dk", dwSs); (*lppArray)[i++] = p; } /* Process the classpath and class */ if (szClassPath) { p = (LPWSTR)apxPoolAlloc(hPool, (lstrlenW(JAVA_CLASSPATH_W) + lstrlenW(szClassPath)) * sizeof(WCHAR)); lstrcpyW(p, JAVA_CLASSPATH_W); lstrcatW(p, szClassPath); (*lppArray)[i++] = p; } if (szClass) { p = (LPWSTR)apxPoolAlloc(hPool, (lstrlenW(szClass)) * sizeof(WCHAR)); lstrcpyW(p, szClass); (*lppArray)[i++] = p; } /* Process command arguments */ if (nCmd && lCmd) { p = (LPWSTR)apxPoolAlloc(hPool, (lCmd + 1) * sizeof(WCHAR)); AplCopyMemory(p, szCmdArgs, (lCmd + 1) * sizeof(WCHAR) + sizeof(WCHAR)); for (; i < nTotal; i++) { (*lppArray)[i] = p; while (*p) p++; p++; } } (*lppArray)[++i] = NULL; return nTotal; }