Blame grp/putgrent.c

Packit 6c4009
/* Copyright (C) 1991-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 <nss.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <grp.h>
Packit 6c4009
Packit 6c4009
#define flockfile(s) _IO_flockfile (s)
Packit 6c4009
#define funlockfile(s) _IO_funlockfile (s)
Packit 6c4009
Packit 6c4009
#define _S(x)	x ? x : ""
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
putgrent (const struct group *gr, FILE *stream)
Packit 6c4009
{
Packit 6c4009
  int retval;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL)
Packit 6c4009
      || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name)
Packit 6c4009
      || !__nss_valid_field (gr->gr_passwd)
Packit 6c4009
      || !__nss_valid_list_field (gr->gr_mem))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  flockfile (stream);
Packit 6c4009
Packit 6c4009
  if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-')
Packit 6c4009
    retval = fprintf (stream, "%s:%s::",
Packit 6c4009
		      gr->gr_name, _S (gr->gr_passwd));
Packit 6c4009
  else
Packit 6c4009
    retval = fprintf (stream, "%s:%s:%lu:",
Packit 6c4009
		      gr->gr_name, _S (gr->gr_passwd),
Packit 6c4009
		      (unsigned long int) gr->gr_gid);
Packit 6c4009
  if (__builtin_expect (retval, 0) < 0)
Packit 6c4009
    {
Packit 6c4009
      funlockfile (stream);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (gr->gr_mem != NULL)
Packit 6c4009
    {
Packit 6c4009
      for (size_t i = 0; gr->gr_mem[i] != NULL; i++)
Packit 6c4009
	if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0)
Packit 6c4009
	  {
Packit 6c4009
	    /* What else can we do?  */
Packit 6c4009
	    funlockfile (stream);
Packit 6c4009
	    return -1;
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  retval = fputc_unlocked ('\n', stream);
Packit 6c4009
Packit 6c4009
  funlockfile (stream);
Packit 6c4009
Packit 6c4009
  return retval < 0 ? -1 : 0;
Packit 6c4009
}