Blame grp/tst_fgetgrent.c

Packit 6c4009
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
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 <grp.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int errors;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_users (FILE *f, int large_pos, int pos)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  if (pos == large_pos)
Packit 6c4009
    {
Packit 6c4009
      if (large_pos == 3)
Packit 6c4009
	fprintf (f, ":three");
Packit 6c4009
Packit 6c4009
      /* we need more than 2048 bytes for proper testing.  */
Packit 6c4009
      for (i = 0; i < 500; i++)
Packit 6c4009
	fprintf (f, ",user%03d", i);
Packit 6c4009
    }
Packit 6c4009
  fprintf (f, "\n");
Packit 6c4009
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_group (const char *filename, int pos)
Packit 6c4009
{
Packit 6c4009
  FILE *f;
Packit 6c4009
Packit 6c4009
  f = fopen (filename, "w");
Packit 6c4009
  fprintf (f, "one:x:1:one");
Packit 6c4009
  write_users (f, pos, 1);
Packit 6c4009
  fprintf (f, "two:x:2:two");
Packit 6c4009
  write_users (f, pos, 2);
Packit 6c4009
  fprintf (f, "three:x:3");
Packit 6c4009
  write_users (f, pos, 3);
Packit 6c4009
  fclose (f);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_entry (const char *name, gid_t gid, struct group *g)
Packit 6c4009
{
Packit 6c4009
  if (!g)
Packit 6c4009
    {
Packit 6c4009
      printf ("Error: Entry is empty\n");
Packit 6c4009
      errors++;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
Packit 6c4009
    printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
Packit 6c4009
	      name, gid);
Packit 6c4009
      errors++;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_fgetgrent (const char *filename)
Packit 6c4009
{
Packit 6c4009
  struct group *g;
Packit 6c4009
  FILE *f;
Packit 6c4009
Packit 6c4009
  f = fopen (filename,"r");
Packit 6c4009
Packit 6c4009
  g = fgetgrent (f);
Packit 6c4009
  test_entry ("one", 1, g);
Packit 6c4009
  g = fgetgrent (f);
Packit 6c4009
  test_entry ("two", 2, g);
Packit 6c4009
  g = fgetgrent (f);
Packit 6c4009
  test_entry ("three", 3, g);
Packit 6c4009
  fclose (f);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  char file[] = "/tmp/tst_fgetgrent.XXXXXX";
Packit 6c4009
  int fd = mkstemp (file);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("mkstemp failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  close (fd);
Packit 6c4009
  int i = 0;
Packit 6c4009
Packit 6c4009
  if (argc > 1)
Packit 6c4009
    i = atoi (argv[1]);
Packit 6c4009
  if (i > 3)
Packit 6c4009
    i = 3;
Packit 6c4009
  if (i)
Packit 6c4009
    printf ("Large group is group: %d\n", i);
Packit 6c4009
  else
Packit 6c4009
    printf ("Not using a large group\n");
Packit 6c4009
  write_group (file, i);
Packit 6c4009
  test_fgetgrent (file);
Packit 6c4009
Packit 6c4009
  remove (file);
Packit 6c4009
Packit 6c4009
  return (errors != 0);
Packit 6c4009
}