Blame gshadow/putsgent.c

Packit 6c4009
/* Copyright (C) 2009-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <gshadow.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
Packit 6c4009
#define _S(x)	x ? x : ""
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Write an entry to the given stream.
Packit 6c4009
   This must know the format of the group file.  */
Packit 6c4009
int
Packit 6c4009
putsgent (const struct sgrp *g, FILE *stream)
Packit 6c4009
{
Packit 6c4009
  int errors = 0;
Packit 6c4009
Packit 6c4009
  if (g->sg_namp == NULL || !__nss_valid_field (g->sg_namp)
Packit 6c4009
      || !__nss_valid_field (g->sg_passwd)
Packit 6c4009
      || !__nss_valid_list_field (g->sg_adm)
Packit 6c4009
      || !__nss_valid_list_field (g->sg_mem))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _IO_flockfile (stream);
Packit 6c4009
Packit 6c4009
  if (fprintf (stream, "%s:%s:", g->sg_namp, _S (g->sg_passwd)) < 0)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  bool first = true;
Packit 6c4009
  char **sp = g->sg_adm;
Packit 6c4009
  if (sp != NULL)
Packit 6c4009
    while (*sp != NULL)
Packit 6c4009
      {
Packit 6c4009
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
Packit 6c4009
	  {
Packit 6c4009
	    ++errors;
Packit 6c4009
	    break;
Packit 6c4009
	  }
Packit 6c4009
	first = false;
Packit 6c4009
      }
Packit 6c4009
  if (putc_unlocked (':', stream) == EOF)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  first = true;
Packit 6c4009
  sp = g->sg_mem;
Packit 6c4009
  if (sp != NULL)
Packit 6c4009
    while (*sp != NULL)
Packit 6c4009
      {
Packit 6c4009
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
Packit 6c4009
	  {
Packit 6c4009
	    ++errors;
Packit 6c4009
	    break;
Packit 6c4009
	  }
Packit 6c4009
	first = false;
Packit 6c4009
      }
Packit 6c4009
  if (putc_unlocked ('\n', stream) == EOF)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  _IO_funlockfile (stream);
Packit 6c4009
Packit 6c4009
  return errors ? -1 : 0;
Packit 6c4009
}