int LshttpdMain::getServerRoot( int argc, char * argv[] ) { char achServerRoot[MAX_PATH_LEN]; char pServerRootEnv[2][20] = {"LSWS_HOME", "LSHTTPD_HOME"}; for( int i = 0; i < 2; ++i ) { const char * pRoot = getenv( pServerRootEnv[i] ); if ( pRoot ) { if ( testServerRoot( pRoot ) == 0 ) { return 0; } } } if ( getServerRootFromExecutablePath( argv[0], achServerRoot, MAX_PATH_LEN ) == 0 ) { if ( testServerRoot( achServerRoot ) == 0 ) { return 0; } } if ( guessCommonServerRoot() == 0 ) return 0; return -1; }
int LshttpdMain::guessCommonServerRoot() { const char *pServerRoots[] = { NULL, "/usr/local/", "/opt/", "/usr/", "/var/", "/home/", "/usr/share/", "/usr/local/share/" }; const char *pServerDirs[] = { "lsws/", "lshttpd/" }; char *pHome = getenv("HOME"); pServerRoots[0] = pHome; char achBuf[MAX_PATH_LEN]; for (size_t i = 0; i < sizeof(pServerRoots) / sizeof(char *); ++i) { if (!pServerRoots[i]) continue; strcpy(achBuf, pServerRoots[i]); for (size_t j = 0; j < sizeof(pServerDirs) / sizeof(char *); ++j) { strcat(achBuf, pServerDirs[j]); if (testServerRoot(achBuf) == 0) return 0; } } return LS_FAIL; }