--- /dev/null
+/*
+Safe string-to-double conversion
+
+Copyright (C) 2013 W. Trevor King
+
+This program 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 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h> /* for strtod(), exit(), EXIT_* */
+#include <stdio.h> /* for stderr, *printf(), perror() */
+#include <errno.h> /* for errno */
+#include "safe-strtod.h" /* check safe_strtod() declaration */
+
+double safe_strtod(const char *str, const char *name)
+{
+ char *endptr;
+ double x = strtod(str, &endptr);
+
+ if (errno) {
+ fprintf(stderr, "error converting %s to a double for %s:\n", str, name);
+ perror("strtod");
+ exit(EXIT_FAILURE);
+ }
+ if (endptr[0] != '\0') {
+ fprintf(stderr, "error converting %s to a double for %s:\n", str, name);
+ fprintf(stderr, "leftover characters: %s\n", endptr);
+ exit(EXIT_FAILURE);
+ }
+ return x;
+}
--- /dev/null
+/*
+Safe string-to-double conversion
+
+Copyright (C) 2013 W. Trevor King
+
+This program 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 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _STRTOD_SAFE_H_
+#define _STRTOD_SAFE_H_
+
+double safe_strtod(const char *str, const char *name);
+
+#endif /* _STRTOD_SAFE_H_ */