Example #1
0
/* Write an entry to the given stream.
   This must know the format of the group file.  */
int
putsgent (const struct sgrp *g, FILE *stream)
{
  int errors = 0;

  if (g->sg_namp == NULL || !__nss_valid_field (g->sg_namp)
      || !__nss_valid_field (g->sg_passwd)
      || !__nss_valid_list_field (g->sg_adm)
      || !__nss_valid_list_field (g->sg_mem))
    {
      __set_errno (EINVAL);
      return -1;
    }

  _IO_flockfile (stream);

  if (fprintf (stream, "%s:%s:", g->sg_namp, _S (g->sg_passwd)) < 0)
    ++errors;

  bool first = true;
  char **sp = g->sg_adm;
  if (sp != NULL)
    while (*sp != NULL)
      {
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
	  {
	    ++errors;
	    break;
	  }
	first = false;
      }
  if (putc_unlocked (':', stream) == EOF)
    ++errors;

  first = true;
  sp = g->sg_mem;
  if (sp != NULL)
    while (*sp != NULL)
      {
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
	  {
	    ++errors;
	    break;
	  }
	first = false;
      }
  if (putc_unlocked ('\n', stream) == EOF)
    ++errors;

  _IO_funlockfile (stream);

  return errors ? -1 : 0;
}
Example #2
0
/* Write an entry to the given stream.
   This must know the format of the group file.  */
int
putgrent (const struct group *gr, FILE *stream)
{
  int retval;

  if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL)
      || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name)
      || !__nss_valid_field (gr->gr_passwd)
      || !__nss_valid_list_field (gr->gr_mem))
    {
      __set_errno (EINVAL);
      return -1;
    }

  flockfile (stream);

  if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-')
    retval = fprintf (stream, "%s:%s::",
		      gr->gr_name, _S (gr->gr_passwd));
  else
    retval = fprintf (stream, "%s:%s:%lu:",
		      gr->gr_name, _S (gr->gr_passwd),
		      (unsigned long int) gr->gr_gid);
  if (__builtin_expect (retval, 0) < 0)
    {
      funlockfile (stream);
      return -1;
    }

  if (gr->gr_mem != NULL)
    {
      for (size_t i = 0; gr->gr_mem[i] != NULL; i++)
	if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0)
	  {
	    /* What else can we do?  */
	    funlockfile (stream);
	    return -1;
	  }
    }

  retval = fputc_unlocked ('\n', stream);

  funlockfile (stream);

  return retval < 0 ? -1 : 0;
}