]> git.ipfire.org Git - thirdparty/glibc.git/blame - grp/tst_fgetgrent.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / grp / tst_fgetgrent.c
CommitLineData
6d7e8eda 1/* Copyright (C) 1999-2023 Free Software Foundation, Inc.
a7f7e954 2 This file is part of the GNU C Library.
a7f7e954
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
a7f7e954
UD
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
a7f7e954 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
a7f7e954
UD
17
18#include <grp.h>
19#include <stdio.h>
f535dd02 20#include <stdlib.h>
a7f7e954
UD
21#include <string.h>
22#include <sys/types.h>
5c112f1b 23#include <unistd.h>
a7f7e954
UD
24
25static int errors;
26
27static void
28write_users (FILE *f, int large_pos, int pos)
29{
30 int i;
31
32 if (pos == large_pos)
33 {
e1c6ee83
UD
34 if (large_pos == 3)
35 fprintf (f, ":three");
36
a7f7e954
UD
37 /* we need more than 2048 bytes for proper testing. */
38 for (i = 0; i < 500; i++)
e1c6ee83 39 fprintf (f, ",user%03d", i);
a7f7e954
UD
40 }
41 fprintf (f, "\n");
42
43}
44
45static void
46write_group (const char *filename, int pos)
47{
48 FILE *f;
4707ba3d 49
a7f7e954 50 f = fopen (filename, "w");
f535dd02 51 fprintf (f, "one:x:1:one");
a7f7e954 52 write_users (f, pos, 1);
f535dd02 53 fprintf (f, "two:x:2:two");
a7f7e954 54 write_users (f, pos, 2);
f535dd02 55 fprintf (f, "three:x:3");
a7f7e954
UD
56 write_users (f, pos, 3);
57 fclose (f);
58}
59
60static void
61test_entry (const char *name, gid_t gid, struct group *g)
62{
63 if (!g)
64 {
65 printf ("Error: Entry is empty\n");
66 errors++;
67 return;
68 }
4707ba3d 69
a7f7e954
UD
70 if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
71 printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
72 else
73 {
74 printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
75 name, gid);
76 errors++;
77 }
78}
79
80
81static void
82test_fgetgrent (const char *filename)
83{
84 struct group *g;
85 FILE *f;
86
f535dd02 87 f = fopen (filename,"r");
a7f7e954
UD
88
89 g = fgetgrent (f);
f535dd02 90 test_entry ("one", 1, g);
a7f7e954 91 g = fgetgrent (f);
f535dd02 92 test_entry ("two", 2, g);
a7f7e954 93 g = fgetgrent (f);
f535dd02 94 test_entry ("three", 3, g);
a7f7e954
UD
95 fclose (f);
96}
97
4707ba3d 98
a7f7e954 99int
f535dd02 100main (int argc, char *argv[])
a7f7e954 101{
5c112f1b
JM
102 char file[] = "/tmp/tst_fgetgrent.XXXXXX";
103 int fd = mkstemp (file);
104 if (fd == -1)
105 {
106 printf ("mkstemp failed: %m\n");
107 return 1;
108 }
109 close (fd);
f535dd02
UD
110 int i = 0;
111
112 if (argc > 1)
113 i = atoi (argv[1]);
114 if (i > 3)
115 i = 3;
116 if (i)
117 printf ("Large group is group: %d\n", i);
118 else
119 printf ("Not using a large group\n");
120 write_group (file, i);
121 test_fgetgrent (file);
a7f7e954 122
a7f7e954 123 remove (file);
4707ba3d 124
a7f7e954
UD
125 return (errors != 0);
126}