Back to index

glibc  2.9
fgetspent.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1997, 1999, 2000, 2002 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 <errno.h>
00020 #include <bits/libc-lock.h>
00021 #include <shadow.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 
00026 /* A reasonable size for a buffer to start with.  */
00027 #define BUFLEN_SPWD 1024
00028 
00029 /* We need to protect the dynamic buffer handling.  */
00030 __libc_lock_define_initialized (static, lock);
00031 
00032 libc_freeres_ptr (static char *buffer);
00033 
00034 /* Read one shadow entry from the given stream.  */
00035 struct spwd *
00036 fgetspent (FILE *stream)
00037 {
00038   static size_t buffer_size;
00039   static struct spwd resbuf;
00040   fpos_t pos;
00041   struct spwd *result;
00042   int save;
00043 
00044   if (fgetpos (stream, &pos) != 0)
00045     return NULL;
00046 
00047   /* Get lock.  */
00048   __libc_lock_lock (lock);
00049 
00050   /* Allocate buffer if not yet available.  */
00051   if (buffer == NULL)
00052     {
00053       buffer_size = BUFLEN_SPWD;
00054       buffer = malloc (buffer_size);
00055     }
00056 
00057   while (buffer != NULL
00058         && (__fgetspent_r (stream, &resbuf, buffer, buffer_size, &result)
00059             == ERANGE))
00060     {
00061       char *new_buf;
00062       buffer_size += BUFLEN_SPWD;
00063       new_buf = realloc (buffer, buffer_size);
00064       if (new_buf == NULL)
00065        {
00066          /* We are out of memory.  Free the current buffer so that the
00067             process gets a chance for a normal termination.  */
00068          save = errno;
00069          free (buffer);
00070          __set_errno (save);
00071        }
00072       buffer = new_buf;
00073 
00074       /* Reset the stream.  */
00075       if (fsetpos (stream, &pos) != 0)
00076        buffer = NULL;
00077     }
00078 
00079   if (buffer == NULL)
00080     result = NULL;
00081 
00082   /* Release lock.  Preserve error value.  */
00083   save = errno;
00084   __libc_lock_unlock (lock);
00085   __set_errno (save);
00086 
00087   return result;
00088 }