2003-05-16 Roland McGrath <roland@redhat.com>
[glibc.git] / nis / nis_file.c
blobc84fb3c6c9becda56513d974e1dfc326261f5295
1 /* Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <rpcsvc/nis.h>
24 #include "nis_xdr.h"
26 static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
28 directory_obj *
29 readColdStartFile (void)
31 XDR xdrs;
32 FILE *in;
33 bool_t status = TRUE;
34 directory_obj *obj;
36 in = fopen (cold_start_file, "rb");
37 if (in == NULL)
38 return NULL;
40 obj = calloc (1, sizeof (directory_obj));
42 if (obj != NULL)
44 xdrstdio_create (&xdrs, in, XDR_DECODE);
45 status = _xdr_directory_obj (&xdrs, obj);
46 xdr_destroy (&xdrs);
48 if (!status)
50 nis_free_directory (obj);
51 obj = NULL;
55 fclose (in);
57 return obj;
60 bool_t
61 writeColdStartFile (const directory_obj *obj)
63 XDR xdrs;
64 FILE *out;
65 bool_t status;
67 out = fopen (cold_start_file, "wb");
68 if (out == NULL)
69 return FALSE;
71 xdrstdio_create (&xdrs, out, XDR_ENCODE);
72 status = _xdr_directory_obj (&xdrs, (directory_obj *) obj);
73 xdr_destroy (&xdrs);
74 fclose (out);
76 return status;
79 nis_object *
80 nis_read_obj (const char *name)
82 XDR xdrs;
83 FILE *in;
84 bool_t status;
85 nis_object *obj;
87 in = fopen (name, "rb");
88 if (in == NULL)
89 return NULL;
91 obj = calloc (1, sizeof (nis_object));
92 if (obj == NULL)
94 fclose (in);
95 return NULL;
98 xdrstdio_create (&xdrs, in, XDR_DECODE);
99 status =_xdr_nis_object (&xdrs, obj);
100 xdr_destroy (&xdrs);
101 fclose (in);
103 if (status)
104 return obj;
105 else
107 nis_free_object (obj);
108 return NULL;
112 bool_t
113 nis_write_obj (const char *name, const nis_object *obj)
115 XDR xdrs;
116 FILE *out;
117 bool_t status;
119 out = fopen (name, "wb");
120 if (out == NULL)
121 return FALSE;
123 xdrstdio_create (&xdrs, out, XDR_ENCODE);
124 status = _xdr_nis_object (&xdrs, (nis_object *) obj);
125 xdr_destroy (&xdrs);
126 fclose (out);
128 return status;