Back to index

glibc  2.9
getXXent.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1997, 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 <errno.h>
00020 #include <bits/libc-lock.h>
00021 #include <stdlib.h>
00022 
00023 #include "nsswitch.h"
00024 
00025 /*******************************************************************\
00026 |* Here we assume several symbols to be defined:           *|
00027 |*                                                         *|
00028 |* LOOKUP_TYPE   - the return type of the function                *|
00029 |*                                                         *|
00030 |* GETFUNC_NAME  - name of the non-reentrant getXXXent function          *|
00031 |*                                                         *|
00032 |* BUFLEN      - size of static buffer                     *|
00033 |*                                                         *|
00034 |* Optionally the following vars can be defined:           *|
00035 |*                                                         *|
00036 |* NEED_H_ERRNO  - an extra parameter will be passed to point to   *|
00037 |*               the global `h_errno' variable.            *|
00038 |*                                                         *|
00039 \*******************************************************************/
00040 
00041 /* To make the real sources a bit prettier.  */
00042 #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
00043 #define APPEND_R(name) APPEND_R1 (name)
00044 #define APPEND_R1(name) name##_r
00045 #define INTERNAL(name) INTERNAL1 (name)
00046 #define INTERNAL1(name) __##name
00047 
00048 /* Sometimes we need to store error codes in the `h_errno' variable.  */
00049 #ifdef NEED_H_ERRNO
00050 # define H_ERRNO_PARM , int *h_errnop
00051 # define H_ERRNO_VAR &h_errno
00052 #else
00053 # define H_ERRNO_PARM
00054 # define H_ERRNO_VAR NULL
00055 #endif
00056 
00057 /* Prototype of the reentrant version.  */
00058 extern int INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer,
00059                                     size_t buflen, LOOKUP_TYPE **result
00060                                     H_ERRNO_PARM);
00061 
00062 /* We need to protect the dynamic buffer handling.  */
00063 __libc_lock_define_initialized (static, lock);
00064 
00065 /* This points to the static buffer used.  */
00066 libc_freeres_ptr (static char *buffer);
00067 
00068 
00069 LOOKUP_TYPE *
00070 GETFUNC_NAME (void)
00071 {
00072   static size_t buffer_size;
00073   static union
00074   {
00075     LOOKUP_TYPE l;
00076     void *ptr;
00077   } resbuf;
00078   LOOKUP_TYPE *result;
00079   int save;
00080 
00081   /* Get lock.  */
00082   __libc_lock_lock (lock);
00083 
00084   result = (LOOKUP_TYPE *)
00085     __nss_getent ((getent_r_function) INTERNAL (REENTRANT_GETNAME),
00086                 &resbuf.ptr, &buffer, BUFLEN, &buffer_size,
00087                 H_ERRNO_VAR);
00088 
00089   save = errno;
00090   __libc_lock_unlock (lock);
00091   __set_errno (save);
00092   return result;
00093 }
00094 
00095 static_link_warning (GETFUNC_NAME)