update from main archive 961001
[glibc.git] / nss / nss_db / db-netgrp.c
blob441a0bdf559bd318128f4ab514e6c3fb3ba0559c
1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <db.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <libc-lock.h>
25 #include <paths.h>
26 #include "nsswitch.h"
27 #include "netgroup.h"
30 #define DBFILE _PATH_VARDB "netgroup.db"
33 /* Locks the static variables in this file. */
34 __libc_lock_define_initialized (static, lock)
36 /* Maintenance of the shared handle open on the database. */
37 static DB *db;
38 static char *entry;
39 static char *cursor;
41 enum nss_status
42 _nss_db_setnetgrent (const char *group)
44 enum nss_status status = NSS_STATUS_SUCCESS;
46 __libc_lock_lock (lock);
48 /* Make sure the data base file is open. */
49 if (db == NULL)
51 db = dbopen (DBFILE, O_RDONLY, 0, DB_BTREE, NULL);
53 if (db == NULL)
54 status = errno = EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
57 if (status == NSS_STATUS_SUCCESS)
59 DBT key = { data: (void *) group, size: strlen (group) };
60 DBT value;
62 if ((*db->get) (db, &key, &value, 0) != 0)
63 status = NSS_STATUS_NOTFOUND;
64 else
65 cursor = entry = value.data;
68 __libc_lock_unlock (lock);
70 return status;
75 enum nss_status
76 _nss_db_endnetgrent (void)
78 __libc_lock_lock (lock);
80 if (db != NULL)
82 (*db->close) (db);
83 db = NULL;
86 __libc_lock_unlock (lock);
88 return NSS_STATUS_SUCCESS;
92 extern enum nss_status _nss_netgroup_parseline (char **cursor,
93 struct __netgrent *result,
94 char *buffer, int buflen);
96 enum nss_status
97 _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, int buflen)
99 int status;
101 __libc_lock_lock (lock);
103 status = _nss_netgroup_parseline (&cursor, result, buffer, buflen);
105 __libc_lock_unlock (lock);
107 return status;