glibc/grp/tst_fgetgrent.c
1999-06-08 11:44:48 +00:00

112 lines
2.5 KiB
C

/* Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
static int errors;
static void
write_users (FILE *f, int large_pos, int pos)
{
int i;
if (pos == large_pos)
{
/* we need more than 2048 bytes for proper testing. */
for (i = 0; i < 500; i++)
fprintf (f, ",user%03d", i);
}
fprintf (f, "\n");
}
static void
write_group (const char *filename, int pos)
{
FILE *f;
f = fopen (filename, "w");
fprintf (f, "one:x:0:one");
write_users (f, pos, 1);
fprintf (f, "two:x:1:two");
write_users (f, pos, 2);
fprintf (f, "three:x:2");
write_users (f, pos, 3);
fclose (f);
}
static void
test_entry (const char *name, gid_t gid, struct group *g)
{
if (!g)
{
printf ("Error: Entry is empty\n");
errors++;
return;
}
if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
else
{
printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
name, gid);
errors++;
}
}
static void
test_fgetgrent (const char *filename)
{
struct group *g;
FILE *f;
f = fopen (filename, "r");
g = fgetgrent (f);
test_entry ("one", 0, g);
g = fgetgrent (f);
test_entry ("two", 1, g);
g = fgetgrent (f);
test_entry ("three", 2, g);
fclose (f);
}
int
main (void)
{
char *file = tmpnam (NULL);
int i;
for (i = 0; i < 4; i++)
{
printf ("Pass %d\n", i);
write_group (file, i);
test_fgetgrent (file);
}
remove (file);
return (errors != 0);
}