void ni_set_prop(ni_proplist * pl_p, ni_name prop, ni_name value, boolean_t * modified) { ni_index where; where = ni_proplist_match(*pl_p, prop, NULL); if (where != NI_INDEX_NULL) { if (value != NULL && where == ni_proplist_match(*pl_p, prop, value)) { return; /* already set */ } ni_proplist_delete(pl_p, where); } ni_proplist_insertprop(pl_p, prop, value, where); if (modified) *modified = TRUE; return; }
boolean_t PLCache_read(PLCache_t * cache, const char * filename) { FILE * file = NULL; int line_number = 0; char line[1024]; ni_proplist pl; enum { nowhere_e, start_e, body_e, end_e } where = nowhere_e; NI_INIT(&pl); file = fopen(filename, "r"); if (file == NULL) { perror(filename); goto failed; } while (1) { if (my_fgets(line, sizeof(line), file) != line) { if (where == start_e || where == body_e) { fprintf(stderr, "file ends prematurely\n"); } break; } line_number++; if (strcmp(line, "{\n") == 0) { if (where != end_e && where != nowhere_e) { fprintf(stderr, "unexpected '{' at line %d\n", line_number); goto failed; } where = start_e; } else if (strcmp(line, "}\n") == 0) { if (where != start_e && where != body_e) { fprintf(stderr, "unexpected '}' at line %d\n", line_number); goto failed; } if (pl.nipl_len > 0) { PLCache_append(cache, PLCacheEntry_create(pl)); ni_proplist_free(&pl); } where = end_e; } else { char propname[128]; char propval[768] = ""; int len = strlen(line); char * sep = strchr(line, '='); int whitespace_len = strspn(line, " \t\n"); if (whitespace_len == len) { continue; } if (sep) { int nlen = (sep - line) - whitespace_len; int vlen = len - whitespace_len - nlen - 2; if (nlen >= sizeof(propname)) { fprintf(stderr, "property name truncated to %d bytes at line %d\n", (int)sizeof(propname) - 1, line_number); nlen = sizeof(propname) - 1; } if (vlen >= sizeof(propval)) { fprintf(stderr, "value truncated to %d bytes at line %d\n", (int)sizeof(propval) - 1, line_number); vlen = sizeof(propval) - 1; } strncpy(propname, line + whitespace_len, nlen); propname[nlen] = '\0'; strncpy(propval, sep + 1, vlen); propval[vlen] = '\0'; ni_proplist_insertprop(&pl, propname, propval, NI_INDEX_NULL); } else { int nlen = len - whitespace_len - 1; if (nlen >= sizeof(propname)) { fprintf(stderr, "property name truncated to %d bytes at line %d\n", (int)sizeof(propname) - 1, line_number); nlen = sizeof(propname) - 1; } strncpy(propname, line + whitespace_len, nlen); propname[nlen] = '\0'; ni_proplist_insertprop(&pl, propname, NULL, NI_INDEX_NULL); } where = body_e; } } failed: if (file) fclose(file); ni_proplist_free(&pl); return (TRUE); }