/** \file string.c memory allocation and string handling functions */
-#include <sys/time.h> /* gettimeofday */
#include "para.h"
+#include "string.h"
+
+#include <sys/time.h> /* gettimeofday */
#include <regex.h>
#include <pwd.h>
#include <sys/utsname.h> /* uname() */
+#include <string.h>
+
#include "error.h"
-#include "string.h"
/**
* paraslash's version of realloc()
* This function modifies \a args by replacing each occurance of \a delim by
* zero. A NULL-terminated array of pointers to char* is allocated dynamically
* and these pointers are initialized to point to the broken-up substrings
- * within \a args. A pointer to this array is returned via \a argv_ptr.
+ * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
+ * to call this function with \a args == NULL.
*
* \return The number of substrings found in \a args.
*/
char **argv;
ssize_t n = 0, i;
- while ((p = strchr(p, delim))) {
+ while (p && (p = strchr(p, delim))) {
p++;
n++;
}
- *argv_ptr = para_malloc((n + 3) * sizeof(char *));
+ *argv_ptr = para_calloc((n + 3) * sizeof(char *));
argv = *argv_ptr;
i = 0;
p = args;
i++;
}
}
- argv[n + 1] = NULL;
return n;
}