lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1999-2015 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 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. |
| 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 | Lesser General Public License for more details. |
| 14 | |
| 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <grp.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.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 | if (large_pos == 3) |
| 35 | fprintf (f, ":three"); |
| 36 | |
| 37 | /* we need more than 2048 bytes for proper testing. */ |
| 38 | for (i = 0; i < 500; i++) |
| 39 | fprintf (f, ",user%03d", i); |
| 40 | } |
| 41 | fprintf (f, "\n"); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | static void |
| 46 | write_group (const char *filename, int pos) |
| 47 | { |
| 48 | FILE *f; |
| 49 | |
| 50 | f = fopen (filename, "w"); |
| 51 | fprintf (f, "one:x:1:one"); |
| 52 | write_users (f, pos, 1); |
| 53 | fprintf (f, "two:x:2:two"); |
| 54 | write_users (f, pos, 2); |
| 55 | fprintf (f, "three:x:3"); |
| 56 | write_users (f, pos, 3); |
| 57 | fclose (f); |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | test_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 | } |
| 69 | |
| 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 | |
| 81 | static void |
| 82 | test_fgetgrent (const char *filename) |
| 83 | { |
| 84 | struct group *g; |
| 85 | FILE *f; |
| 86 | |
| 87 | f = fopen (filename,"r"); |
| 88 | |
| 89 | g = fgetgrent (f); |
| 90 | test_entry ("one", 1, g); |
| 91 | g = fgetgrent (f); |
| 92 | test_entry ("two", 2, g); |
| 93 | g = fgetgrent (f); |
| 94 | test_entry ("three", 3, g); |
| 95 | fclose (f); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | int |
| 100 | main (int argc, char *argv[]) |
| 101 | { |
| 102 | char *file = tmpnam (NULL); |
| 103 | int i = 0; |
| 104 | |
| 105 | if (argc > 1) |
| 106 | i = atoi (argv[1]); |
| 107 | if (i > 3) |
| 108 | i = 3; |
| 109 | if (i) |
| 110 | printf ("Large group is group: %d\n", i); |
| 111 | else |
| 112 | printf ("Not using a large group\n"); |
| 113 | write_group (file, i); |
| 114 | test_fgetgrent (file); |
| 115 | |
| 116 | remove (file); |
| 117 | |
| 118 | return (errors != 0); |
| 119 | } |