Back to index

glibc  2.9
fgetpwent_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1996, 1997, 1998, 1999 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 <stdio.h>
00022 #include <pwd.h>
00023 
00024 #ifdef USE_IN_LIBIO
00025 # define flockfile(s) _IO_flockfile (s)
00026 # define funlockfile(s) _IO_funlockfile (s)
00027 #endif
00028 
00029 /* Define a line parsing function using the common code
00030    used in the nss_files module.  */
00031 
00032 #define STRUCTURE    passwd
00033 #define ENTNAME             pwent
00034 struct pwent_data {};
00035 
00036 #include <nss/nss_files/files-parse.c>
00037 LINE_PARSER
00038 (,
00039  STRING_FIELD (result->pw_name, ISCOLON, 0);
00040  if (line[0] == '\0'
00041      && (result->pw_name[0] == '+' || result->pw_name[0] == '-'))
00042    {
00043      /* This a special case.  We allow lines containing only a `+' sign
00044        since this is used for nss_compat.  All other services will
00045        reject this entry later.  Initialize all other fields now.  */
00046      result->pw_passwd = NULL;
00047      result->pw_uid = 0;
00048      result->pw_gid = 0;
00049      result->pw_gecos = NULL;
00050      result->pw_dir = NULL;
00051      result->pw_shell = NULL;
00052    }
00053  else
00054    {
00055      STRING_FIELD (result->pw_passwd, ISCOLON, 0);
00056      if (result->pw_name[0] == '+' || result->pw_name[0] == '-')
00057        {
00058         INT_FIELD_MAYBE_NULL (result->pw_uid, ISCOLON, 0, 10, , 0)
00059         INT_FIELD_MAYBE_NULL (result->pw_gid, ISCOLON, 0, 10, , 0)
00060        }
00061      else
00062        {
00063         INT_FIELD (result->pw_uid, ISCOLON, 0, 10,)
00064         INT_FIELD (result->pw_gid, ISCOLON, 0, 10,)
00065        }
00066      STRING_FIELD (result->pw_gecos, ISCOLON, 0);
00067      STRING_FIELD (result->pw_dir, ISCOLON, 0);
00068      result->pw_shell = line;
00069    }
00070  )
00071 
00072 
00073 /* Read one entry from the given stream.  */
00074 int
00075 __fgetpwent_r (FILE *stream, struct passwd *resbuf, char *buffer,
00076               size_t buflen, struct passwd **result)
00077 {
00078   char *p;
00079 
00080   flockfile (stream);
00081   do
00082     {
00083       buffer[buflen - 1] = '\xff';
00084       p = fgets_unlocked (buffer, buflen, stream);
00085       if (p == NULL && feof_unlocked (stream))
00086        {
00087          funlockfile (stream);
00088          *result = NULL;
00089          __set_errno (ENOENT);
00090          return errno;
00091        }
00092       if (p == NULL || buffer[buflen - 1] != '\xff')
00093        {
00094          funlockfile (stream);
00095          *result = NULL;
00096          __set_errno (ERANGE);
00097          return errno;
00098        }
00099 
00100       /* Skip leading blanks.  */
00101       while (isspace (*p))
00102        ++p;
00103     } while (*p == '\0' || *p == '#' ||   /* Ignore empty and comment lines.  */
00104             /* Parse the line.  If it is invalid, loop to
00105               get the next line of the file to parse.  */
00106             ! parse_line (p, resbuf, (void *) buffer, buflen, &errno));
00107 
00108   funlockfile (stream);
00109 
00110   *result = resbuf;
00111   return 0;
00112 }
00113 weak_alias (__fgetpwent_r, fgetpwent_r)