]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_file.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nis / nis_file.c
CommitLineData
04277e02 1/* Copyright (c) 1997-2019 Free Software Foundation, Inc.
e61abf83 2 This file is part of the GNU C Library.
32abdb71 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
e61abf83
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.
e61abf83
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.
e61abf83 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/>. */
e61abf83
UD
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <rpcsvc/nis.h>
82f43dd2 23#include <shlib-compat.h>
91eee4dd 24#include "nis_xdr.h"
5ae9d168 25
eca086a6
UD
26typedef bool_t (*iofct_t) (XDR *, void *);
27typedef void (*freefct_t) (void *);
0ecb606c 28
eca086a6
UD
29
30static void *
31read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
32 size_t objsize)
e61abf83 33{
312be3f9 34 FILE *in = fopen (name, "rce");
e61abf83 35 if (in == NULL)
3e5f5557 36 return NULL;
e61abf83 37
eca086a6 38 void *obj = calloc (1, objsize);
3d8fa13a
UD
39
40 if (obj != NULL)
e852e889 41 {
7440c23e 42 XDR xdrs;
3d8fa13a 43 xdrstdio_create (&xdrs, in, XDR_DECODE);
eca086a6 44 bool_t status = readfct (&xdrs, obj);
3d8fa13a
UD
45 xdr_destroy (&xdrs);
46
47 if (!status)
48 {
eca086a6 49 freefct (obj);
3d8fa13a
UD
50 obj = NULL;
51 }
e852e889 52 }
3d8fa13a
UD
53
54 fclose (in);
55
56 return obj;
e61abf83
UD
57}
58
eca086a6
UD
59static bool_t
60write_nis_obj (const char *name, const void *obj, iofct_t writefct)
e61abf83 61{
312be3f9 62 FILE *out = fopen (name, "wce");
e61abf83 63 if (out == NULL)
3e5f5557 64 return FALSE;
e61abf83 65
eca086a6 66 XDR xdrs;
e61abf83 67 xdrstdio_create (&xdrs, out, XDR_ENCODE);
eca086a6 68 bool_t status = writefct (&xdrs, (void *) obj);
a53bad16
UD
69 xdr_destroy (&xdrs);
70 fclose (out);
e61abf83 71
a53bad16 72 return status;
e61abf83
UD
73}
74
e61abf83 75
eca086a6 76static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
e61abf83 77
eca086a6
UD
78directory_obj *
79readColdStartFile (void)
80{
81 return read_nis_obj (cold_start_file, (iofct_t) _xdr_directory_obj,
82 (freefct_t) nis_free_directory, sizeof (directory_obj));
83}
1e4d83f6 84libnsl_hidden_nolink_def (readColdStartFile, GLIBC_2_1)
32abdb71 85
eca086a6
UD
86bool_t
87writeColdStartFile (const directory_obj *obj)
88{
89 return write_nis_obj (cold_start_file, obj, (iofct_t) _xdr_directory_obj);
90}
1e4d83f6 91libnsl_hidden_nolink_def (writeColdStartFile, GLIBC_2_1)
e61abf83 92
eca086a6
UD
93nis_object *
94nis_read_obj (const char *name)
95{
96 return read_nis_obj (name, (iofct_t) _xdr_nis_object,
97 (freefct_t) nis_free_object, sizeof (nis_object));
e61abf83 98}
1e4d83f6 99libnsl_hidden_nolink_def (nis_read_obj, GLIBC_2_1)
e61abf83
UD
100
101bool_t
102nis_write_obj (const char *name, const nis_object *obj)
103{
eca086a6 104 return write_nis_obj (name, obj, (iofct_t) _xdr_nis_object);
e61abf83 105}
1e4d83f6 106libnsl_hidden_nolink_def (nis_write_obj, GLIBC_2_1)