]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-netgrp.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / nis / nss_nis / nis-netgrp.c
CommitLineData
d4697bc9 1/* Copyright (C) 1996-2014 Free Software Foundation, Inc.
6259ec0d 2 This file is part of the GNU C Library.
b85697f6 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6259ec0d
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
6259ec0d
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
6259ec0d 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
6259ec0d 18
8500b0ca 19#include <assert.h>
6259ec0d
UD
20#include <ctype.h>
21#include <errno.h>
8500b0ca 22#include <malloc.h>
6259ec0d 23#include <netdb.h>
8500b0ca 24#include <nss.h>
6259ec0d
UD
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <netgroup.h>
29#include <rpcsvc/yp.h>
30#include <rpcsvc/ypclnt.h>
31
32#include "nss-nis.h"
33
6259ec0d 34extern enum nss_status
8500b0ca 35_nss_netgroup_parseline (char **cursor, struct __netgrent *netgrp,
d71b808a 36 char *buffer, size_t buflen, int *errnop);
6259ec0d 37
b677d674
UD
38
39static void
8500b0ca 40internal_nis_endnetgrent (struct __netgrent *netgrp)
b677d674 41{
ff3cacc5
UD
42 free (netgrp->data);
43 netgrp->data = NULL;
44 netgrp->data_size = 0;
45 netgrp->cursor = NULL;
b677d674
UD
46}
47
ab9a9ff8 48
6259ec0d 49enum nss_status
8500b0ca 50_nss_nis_setnetgrent (const char *group, struct __netgrent *netgrp)
6259ec0d 51{
8500b0ca 52 int len;
6259ec0d
UD
53 enum nss_status status;
54
55 status = NSS_STATUS_SUCCESS;
56
a1ffb40e 57 if (__glibc_unlikely (group == NULL || group[0] == '\0'))
6259ec0d
UD
58 return NSS_STATUS_UNAVAIL;
59
ab9a9ff8 60 char *domain;
a1ffb40e 61 if (__glibc_unlikely (yp_get_default_domain (&domain)))
6259ec0d
UD
62 return NSS_STATUS_UNAVAIL;
63
8500b0ca
UD
64 status = yperr2nss (yp_match (domain, "netgroup", group, strlen (group),
65 &netgrp->data, &len));
a1ffb40e 66 if (__glibc_likely (status == NSS_STATUS_SUCCESS))
6259ec0d 67 {
8500b0ca
UD
68 /* Our implementation of yp_match already allocates a buffer
69 which is one byte larger than the value in LEN specifies
70 and the last byte is filled with NUL. So we can simply
71 use that buffer. */
b4f6f4be 72 assert (len >= 0);
8500b0ca
UD
73 assert (malloc_usable_size (netgrp->data) >= len + 1);
74 assert (netgrp->data[len] == '\0');
75
76 netgrp->data_size = len;
77 netgrp->cursor = netgrp->data;
6259ec0d
UD
78 }
79
6259ec0d
UD
80 return status;
81}
82
83
84enum nss_status
8500b0ca 85_nss_nis_endnetgrent (struct __netgrent *netgrp)
6259ec0d 86{
8500b0ca 87 internal_nis_endnetgrent (netgrp);
6259ec0d
UD
88
89 return NSS_STATUS_SUCCESS;
90}
91
ab9a9ff8 92
6259ec0d 93enum nss_status
d71b808a
UD
94_nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
95 int *errnop)
6259ec0d 96{
8500b0ca
UD
97 return _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
98 errnop);
6259ec0d 99}