Back to index

glibc  2.9
tst_fgetgrent.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <grp.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <sys/types.h>
00025 
00026 static int errors;
00027 
00028 static void
00029 write_users (FILE *f, int large_pos, int pos)
00030 {
00031   int i;
00032 
00033   if (pos == large_pos)
00034     {
00035       if (large_pos == 3)
00036        fprintf (f, ":three");
00037 
00038       /* we need more than 2048 bytes for proper testing.  */
00039       for (i = 0; i < 500; i++)
00040        fprintf (f, ",user%03d", i);
00041     }
00042   fprintf (f, "\n");
00043 
00044 }
00045 
00046 static void
00047 write_group (const char *filename, int pos)
00048 {
00049   FILE *f;
00050 
00051   f = fopen (filename, "w");
00052   fprintf (f, "one:x:1:one");
00053   write_users (f, pos, 1);
00054   fprintf (f, "two:x:2:two");
00055   write_users (f, pos, 2);
00056   fprintf (f, "three:x:3");
00057   write_users (f, pos, 3);
00058   fclose (f);
00059 }
00060 
00061 static void
00062 test_entry (const char *name, gid_t gid, struct group *g)
00063 {
00064   if (!g)
00065     {
00066       printf ("Error: Entry is empty\n");
00067       errors++;
00068       return;
00069     }
00070 
00071   if ((g->gr_gid == gid) && (strcmp (g->gr_name, name) == 0))
00072     printf ("Ok: %s: %d\n", g->gr_name, g->gr_gid);
00073   else
00074     {
00075       printf ("Error: %s: %d should be: %s: %d\n", g->gr_name, g->gr_gid,
00076              name, gid);
00077       errors++;
00078     }
00079 }
00080 
00081 
00082 static void
00083 test_fgetgrent (const char *filename)
00084 {
00085   struct group *g;
00086   FILE *f;
00087 
00088   f = fopen (filename,"r");
00089 
00090   g = fgetgrent (f);
00091   test_entry ("one", 1, g);
00092   g = fgetgrent (f);
00093   test_entry ("two", 2, g);
00094   g = fgetgrent (f);
00095   test_entry ("three", 3, g);
00096   fclose (f);
00097 }
00098 
00099 
00100 int
00101 main (int argc, char *argv[])
00102 {
00103   char *file = tmpnam (NULL);
00104   int i = 0;
00105 
00106   if (argc > 1)
00107     i = atoi (argv[1]);
00108   if (i > 3)
00109     i = 3;
00110   if (i)
00111     printf ("Large group is group: %d\n", i);
00112   else
00113     printf ("Not using a large group\n");
00114   write_group (file, i);
00115   test_fgetgrent (file);
00116 
00117   remove (file);
00118 
00119   return (errors != 0);
00120 }