#define LF 0x01
#define CR 0x02
+#define COUNT_OF(obj, type) (sizeof (obj) / sizeof (type))
+
#define SKIP_LINE_ENDINGS(flags) ((flags) == (CR|LF) ? 2 : 1)
#define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
unsigned int count;
const char *desc;
} tables[] = {
- { fg_colors, sizeof (fg_colors) / sizeof (struct color), "foreground" },
- { bg_colors, sizeof (bg_colors) / sizeof (struct color), "background" },
+ { fg_colors, COUNT_OF (fg_colors, struct color), "foreground" },
+ { bg_colors, COUNT_OF (bg_colors, struct color), "background" },
};
enum {
{
bool valid_attr = false;
unsigned int i;
- for (i = 0; i < sizeof (attrs) / sizeof (struct attr); i++)
+ for (i = 0; i < COUNT_OF (attrs, struct attr); i++)
{
const size_t name_len = strlen (attrs[i].name);
if ((size_t)(p - s) == name_len && strneq (s, attrs[i].name, name_len))
{
const struct opt_data *opt_data = NULL;
unsigned int i;
- for (i = 0; i < sizeof (opts_data) / sizeof (struct opt_data); i++)
+ for (i = 0; i < COUNT_OF (opts_data, struct opt_data); i++)
if (streq (opt->name, opts_data[i].name))
{
opt_data = &opts_data[i];
return p;
}
#else
+static const char *const format_debug = "%s: %10s %7lu bytes [source file %s, line %5u]\n";
static void *
malloc_wrap_debug (size_t size, const char *file, unsigned int line)
{
void *p = malloc (size);
if (!p)
MEM_ALLOC_FAIL_DEBUG (file, line);
- fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+ fprintf (log, format_debug, program_name, "malloc'ed", (unsigned long)size, file, line);
return p;
}
void *p = calloc (nmemb, size);
if (!p)
MEM_ALLOC_FAIL_DEBUG (file, line);
- fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
+ fprintf (log, format_debug, program_name, "calloc'ed", (unsigned long)(nmemb * size), file, line);
return p;
}
void *p = realloc (ptr, size);
if (!p)
MEM_ALLOC_FAIL_DEBUG (file, line);
- fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+ fprintf (log, format_debug, program_name, "realloc'ed", (unsigned long)size, file, line);
return p;
}
#endif /* !DEBUG */