summaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorMichael McConville <mmcconville@mykolab.com>2015-12-02 11:32:42 -0500
committerWill Estes <westes575@gmail.com>2015-12-02 14:39:09 -0500
commit399e94f904b913a4093295426820ab89fc3cd24f (patch)
treeac04185a0167975ba0b6ba6265a5a11e1e536761 /src/misc.c
parent9ba6e5283efd2fe454d3bc92eca960b3ebd91294 (diff)
Made string copying more standard.
copy_string() was a clone of the stdlib's strdup(). For safety, simplicity, and speed, we should use that instead. We introduce xstrdup() which wraps strdup() in a failure upon memory allocation errors.
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c26
1 files changed, 6 insertions, 20 deletions
diff --git a/src/misc.c b/src/misc.c
index 2bffdcd..d973b24 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -106,7 +106,7 @@ void action_define (const char *defname, int value)
add_action (buf);
/* track #defines so we can undef them when we're done. */
- cpy = copy_string (defname);
+ cpy = xstrdup(defname);
buf_append (&defs_buf, &cpy, 1);
}
@@ -240,28 +240,14 @@ unsigned char clower (int c)
}
-/* copy_string - returns a dynamically allocated copy of a string */
-
-char *copy_string (const char *str)
+char *xstrdup(const char *s)
{
- const char *c1;
- char *c2;
- char *copy;
- unsigned int size;
-
- /* find length */
- for (c1 = str; *c1; ++c1) ;
-
- size = (c1 - str + 1) * sizeof (char);
+ char *s2;
- copy = (char *) flex_alloc (size);
+ if ((s2 = strdup(s)) == NULL)
+ flexfatal (_("memory allocation failure in xstrdup()"));
- if (copy == NULL)
- flexfatal (_("dynamic memory failure in copy_string()"));
-
- for (c2 = copy; (*c2++ = *str++) != 0;) ;
-
- return copy;
+ return s2;
}