ref: 12a0ed55204affb277def2a73c1769bd76105ed4
dir: /group.c/
#include <u.h> #include <libc.h> #include "group.h" int loadgroups(Groups *gs, char *raw) { Group *g; char *m, **memb, *s, *e, *a[5], *ide; int line, n; vlong id; memset(gs, 0, sizeof(*gs)); if((gs->raw = strdup(raw)) == nil) goto error; line = 1; for(s = gs->raw; *s; s = e+1, line++){ if((e = strchr(s, '\n')) != nil) *e = 0; if((n = getfields(s, a, nelem(a), 1, ":")) >= 3 && strlen(a[0]) > 0 && strlen(a[2]) > 0){ if((id = strtoll(a[2], &ide, 0)) < 0 || id > 0xffffffff || *ide != 0){ werrstr("invalid uid: %s", a[2]); goto error; } if((g = realloc(gs->g, (gs->ng+1)*sizeof(*g))) == nil) goto error; gs->g = g; g += gs->ng++; g->id = id; g->name = a[0]; g->memb = nil; g->nmemb = 0; for(m = a[3]; n > 3 && *m; *m++ = 0){ if((memb = realloc(g->memb, (g->nmemb+1)*sizeof(*g->memb))) == nil) goto error; g->memb = memb; memb += g->nmemb++; *memb = m; if((m = strchr(m, ',')) == nil) break; } }else{ werrstr("line %d: invalid record", line); goto error; } if(e == nil) break; } return 0; error: werrstr("togroups: %r"); freegroups(gs); return -1; } void freegroups(Groups *gs) { int i; for(i = 0; i < gs->ng; i++) free(gs->g[i].memb); free(gs->g); free(gs->raw); } Group * findgroup(Groups *gs, char *name, u32int *id) { int i; Group *g; g = gs->g; for(i = 0; i < gs->ng; i++, g++){ if(strcmp(g->name, name) == 0){ if(id != nil) *id = g->id; return g; } } if(id != nil) *id = ~0; return nil; } Group * findgroupid(Groups *gs, u32int id) { int i; Group *g; g = gs->g; for(i = 0; i < gs->ng; i++, g++){ if(g->id == id) return g; } return nil; }