Adding a bit more checking on file operations.
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_preferences.cc
index 854314447adad865afe0e43bab157a24d7841f03..5f741224834e16fee6458cd1d5fa2b706565472d 100644 (file)
@@ -1,12 +1,12 @@
 /* -*- c++ -*- */
 /*
- * Copyright 2003 Free Software Foundation, Inc.
+ * Copyright 2003,2010 Free Software Foundation, Inc.
  * 
  * This file is part of GNU Radio
  * 
  * GNU Radio is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
+ * the Free Software Foundation; either version 3, or (at your option)
  * any later version.
  * 
  * GNU Radio is distributed in the hope that it will be useful,
@@ -16,8 +16,8 @@
  * 
  * You should have received a copy of the GNU General Public License
  * along with GNU Radio; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <string.h>
 
 
 #ifdef MKDIR_TAKES_ONE_ARG
@@ -76,11 +77,20 @@ gr_preferences::get (const char *key)
   static char buf[1024];
 
   FILE         *fp = fopen (pathname (key), "r");
-  if (fp == 0)
+  if (fp == 0) {
+    perror (pathname (key));
     return 0;
+  }
 
   memset (buf, 0, sizeof (buf));
-  fread (buf, 1, sizeof (buf) - 1, fp);
+  size_t ret = fread (buf, 1, sizeof (buf) - 1, fp);
+  if(ret == 0) {
+    if(ferror(fp) != 0) {
+      perror (pathname (key));
+      fclose (fp);
+      return 0;
+    }
+  }
   fclose (fp);
   return buf;
 }
@@ -96,6 +106,13 @@ gr_preferences::set (const char *key, const char *value)
     return;
   }
 
-  fwrite (value, 1, strlen (value), fp);
+  size_t ret = fwrite (value, 1, strlen (value), fp);
+  if(ret == 0) {
+    if(ferror(fp) != 0) {
+      perror (pathname (key));
+      fclose (fp);
+      return;
+    }
+  }
   fclose (fp);
 };