about summary refs log tree commit diff
path: root/nis/nis_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'nis/nis_file.c')
-rw-r--r--nis/nis_file.c93
1 files changed, 35 insertions, 58 deletions
diff --git a/nis/nis_file.c b/nis/nis_file.c
index 1f2295787c..566f30c48c 100644
--- a/nis/nis_file.c
+++ b/nis/nis_file.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 1997, 1998, 1999, 2004 Free Software Foundation, Inc.
+/* Copyright (c) 1997, 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
 
@@ -23,27 +23,30 @@
 #include <rpcsvc/nis.h>
 #include "nis_xdr.h"
 
-static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
+typedef bool_t (*iofct_t) (XDR *, void *);
+typedef void (*freefct_t) (void *);
 
-directory_obj *
-readColdStartFile (void)
+
+static void *
+read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
+	      size_t objsize)
 {
-  FILE *in = fopen (cold_start_file, "rc");
+  FILE *in = fopen (name, "rc");
   if (in == NULL)
     return NULL;
 
-  directory_obj *obj = calloc (1, sizeof (directory_obj));
+  void *obj = calloc (1, objsize);
 
   if (obj != NULL)
     {
       XDR xdrs;
       xdrstdio_create (&xdrs, in, XDR_DECODE);
-      bool_t status = _xdr_directory_obj (&xdrs, obj);
+      bool_t status = readfct (&xdrs, obj);
       xdr_destroy (&xdrs);
 
       if (!status)
 	{
-	  nis_free_directory (obj);
+	  freefct (obj);
 	  obj = NULL;
 	}
     }
@@ -52,75 +55,49 @@ readColdStartFile (void)
 
   return obj;
 }
-libnsl_hidden_def (readColdStartFile)
 
-bool_t
-writeColdStartFile (const directory_obj *obj)
+static bool_t
+write_nis_obj (const char *name, const void *obj, iofct_t writefct)
 {
-  XDR xdrs;
-  FILE *out;
-  bool_t status;
-
-  out = fopen (cold_start_file, "wb");
+  FILE *out = fopen (name, "w");
   if (out == NULL)
     return FALSE;
 
+  XDR xdrs;
   xdrstdio_create (&xdrs, out, XDR_ENCODE);
-  status = _xdr_directory_obj (&xdrs, (directory_obj *) obj);
+  bool_t status = writefct (&xdrs, (void *) obj);
   xdr_destroy (&xdrs);
   fclose (out);
 
   return status;
 }
 
-nis_object *
-nis_read_obj (const char *name)
-{
-  XDR xdrs;
-  FILE *in;
-  bool_t status;
-  nis_object *obj;
 
-  in = fopen (name, "rb");
-  if (in == NULL)
-    return NULL;
+static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
 
-  obj = calloc (1, sizeof (nis_object));
-  if (obj == NULL)
-    {
-      fclose (in);
-      return NULL;
-    }
+directory_obj *
+readColdStartFile (void)
+{
+  return read_nis_obj (cold_start_file, (iofct_t) _xdr_directory_obj,
+		       (freefct_t) nis_free_directory, sizeof (directory_obj));
+}
+libnsl_hidden_def (readColdStartFile)
 
-  xdrstdio_create (&xdrs, in, XDR_DECODE);
-  status =_xdr_nis_object (&xdrs, obj);
-  xdr_destroy (&xdrs);
-  fclose (in);
+bool_t
+writeColdStartFile (const directory_obj *obj)
+{
+  return write_nis_obj (cold_start_file, obj, (iofct_t) _xdr_directory_obj);
+}
 
-  if (status)
-    return obj;
-  else
-    {
-      nis_free_object (obj);
-      return NULL;
-    }
+nis_object *
+nis_read_obj (const char *name)
+{
+  return read_nis_obj (name, (iofct_t) _xdr_nis_object,
+		       (freefct_t) nis_free_object, sizeof (nis_object));
 }
 
 bool_t
 nis_write_obj (const char *name, const nis_object *obj)
 {
-  XDR xdrs;
-  FILE *out;
-  bool_t status;
-
-  out = fopen (name, "wb");
-  if (out == NULL)
-    return FALSE;
-
-  xdrstdio_create (&xdrs, out, XDR_ENCODE);
-  status = _xdr_nis_object (&xdrs, (nis_object *) obj);
-  xdr_destroy (&xdrs);
-  fclose (out);
-
-  return status;
+  return write_nis_obj (name, obj, (iofct_t) _xdr_nis_object);
 }