1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #include <stdio.h>
21: #include <stdlib.h>
22: #include <string.h>
23: #include <rpcsvc/nis.h>
24: #include "nis_xdr.h"
25:
26: typedef bool_t (*iofct_t) (XDR *, void *);
27: typedef void (*freefct_t) (void *);
28:
29:
30: static void *
31: read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
32: size_t objsize)
33: {
34: FILE *in = fopen (name, "rc");
35: if (in == NULL)
36: return NULL;
37:
38: void *obj = calloc (1, objsize);
39:
40: if (obj != NULL)
41: {
42: XDR xdrs;
43: xdrstdio_create (&xdrs, in, XDR_DECODE);
44: bool_t status = readfct (&xdrs, obj);
45: xdr_destroy (&xdrs);
46:
47: if (!status)
48: {
49: freefct (obj);
50: obj = NULL;
51: }
52: }
53:
54: fclose (in);
55:
56: return obj;
57: }
58:
59: static bool_t
60: write_nis_obj (const char *name, const void *obj, iofct_t writefct)
61: {
62: FILE *out = fopen (name, "w");
63: if (out == NULL)
64: return FALSE;
65:
66: XDR xdrs;
67: xdrstdio_create (&xdrs, out, XDR_ENCODE);
68: bool_t status = writefct (&xdrs, (void *) obj);
69: xdr_destroy (&xdrs);
70: fclose (out);
71:
72: return status;
73: }
74:
75:
76: static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
77:
78: directory_obj *
79: readColdStartFile (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: }
84: libnsl_hidden_def (readColdStartFile)
85:
86: bool_t
87: writeColdStartFile (const directory_obj *obj)
88: {
89: return write_nis_obj (cold_start_file, obj, (iofct_t) _xdr_directory_obj);
90: }
91:
92: nis_object *
93: nis_read_obj (const char *name)
94: {
95: return read_nis_obj (name, (iofct_t) _xdr_nis_object,
96: (freefct_t) nis_free_object, sizeof (nis_object));
97: }
98:
99: bool_t
100: nis_write_obj (const char *name, const nis_object *obj)
101: {
102: return write_nis_obj (name, obj, (iofct_t) _xdr_nis_object);
103: }