VALUE group_putsgent(VALUE self, VALUE io) { #ifdef GSHADOW struct sgrp sgroup, *tmp_sgrp; VALUE path; long i = 0; Check_EU_Type(self, rb_cGshadow); Check_Writes(io, FMODE_WRITABLE); path = RFILE_PATH(io); rewind(RFILE_FPTR(io)); SGRP_NAME(&sgroup) = RSTRING_PTR(rb_ivar_get(self, id_name)); while ( (tmp_sgrp = fgetsgent(RFILE_FPTR(io))) ) if ( !strcmp(SGRP_NAME(tmp_sgrp), SGRP_NAME(&sgroup)) ) rb_raise(rb_eArgError, "%s is already mentioned in %s:%ld", RSTRING_PTR(rb_ivar_get(self, id_name)), StringValuePtr(path), ++i ); sgroup.sg_passwd = RSTRING_PTR(rb_ivar_get(self, id_passwd)); sgroup.sg_adm = setup_char_members( rb_iv_get(self,"@admins") ); sgroup.sg_mem = setup_char_members( rb_iv_get(self, "@members") ); if ( putsgent(&sgroup,RFILE_FPTR(io)) ) eu_errno(RFILE_PATH(io)); free_char_members(sgroup.sg_adm, RARRAY_LEN( rb_iv_get(self, "@admins") )); free_char_members(sgroup.sg_mem, RARRAY_LEN( rb_iv_get(self, "@members") )); return Qtrue; #else return Qnil; #endif }
static int do_test (void) { FILE *fp = tmpfile (); if (fp == NULL) { puts ("cannot open temporary file"); return 1; } for (size_t i = 0; i < ndata; ++i) if (putsgent (&data[i], fp) != 0) { printf ("putsgent call %zu failed\n", i + 1); return 1; } rewind (fp); int result = 0; int seen = -1; struct sgrp *g; while ((g = fgetsgent (fp)) != NULL) { ++seen; if (strcmp (g->sg_namp, data[seen].sg_namp) != 0) { printf ("sg_namp of entry %d does not match: %s vs %s\n", seen + 1, g->sg_namp, data[seen].sg_namp); result = 1; } if (strcmp (g->sg_passwd, data[seen].sg_passwd) != 0) { printf ("sg_passwd of entry %d does not match: %s vs %s\n", seen + 1, g->sg_passwd, data[seen].sg_passwd); result = 1; } if (g->sg_adm == NULL) { printf ("sg_adm of entry %d is NULL\n", seen + 1); result = 1; } else { int i = 1; char **sp1 = g->sg_adm; char **sp2 = data[seen].sg_adm; while (*sp1 != NULL && *sp2 != NULL) { if (strcmp (*sp1, *sp2) != 0) { printf ("sg_adm[%d] of entry %d does not match: %s vs %s\n", i, seen + 1, *sp1, *sp2); result = 1; } ++sp1; ++sp2; ++i; } if (*sp1 == NULL && *sp2 != NULL) { printf ("sg_adm of entry %d has too few entries\n", seen + 1); result = 1; } else if (*sp1 != NULL && *sp2 == NULL) { printf ("sg_adm of entry %d has too many entries\n", seen + 1); result = 1; } } if (g->sg_mem == NULL) { printf ("sg_mem of entry %d is NULL\n", seen + 1); result = 1; } else { int i = 1; char **sp1 = g->sg_mem; char **sp2 = data[seen].sg_mem; while (*sp1 != NULL && *sp2 != NULL) { if (strcmp (*sp1, *sp2) != 0) { printf ("sg_mem[%d] of entry %d does not match: %s vs %s\n", i, seen + 1, *sp1, *sp2); result = 1; } ++sp1; ++sp2; ++i; } if (*sp1 == NULL && *sp2 != NULL) { printf ("sg_mem of entry %d has too few entries\n", seen + 1); result = 1; } else if (*sp1 != NULL && *sp2 == NULL) { printf ("sg_mem of entry %d has too many entries\n", seen + 1); result = 1; } } } fclose (fp); return result; }
static void check (struct sgrp e, const char *expected) { char *buf; size_t buf_size; FILE *f = open_memstream (&buf, &buf_size); if (f == NULL) { printf ("open_memstream: %m\n"); errors = true; return; } int ret = putsgent (&e, f); if (expected == NULL) { if (ret == -1) { if (errno != EINVAL) { printf ("putsgent: unexpected error code: %m\n"); errors = true; } } else { printf ("putsgent: unexpected success (\"%s\")\n", e.sg_namp); errors = true; } } else { /* Expect success. */ size_t expected_length = strlen (expected); if (ret == 0) { long written = ftell (f); if (written <= 0 || fflush (f) < 0) { printf ("stream error: %m\n"); errors = true; } else if (buf[written - 1] != '\n') { printf ("FAILED: \"%s\" without newline\n", expected); errors = true; } else if (strncmp (buf, expected, written - 1) != 0 || written - 1 != expected_length) { printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n", buf, written - 1, expected, expected_length); errors = true; } } else { printf ("FAILED: putsgent (expected \"%s\"): %m\n", expected); errors = true; } } fclose (f); free (buf); }
static int gshadow_put (const void *ent, FILE * file) { const struct sgrp *sg = ent; return (putsgent (sg, file) == -1) ? -1 : 0; }