Update.
[platform/upstream/glibc.git] / grp / tst_fgetgrent.c
1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <grp.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/types.h>
24
25 static int errors;
26
27 static void
28 write_users (FILE *f, int large_pos, int pos)
29 {
30   int i;
31
32   if (pos == large_pos)
33     {
34       /* we need more than 2048 bytes for proper testing.  */
35       for (i = 0; i < 500; i++)
36         fprintf (f, ",user%03d", i);
37     }
38   fprintf (f, "\n");
39
40 }
41
42 static void
43 write_group (const char *filename, int pos)
44 {
45   FILE *f;
46
47   f = fopen (filename, "w");
48   fprintf (f, "one:x:0:one");
49   write_users (f, pos, 1);
50   fprintf (f, "two:x:1:two");
51   write_users (f, pos, 2);
52   fprintf (f, "three:x:2");
53   write_users (f, pos, 3);
54   fclose (f);
55 }
56
57 static void
58 test_entry (const char *name, gid_t gid, struct group *g)
59 {
60   if (!g)
61     {
62       printf ("Error: Entry is empty\n");
63       errors++;
64       return;
65     }
66
67   if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
68     printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
69   else
70     {
71       printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
72               name, gid);
73       errors++;
74     }
75 }
76
77
78 static void
79 test_fgetgrent (const char *filename)
80 {
81   struct group *g;
82   FILE *f;
83
84   f = fopen (filename, "r");
85
86   g = fgetgrent (f);
87   test_entry ("one", 0, g);
88   g = fgetgrent (f);
89   test_entry ("two", 1, g);
90   g = fgetgrent (f);
91   test_entry ("three", 2, g);
92   fclose (f);
93 }
94
95
96 int
97 main (void)
98 {
99   char *file = tmpnam (NULL);
100   int i;
101
102   for (i = 0; i < 4; i++)
103     {
104       printf ("Pass %d\n", i);
105       write_group (file, i);
106       test_fgetgrent (file);
107     }
108   remove (file);
109
110   return (errors != 0);
111 }