static void FreeNsswitchEntryContents(NsswitchEntry *entry) { size_t i; for(i = 0; i < entry->modules.size; i++) { CTFreeParseTokenContents(GetEntryModule(entry, i)); } CTArrayFree(&entry->modules); CT_SAFE_FREE_STRING(entry->leadingWhiteSpace); CT_SAFE_FREE_STRING(entry->comment); CTFreeParseTokenContents(&entry->name); }
static DWORD RemoveModule(NsswitchConf *conf, int line, int moduleIndex) { DWORD ceError = ERROR_SUCCESS; NsswitchEntry *lineObj = (NsswitchEntry *)GetEntry(conf, line); CTParseToken *beforeModule = NULL, *afterModule = NULL; CTParseToken *removeModule; removeModule = (CTParseToken *)lineObj->modules.data + moduleIndex; if(moduleIndex - 1 >= 0) beforeModule = (CTParseToken *)lineObj->modules.data + moduleIndex - 1; if(moduleIndex + 1 < lineObj->modules.size) afterModule = (CTParseToken *)lineObj->modules.data + moduleIndex + 1; if(afterModule == NULL && beforeModule != NULL) { /* Since the last module is being removed, move the trailingSeparator * to the previous module */ CT_SAFE_FREE_STRING(beforeModule->trailingSeparator); beforeModule->trailingSeparator = removeModule->trailingSeparator; removeModule->trailingSeparator = NULL; } CTFreeParseTokenContents(removeModule); GCE(ceError = CTArrayRemove(&lineObj->modules, moduleIndex, sizeof(CTParseToken), 1)); conf->modified = 1; cleanup: return ceError; }
void CTFreeParseToken(CTParseToken **token) { if(*token != NULL) { CTFreeParseTokenContents(*token); CT_SAFE_FREE_MEMORY(*token); } }
static DWORD AddFormattedLine(NsswitchConf *conf, const char *filename, const char *linestr, const char **endptr) { DWORD ceError = ERROR_SUCCESS; NsswitchEntry lineObj; const char *pos = linestr; const char *token_start = NULL; CTParseToken token; memset(&lineObj, 0, sizeof(lineObj)); memset(&token, 0, sizeof(token)); /* Find the leading whitespace in the line */ token_start = pos; while(isblank(*pos)) pos++; GCE(ceError = CTStrndup(token_start, pos - token_start, &lineObj.leadingWhiteSpace)); /* Read the name of the entry and attach its trailing : or = */ GCE(ceError = CTReadToken(&pos, &lineObj.name, "=: \t", ";#\r\n", "")); /* Create an array of the modules for this entry */ while(strchr("\r\n;#", *pos) == NULL) { GCE(ceError = CTReadToken(&pos, &token, ", \t", ";#\r\n", "")); GCE(ceError = CTArrayAppend(&lineObj.modules, sizeof(CTParseToken), &token, 1)); memset(&token, 0, sizeof(token)); } /*Read the comment, if there is one*/ token_start = pos; while(strchr("\r\n", *pos) == NULL) pos++; if(pos != token_start) GCE(ceError = CTStrndup(token_start, pos - token_start, &lineObj.comment)); GCE(ceError = CTArrayAppend(&conf->lines, sizeof(lineObj), &lineObj, 1)); memset(&lineObj, 0, sizeof(lineObj)); if(endptr != NULL) *endptr = pos; cleanup: FreeNsswitchEntryContents(&lineObj); CTFreeParseTokenContents(&token); return ceError; }
DWORD CTReadToken(const char **pos, CTParseToken *store, const char *includeSeparators, const char *excludeSeparators, const char *trimBack) { DWORD ceError = ERROR_SUCCESS; char const * token_start = *pos; char const * white_start, * white_end; memset(store, 0, sizeof(*store)); while(**pos != '\0' && strchr(includeSeparators, **pos) == NULL && strchr(excludeSeparators, **pos) == NULL) { (*pos)++; } white_start = *pos; while (**pos != '\0' && strchr(includeSeparators, **pos) != NULL) { (*pos)++; } white_end = *pos; while (white_start > token_start && white_start[-1] != '\0' && strchr(trimBack, white_start[-1]) != NULL) { white_start--; } if(token_start != white_start) { ceError = CTStrndup(token_start, white_start - token_start, &store->value); BAIL_ON_CENTERIS_ERROR(ceError); } if(white_start != white_end) { ceError = CTStrndup(white_start, white_end - white_start, &store->trailingSeparator); BAIL_ON_CENTERIS_ERROR(ceError); } error: if (ceError) CTFreeParseTokenContents(store); return ceError; }
DWORD CTCopyTokenContents(CTParseToken *dest, const CTParseToken *source) { DWORD ceError = ERROR_SUCCESS; memset(dest, 0, sizeof(*dest)); if(source->value != NULL) { ceError = CTDupOrNullStr(source->value, &dest->value); BAIL_ON_CENTERIS_ERROR(ceError); } if(source->trailingSeparator != NULL) { ceError = CTDupOrNullStr(source->trailingSeparator, &dest->trailingSeparator); BAIL_ON_CENTERIS_ERROR(ceError); } error: if(ceError) CTFreeParseTokenContents(dest); return ceError; }
static DWORD InsertModule(NsswitchConf *conf, const DistroInfo *distro, int line, int insertIndex, const char *name) { DWORD ceError = ERROR_SUCCESS; NsswitchEntry *lineObj = (NsswitchEntry *)GetEntry(conf, line); CTParseToken *beforeModule = NULL, *afterModule = NULL; CTParseToken addModule; memset(&addModule, 0, sizeof(addModule)); if(insertIndex == -1) insertIndex = lineObj->modules.size; GCE(ceError = CTStrdup(name, &addModule.value)); if(insertIndex - 1 >= 0) beforeModule = (CTParseToken *)lineObj->modules.data + insertIndex - 1; if(insertIndex < lineObj->modules.size) afterModule = (CTParseToken *)lineObj->modules.data + insertIndex; if(beforeModule != NULL) { /* Copy the separator from the previous module */ GCE(ceError = CTDupOrNullStr(beforeModule->trailingSeparator, &addModule.trailingSeparator)); if(afterModule == NULL) { /*This is the last module. Put in the correct separator after the * previous module */ CT_SAFE_FREE_STRING(beforeModule->trailingSeparator); GCE(ceError = CTStrdup(GetModuleSeparator(conf, distro), &beforeModule->trailingSeparator)); } } else { if(afterModule == NULL) { //This is the last module if(lineObj->comment == NULL) { //Leave the trailingSeparator as NULL } else { GCE(ceError = CTStrdup(" ", &addModule.trailingSeparator)); } } else { //This is the first module. Add the appropriate separator to //distinguish it from the next module. GCE(ceError = CTStrdup(GetModuleSeparator(conf, distro), &addModule.trailingSeparator)); } } GCE(ceError = CTArrayInsert(&lineObj->modules, insertIndex, sizeof(addModule), &addModule, 1)); memset(&addModule, 0, sizeof(addModule)); conf->modified = 1; cleanup: CTFreeParseTokenContents(&addModule); return ceError; }