Replace mh_xmalloc() with mh_xcalloc()
[mmh] / sbr / brkstring.c
index da42d61..acbba6a 100644 (file)
@@ -7,6 +7,7 @@
 ** complete copyright information.
 */
 
+#include <sysexits.h>
 #include <h/mh.h>
 #include <h/utils.h>
 
@@ -29,7 +30,7 @@ brkstring(char *str, char *brksep, char *brkterm)
        /* allocate initial space for pointers on first call */
        if (!broken) {
                len = NUMBROKEN;
-               broken = (char **)mh_xmalloc((size_t)(len * sizeof(*broken)));
+               broken = (char **)mh_xcalloc((size_t)len, sizeof(*broken));
        }
 
        /*
@@ -46,7 +47,7 @@ brkstring(char *str, char *brksep, char *brkterm)
                }
 
                /* handle separators */
-               while ((c=*s) && strchr(brksep, c)) {
+               while ((c=*s) && brksep && strchr(brksep, c)) {
                        *s++ = '\0';
                }
 
@@ -54,7 +55,7 @@ brkstring(char *str, char *brksep, char *brkterm)
                ** we are either at the end of the string, or the
                ** terminator found has been found, so finish up.
                */
-               if (!c || strchr(brkterm, c)) {
+               if (!c || (brkterm && strchr(brkterm, c))) {
                        *s = '\0';
                        broken[i] = NULL;
                        return broken;
@@ -62,9 +63,14 @@ brkstring(char *str, char *brksep, char *brkterm)
 
                /* set next start addr and walk over word */
                broken[i] = s;
-               while ((c=*++s) && !strchr(brksep, c) && !strchr(brkterm, c)) {
-                       continue;
+               while ((c = *++s)) {
+                       if (brksep && strchr(brksep, c)) {
+                               break;
+                       }
+                       if (brkterm && strchr(brkterm, c)) {
+                               break;
+                       }
                }
        }
-       adios("brkstring()", "reached unreachable point");
+       adios(EX_SOFTWARE, "brkstring()", "reached unreachable point");
 }