Back to index

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