#define free_null(ptr) free_wrap((void **)&ptr)
#define xstrdup(str) strdup_wrap(str)
-#if BUF_SIZE <= 0
+#if BUF_SIZE <= 0 || BUF_SIZE > 65536
# undef BUF_SIZE
#endif
#ifndef BUF_SIZE
#if !DEBUG
# define MEM_ALLOC_FAIL() do { \
fprintf (stderr, "%s: memory allocation failure\n", program_name); \
- exit (2); \
+ exit (EXIT_FAILURE); \
} while (false)
#else
# define MEM_ALLOC_FAIL_DEBUG(file, line) do { \
fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
- exit (2); \
+ exit (EXIT_FAILURE); \
} while (false)
#endif
#define COLOR_SEP_CHAR '/'
-#define VERSION "0.53"
+#define VERSION "0.54"
typedef enum { false, true } bool;
{ "default", "49m" },
};
+struct bytes_size {
+ unsigned int size;
+ char unit;
+};
+
enum fmts {
FMT_GENERIC,
FMT_STRING,
static void free_wrap (void **);
static char *strdup_wrap (const char *);
static char *str_concat (const char *, const char *);
+static bool get_bytes_size (unsigned long, struct bytes_size *);
static char *get_file_type (mode_t);
static bool has_color_name (const char *, const char *);
static void vfprintf_diag (const char *, ...);
static void
print_version (void)
{
+#ifdef HAVE_VERSION
+# include "version.h"
+#else
+ const char *version = NULL;
+#endif
+ const char *version_prefix, *version_string;
const char *c_flags;
+ struct bytes_size bytes_size;
bool debug;
#ifdef CFLAGS
c_flags = to_str (CFLAGS);
#else
debug = false;
#endif
- printf ("%s v%s (compiled at %s, %s)\n", "colorize", VERSION, __DATE__, __TIME__);
+ version_prefix = version ? "" : "v";
+ version_string = version ? version : VERSION;
+ printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix, version_string, __DATE__, __TIME__);
+
printf ("Compiler flags: %s\n", c_flags);
- printf ("Buffer size: %u bytes\n", BUF_SIZE);
+ if (get_bytes_size (BUF_SIZE, &bytes_size))
+ {
+ if (BUF_SIZE % 1024 == 0)
+ printf ("Buffer size: %u%c\n", bytes_size.size, bytes_size.unit);
+ else
+ printf ("Buffer size: %u%c, %u byte%s\n", bytes_size.size, bytes_size.unit,
+ BUF_SIZE % 1024, BUF_SIZE % 1024 > 1 ? "s" : "");
+ }
+ else
+ printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE, BUF_SIZE > 1 ? "s" : "");
printf ("Debugging: %s\n", debug ? "yes" : "no");
}
MEM_ALLOC_FAIL_DEBUG (file, line);
return p;
}
-#endif
+#endif /* !DEBUG */
static void
free_wrap (void **ptr)
return str;
}
+static bool
+get_bytes_size (unsigned long bytes, struct bytes_size *bytes_size)
+{
+ const char *unit, units[] = { '0', 'K', 'M', 'G', '\0' };
+ unsigned long size = bytes;
+ if (bytes < 1024)
+ return false;
+ unit = units;
+ while (size >= 1024 && *(unit + 1))
+ {
+ size /= 1024;
+ unit++;
+ }
+ bytes_size->size = (unsigned int)size;
+ bytes_size->unit = *unit;
+ return true;
+}
+
static char *
get_file_type (mode_t mode)
{