Back to index

glibc  2.9
nis_file.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <rpcsvc/nis.h>
00024 #include "nis_xdr.h"
00025 
00026 typedef bool_t (*iofct_t) (XDR *, void *);
00027 typedef void (*freefct_t) (void *);
00028 
00029 
00030 static void *
00031 read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
00032              size_t objsize)
00033 {
00034   FILE *in = fopen (name, "rc");
00035   if (in == NULL)
00036     return NULL;
00037 
00038   void *obj = calloc (1, objsize);
00039 
00040   if (obj != NULL)
00041     {
00042       XDR xdrs;
00043       xdrstdio_create (&xdrs, in, XDR_DECODE);
00044       bool_t status = readfct (&xdrs, obj);
00045       xdr_destroy (&xdrs);
00046 
00047       if (!status)
00048        {
00049          freefct (obj);
00050          obj = NULL;
00051        }
00052     }
00053 
00054   fclose (in);
00055 
00056   return obj;
00057 }
00058 
00059 static bool_t
00060 write_nis_obj (const char *name, const void *obj, iofct_t writefct)
00061 {
00062   FILE *out = fopen (name, "w");
00063   if (out == NULL)
00064     return FALSE;
00065 
00066   XDR xdrs;
00067   xdrstdio_create (&xdrs, out, XDR_ENCODE);
00068   bool_t status = writefct (&xdrs, (void *) obj);
00069   xdr_destroy (&xdrs);
00070   fclose (out);
00071 
00072   return status;
00073 }
00074 
00075 
00076 static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
00077 
00078 directory_obj *
00079 readColdStartFile (void)
00080 {
00081   return read_nis_obj (cold_start_file, (iofct_t) _xdr_directory_obj,
00082                      (freefct_t) nis_free_directory, sizeof (directory_obj));
00083 }
00084 libnsl_hidden_def (readColdStartFile)
00085 
00086 bool_t
00087 writeColdStartFile (const directory_obj *obj)
00088 {
00089   return write_nis_obj (cold_start_file, obj, (iofct_t) _xdr_directory_obj);
00090 }
00091 
00092 nis_object *
00093 nis_read_obj (const char *name)
00094 {
00095   return read_nis_obj (name, (iofct_t) _xdr_nis_object,
00096                      (freefct_t) nis_free_object, sizeof (nis_object));
00097 }
00098 
00099 bool_t
00100 nis_write_obj (const char *name, const nis_object *obj)
00101 {
00102   return write_nis_obj (name, obj, (iofct_t) _xdr_nis_object);
00103 }