int main(void) { char *strGrp; int testNdx; struct group *dupGrp; struct group *scanGrp; struct group origGrp; /* Setup. */ printf("1..4\n"); testNdx = 0; /* Manually build a group using static values. */ build_grp(&origGrp); /* Copy the group. */ testNdx++; if ((dupGrp = gr_dup(&origGrp)) == NULL) printf("not "); printf("ok %d - %s\n", testNdx, "gr_dup"); /* Compare the original and duplicate groups. */ testNdx++; if (! gr_equal(&origGrp, dupGrp)) printf("not "); printf("ok %d - %s\n", testNdx, "gr_equal"); /* Create group string from the duplicate group structure. */ testNdx++; strGrp = gr_make(dupGrp); if (strcmp(strGrp, origStrGrp) != 0) printf("not "); printf("ok %d - %s\n", testNdx, "gr_make"); /* * Create group structure from string and compare it to the original * group structure. */ testNdx++; if ((scanGrp = gr_scan(strGrp)) == NULL || ! gr_equal(&origGrp, scanGrp)) printf("not "); printf("ok %d - %s\n", testNdx, "gr_scan"); /* Clean up. */ free(scanGrp); free(strGrp); free(dupGrp); exit(EXIT_SUCCESS); }
/* * Create a struct group from a line. */ struct group * gr_scan(const char *line) { struct group gr; char *line_copy; struct group *new_gr; if ((line_copy = strdup(line)) == NULL) return (NULL); if (!__gr_scan(line_copy, &gr)) { free(line_copy); return (NULL); } new_gr = gr_dup(&gr); free(line_copy); if (gr.gr_mem != NULL) free(gr.gr_mem); return (new_gr); }
static int gr_update(struct group * grp, char const * group) { int pfd, tfd; struct group *gr = NULL; struct group *old_gr = NULL; if (grp != NULL) gr = gr_dup(grp); if (group != NULL) old_gr = GETGRNAM(group); if (gr_init(conf.etcpath, NULL)) err(1, "gr_init()"); if ((pfd = gr_lock()) == -1) { gr_fini(); err(1, "gr_lock()"); } if ((tfd = gr_tmp(-1)) == -1) { gr_fini(); err(1, "gr_tmp()"); } if (gr_copy(pfd, tfd, gr, old_gr) == -1) { gr_fini(); close(tfd); err(1, "gr_copy()"); } fsync(tfd); close(tfd); if (gr_mkdb() == -1) { gr_fini(); err(1, "gr_mkdb()"); } free(gr); gr_fini(); return 0; }