Bug 25819: Update to Unicode 13.0.0
[glibc.git] / grp / tst_fgetgrent.c
blobfc97e42de1ac994376f0fdfef9200196c2c7cf6b
1 /* Copyright (C) 1999-2020 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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <grp.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <unistd.h>
26 static int errors;
28 static void
29 write_users (FILE *f, int large_pos, int pos)
31 int i;
33 if (pos == large_pos)
35 if (large_pos == 3)
36 fprintf (f, ":three");
38 /* we need more than 2048 bytes for proper testing. */
39 for (i = 0; i < 500; i++)
40 fprintf (f, ",user%03d", i);
42 fprintf (f, "\n");
46 static void
47 write_group (const char *filename, int pos)
49 FILE *f;
51 f = fopen (filename, "w");
52 fprintf (f, "one:x:1:one");
53 write_users (f, pos, 1);
54 fprintf (f, "two:x:2:two");
55 write_users (f, pos, 2);
56 fprintf (f, "three:x:3");
57 write_users (f, pos, 3);
58 fclose (f);
61 static void
62 test_entry (const char *name, gid_t gid, struct group *g)
64 if (!g)
66 printf ("Error: Entry is empty\n");
67 errors++;
68 return;
71 if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
72 printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
73 else
75 printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
76 name, gid);
77 errors++;
82 static void
83 test_fgetgrent (const char *filename)
85 struct group *g;
86 FILE *f;
88 f = fopen (filename,"r");
90 g = fgetgrent (f);
91 test_entry ("one", 1, g);
92 g = fgetgrent (f);
93 test_entry ("two", 2, g);
94 g = fgetgrent (f);
95 test_entry ("three", 3, g);
96 fclose (f);
101 main (int argc, char *argv[])
103 char file[] = "/tmp/tst_fgetgrent.XXXXXX";
104 int fd = mkstemp (file);
105 if (fd == -1)
107 printf ("mkstemp failed: %m\n");
108 return 1;
110 close (fd);
111 int i = 0;
113 if (argc > 1)
114 i = atoi (argv[1]);
115 if (i > 3)
116 i = 3;
117 if (i)
118 printf ("Large group is group: %d\n", i);
119 else
120 printf ("Not using a large group\n");
121 write_group (file, i);
122 test_fgetgrent (file);
124 remove (file);
126 return (errors != 0);