Back to index

glibc  2.9
fgetgrent_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1996-1999, 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <ctype.h>
00020 #include <errno.h>
00021 #include <grp.h>
00022 #include <stdio.h>
00023 
00024 #ifdef USE_IN_LIBIO
00025 # include <libio/iolibio.h>
00026 # define flockfile(s) _IO_flockfile (s)
00027 # define funlockfile(s) _IO_funlockfile (s)
00028 #endif
00029 
00030 /* Define a line parsing function using the common code
00031    used in the nss_files module.  */
00032 
00033 #define STRUCTURE    group
00034 #define ENTNAME             grent
00035 struct grent_data {};
00036 
00037 #define TRAILING_LIST_MEMBER              gr_mem
00038 #define TRAILING_LIST_SEPARATOR_P(c)      ((c) == ',')
00039 #include <nss/nss_files/files-parse.c>
00040 LINE_PARSER
00041 (,
00042  STRING_FIELD (result->gr_name, ISCOLON, 0);
00043  if (line[0] == '\0'
00044      && (result->gr_name[0] == '+' || result->gr_name[0] == '-'))
00045    {
00046      result->gr_passwd = NULL;
00047      result->gr_gid = 0;
00048    }
00049  else
00050    {
00051      STRING_FIELD (result->gr_passwd, ISCOLON, 0);
00052      if (result->gr_name[0] == '+' || result->gr_name[0] == '-')
00053        INT_FIELD_MAYBE_NULL (result->gr_gid, ISCOLON, 0, 10, , 0)
00054      else
00055        INT_FIELD (result->gr_gid, ISCOLON, 0, 10,)
00056    }
00057  )
00058 
00059 
00060 /* Read one entry from the given stream.  */
00061 int
00062 __fgetgrent_r (FILE *stream, struct group *resbuf, char *buffer, size_t buflen,
00063               struct group **result)
00064 {
00065   char *p;
00066   int parse_result;
00067 
00068   flockfile (stream);
00069   do
00070     {
00071       buffer[buflen - 1] = '\xff';
00072       p = fgets_unlocked (buffer, buflen, stream);
00073       if (__builtin_expect (p == NULL, 0) && feof_unlocked (stream))
00074        {
00075          funlockfile (stream);
00076          *result = NULL;
00077          __set_errno (ENOENT);
00078          return errno;
00079        }
00080       if (__builtin_expect (p == NULL, 0) || buffer[buflen - 1] != '\xff')
00081        {
00082          funlockfile (stream);
00083          *result = NULL;
00084          __set_errno (ERANGE);
00085          return errno;
00086        }
00087 
00088       /* Skip leading blanks.  */
00089       while (isspace (*p))
00090        ++p;
00091     } while (*p == '\0' || *p == '#'      /* Ignore empty and comment lines.  */
00092             /* Parse the line.  If it is invalid, loop to
00093               get the next line of the file to parse.  */
00094             || ! (parse_result = parse_line (p, resbuf,
00095                                          (void *) buffer, buflen,
00096                                          &errno)));
00097 
00098   funlockfile (stream);
00099 
00100   if (__builtin_expect (parse_result, 0) == -1)
00101     {
00102       /* The parser ran out of space.  */
00103       *result = NULL;
00104       return errno;
00105     }
00106 
00107   *result = resbuf;
00108   return 0;
00109 }
00110 weak_alias (__fgetgrent_r, fgetgrent_r)