#include <limits.h>
#include <dirent.h>
#include <getopt.h>
+#include <assert.h>
#include <sys/mman.h>
#include "tests.h"
return buf;
}
-static char *copy_string(const char *s)
+/*
+ * Duplicate a string.
+ */
+static char *dup_string(const char *s)
{
char *str;
- if (!s)
- return NULL;
- str = malloc(strlen(s) + 1);
+ assert(s != NULL);
+ str = strdup(s);
CHECK(str != NULL);
- strcpy(str, s);
return str;
}
size_t sz;
if (a && !b)
- return copy_string(a);
+ return dup_string(a);
if (b && !a)
- return copy_string(b);
+ return dup_string(b);
if (!a && !b)
return NULL;
sz = strlen(a) + strlen(b) + 1;
size_t na, nb;
if (a && !b)
- return copy_string(a);
+ return dup_string(a);
if (b && !a)
- return copy_string(b);
+ return dup_string(b);
if (!a && !b)
return NULL;
entry = zalloc(sizeof(struct dir_entry_info));
entry->type = type;
- entry->name = copy_string(name);
+ entry->name = dup_string(name);
entry->parent = parent;
entry->next = parent->first;
entry->dir = dir;
dir->entry = entry;
- dir->name = copy_string(name);
+ dir->name = dup_string(name);
dir->parent = parent;
} else if (entry->type == 's') {
struct symlink_info *symlink = target;
free(path);
dir = zalloc(sizeof(struct dir_info));
- dir->name = copy_string(name);
+ dir->name = dup_string(name);
dir->parent = parent;
if (parent)
add_dir_entry(parent, 'd', name, dir);
free(path);
file = zalloc(sizeof(struct file_info));
- file->name = copy_string(name);
+ file->name = dup_string(name);
add_dir_entry(parent, 'f', name, file);
size_t len, totlen, tarlen;
if (target_pathname[0] == '/')
- return copy_string(target_pathname);
+ return dup_string(target_pathname);
p = strrchr(path, '/');
len = p - path;
len += 1;
*rename_entry = NULL;
if (grow || tests_random_no(20) < 10)
- return copy_string(make_name(dir));
+ return dup_string(make_name(dir));
r = tests_random_no(dir->number_of_entries);
entry = dir->first;
entry = dir->first;
if (!entry ||
(entry->type == 'd' && entry->dir->number_of_entries != 0))
- return copy_string(make_name(dir));
+ return dup_string(make_name(dir));
if ((isdir && entry->type != 'd') ||
(!isdir && entry->type == 'd'))
- return copy_string(make_name(dir));
+ return dup_string(make_name(dir));
*rename_entry = entry;
- return copy_string(entry->name);
+ return dup_string(entry->name);
}
static void rename_entry(struct dir_entry_info *entry)
len2 = strlen(p2);
up = str_count(p1, '/');
if (up == 0 && len2 != 0)
- return copy_string(p2);
+ return dup_string(p2);
if (up == 0 && len2 == 0) {
p2 = strrchr(path2, '/');
- return copy_string(p2);
+ return dup_string(p2);
}
if (up == 1 && len2 == 0)
- return copy_string(".");
+ return dup_string(".");
if (len2 == 0)
up -= 1;
len = up * 3 + len2 + 1;
static void symlink_new(struct dir_info *dir, const char *name_)
{
struct symlink_info *s;
- char *path, *target, *name = copy_string(name_);
+ char *path, *target, *name = dup_string(name_);
path = dir_path(dir, name);
target = pick_symlink_target(path);