#include "nls.h"
#include "c.h"
+#include "xalloc.h"
/* Use libpcre2posix if it's available */
#ifdef HAVE_PCRE2_POSIX
#ifdef HAVE_SYS_XATTR_H
-/**
- * malloc_or_die -- Wrapper for malloc()
- *
- * This does the same thing as malloc() except that it aborts if memory
- * can't be allocated.
- */
-static void *malloc_or_die(size_t size)
-{
- void *mem = malloc(size);
-
- if (!mem) {
- jlog(JLOG_SYSFAT, "Cannot allocate memory");
- exit(1);
- }
- return mem;
-}
-
/**
* llistxattr_or_die - Wrapper for llistxattr()
*
*/
static const char **get_sorted_xattr_name_table(const char *names, int n)
{
- const char **table = malloc_or_die(n * sizeof(char *));
+ const char **table = xmalloc(n * sizeof(char *));
int i;
for (i = 0; i < n; i++) {
if (len_a != len_b)
return FALSE; // total lengths of xattr names differ
- names_a = malloc_or_die(len_a);
- names_b = malloc_or_die(len_b);
+ names_a = xmalloc(len_a);
+ names_b = xmalloc(len_b);
len_a = llistxattr_or_die(a->links->path, names_a, len_a);
len_b = llistxattr_or_die(b->links->path, names_b, len_b);
if (len_a != len_b)
goto exit; // xattrs with same name, different value lengths
- value_a = malloc_or_die(len_a);
- value_b = malloc_or_die(len_b);
+ value_a = xmalloc(len_a);
+ value_b = xmalloc(len_b);
len_a = lgetxattr_or_die(a->links->path, name_ptrs_a[i],
value_a, len_a);
if (!opts.dry_run) {
size_t len = strlen(b->links->path) + strlen(".hardlink-temporary") + 1;
- char *new_path = malloc(len);
-
- if (new_path == NULL) {
- jlog(JLOG_SYSFAT, "Cannot allocate memory");
- exit(1);
- }
+ char *new_path = xmalloc(len);
snprintf(new_path, len, "%s.hardlink-temporary", b->links->path);
pathlen = strlen(fpath) + 1;
- fil = calloc(1, sizeof(*fil));
-
- if (fil == NULL)
- return jlog(JLOG_SYSFAT, "Cannot continue"), 1;
-
- fil->links = calloc(1, sizeof(struct link) + pathlen);
-
- if (fil->links == NULL)
- return jlog(JLOG_SYSFAT, "Cannot continue"), 1;
+ fil = xcalloc(1, sizeof(*fil));
+ fil->links = xcalloc(1, sizeof(struct link) + pathlen);
fil->st = *sb;
fil->links->basename = ftwbuf->base;
struct regex_link *link;
int err;
- link = malloc(sizeof(*link));
-
- if (link == NULL) {
- jlog(JLOG_SYSFAT, "Cannot allocate memory");
- exit(1);
- }
+ link = xmalloc(sizeof(*link));
if ((err = regcomp(&link->preg, regex, REG_NOSUB | REG_EXTENDED)) != 0) {
size_t size = regerror(err, &link->preg, NULL, 0);
- char *buf = malloc(size + 1);
-
- if (buf == NULL) {
- jlog(JLOG_SYSFAT, "Cannot allocate memory");
- exit(1);
- }
+ char *buf = xmalloc(size + 1);
regerror(err, &link->preg, buf, size);