From 8d7e0aaa20dff413fdc9747b842015664dae6cac Mon Sep 17 00:00:00 2001
From: Johannes Hofmann <Johannes.Hofmann@gmx.de>
Date: Mon, 15 Aug 2005 12:01:08 +0100
Subject: add check to avoid saving to input image file

---
 src/GipfelWidget.cxx | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/GipfelWidget.cxx b/src/GipfelWidget.cxx
index d2fe69c..44134f8 100644
--- a/src/GipfelWidget.cxx
+++ b/src/GipfelWidget.cxx
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <math.h>
@@ -147,12 +148,26 @@ GipfelWidget::save_image(char *file) {
   char buf[1024];
   int status;
   size_t n;
+  struct stat in_stat, out_stat;
 
   if (img_file == NULL) {
     fprintf(stderr, "Nothing to save\n");
     return 1;
   }
 
+  if (stat(img_file, &in_stat) != 0) {
+    perror("stat");
+    return 1;
+  }
+
+  if (stat(file, &out_stat) == 0) {
+    if (in_stat.st_ino == out_stat.st_ino) {
+      fprintf(stderr, "Input image %s and output image %s are the same file\n",
+        img_file, file);
+      return 1;
+    }
+  } 
+
   out = fopen(file, "w");
   if (out == NULL) {
     perror("fopen");
-- 
cgit v1.2.3