cachefile = optarg;
break;
case 'd':
- if (searchdirs == NULL) {
- searchdirs = safe_malloc(sizeof (char *));
- } else {
- char **tmp = safe_malloc((nsearch + 1) *
- sizeof (char *));
- bcopy(searchdirs, tmp, nsearch *
- sizeof (char *));
- free(searchdirs);
- searchdirs = tmp;
- }
+ searchdirs = safe_realloc(searchdirs,
+ (nsearch + 1) * sizeof (char *));
searchdirs[nsearch++] = optarg;
break;
case 'D':
* Check the environment for the preferred search path.
*/
if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
- char *dir;
+ char *dir, *tmp = NULL;
envdup = strdup(env);
- dir = strtok(envdup, ":");
- while (dir != NULL) {
- if (searchdirs == NULL) {
- searchdirs = safe_malloc(sizeof (char *));
- } else {
- char **tmp = safe_malloc((nsearch + 1) *
- sizeof (char *));
- bcopy(searchdirs, tmp, nsearch *
- sizeof (char *));
- free(searchdirs);
- searchdirs = tmp;
- }
+ for (dir = strtok_r(envdup, ":", &tmp);
+ dir != NULL;
+ dir = strtok_r(NULL, ":", &tmp)) {
+ searchdirs = safe_realloc(searchdirs,
+ (nsearch + 1) * sizeof (char *));
searchdirs[nsearch++] = dir;
- dir = strtok(NULL, ":");
}
}
}
if (err == 1) {
- if (searchdirs != NULL)
- free(searchdirs);
- if (envdup != NULL)
- free(envdup);
+ free(searchdirs);
+ free(envdup);
nvlist_free(policy);
nvlist_free(pools);
nvlist_free(props);
nvlist_free(props);
nvlist_free(pools);
nvlist_free(policy);
- if (searchdirs != NULL)
- free(searchdirs);
- if (envdup != NULL)
- free(envdup);
+ free(searchdirs);
+ free(envdup);
return (err ? 1 : 0);
}
return (data);
}
+/*
+ * Utility function to guarantee realloc() success.
+ */
+void *
+safe_realloc(void *from, size_t size)
+{
+ void *data;
+
+ if ((data = realloc(from, size)) == NULL) {
+ (void) fprintf(stderr, "internal error: out of memory\n");
+ exit(1);
+ }
+
+ return (data);
+}
+
/*
* Display an out of memory error message and abort the current program.
*/
* Basic utility functions
*/
void *safe_malloc(size_t);
+void *safe_realloc(void *, size_t);
void zpool_no_memory(void);
uint_t num_logs(nvlist_t *nv);
uint64_t array64_max(uint64_t array[], unsigned int len);