]> git.ipfire.org Git - thirdparty/glibc.git/blame - grp/tst_fgetgrent.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / grp / tst_fgetgrent.c
CommitLineData
04277e02 1/* Copyright (C) 1999-2019 Free Software Foundation, Inc.
a7f7e954
UD
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
41bdb6e2
AJ
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.
a7f7e954
UD
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
41bdb6e2 13 Lesser General Public License for more details.
a7f7e954 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
a7f7e954
UD
18
19#include <grp.h>
20#include <stdio.h>
f535dd02 21#include <stdlib.h>
a7f7e954
UD
22#include <string.h>
23#include <sys/types.h>
5c112f1b 24#include <unistd.h>
a7f7e954
UD
25
26static int errors;
27
28static void
29write_users (FILE *f, int large_pos, int pos)
30{
31 int i;
32
33 if (pos == large_pos)
34 {
e1c6ee83
UD
35 if (large_pos == 3)
36 fprintf (f, ":three");
37
a7f7e954
UD
38 /* we need more than 2048 bytes for proper testing. */
39 for (i = 0; i < 500; i++)
e1c6ee83 40 fprintf (f, ",user%03d", i);
a7f7e954
UD
41 }
42 fprintf (f, "\n");
43
44}
45
46static void
47write_group (const char *filename, int pos)
48{
49 FILE *f;
4707ba3d 50
a7f7e954 51 f = fopen (filename, "w");
f535dd02 52 fprintf (f, "one:x:1:one");
a7f7e954 53 write_users (f, pos, 1);
f535dd02 54 fprintf (f, "two:x:2:two");
a7f7e954 55 write_users (f, pos, 2);
f535dd02 56 fprintf (f, "three:x:3");
a7f7e954
UD
57 write_users (f, pos, 3);
58 fclose (f);
59}
60
61static void
62test_entry (const char *name, gid_t gid, struct group *g)
63{
64 if (!g)
65 {
66 printf ("Error: Entry is empty\n");
67 errors++;
68 return;
69 }
4707ba3d 70
a7f7e954
UD
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
74 {
75 printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
76 name, gid);
77 errors++;
78 }
79}
80
81
82static void
83test_fgetgrent (const char *filename)
84{
85 struct group *g;
86 FILE *f;
87
f535dd02 88 f = fopen (filename,"r");
a7f7e954
UD
89
90 g = fgetgrent (f);
f535dd02 91 test_entry ("one", 1, g);
a7f7e954 92 g = fgetgrent (f);
f535dd02 93 test_entry ("two", 2, g);
a7f7e954 94 g = fgetgrent (f);
f535dd02 95 test_entry ("three", 3, g);
a7f7e954
UD
96 fclose (f);
97}
98
4707ba3d 99
a7f7e954 100int
f535dd02 101main (int argc, char *argv[])
a7f7e954 102{
5c112f1b
JM
103 char file[] = "/tmp/tst_fgetgrent.XXXXXX";
104 int fd = mkstemp (file);
105 if (fd == -1)
106 {
107 printf ("mkstemp failed: %m\n");
108 return 1;
109 }
110 close (fd);
f535dd02
UD
111 int i = 0;
112
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);
a7f7e954 123
a7f7e954 124 remove (file);
4707ba3d 125
a7f7e954
UD
126 return (errors != 0);
127}