]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
Make diagnostics more consistent
[colorize.git] / colorize.c
index 26eaa2f5ee9092ac7d11862cf3e9b4ae741af604..c316f17d6023ba48f29b4bef7148254a1498425e 100644 (file)
@@ -57,7 +57,7 @@
 #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
 
@@ -143,6 +143,11 @@ static const struct color bg_colors[] = {
     { "default", "49m" },
 };
 
+struct bytes_size {
+    unsigned int size;
+    char unit;
+};
+
 enum fmts {
     FMT_GENERIC,
     FMT_STRING,
@@ -212,6 +217,7 @@ static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
 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 *, ...);
@@ -397,7 +403,14 @@ print_help (void)
 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);
@@ -409,9 +422,21 @@ print_version (void)
 #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");
 }
 
@@ -982,6 +1007,7 @@ malloc_wrap_debug (size_t size, const char *file, unsigned int line)
     void *p = malloc (size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
+    vfprintf_diag ("malloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size, file, line);
     return p;
 }
 
@@ -991,6 +1017,7 @@ calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int lin
     void *p = calloc (nmemb, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
+    vfprintf_diag ("calloc'ed %lu bytes [source file %s, line %u]", (unsigned long)(nmemb * size), file, line);
     return p;
 }
 
@@ -1000,9 +1027,10 @@ realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
     void *p = realloc (ptr, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
+    vfprintf_diag ("realloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size, file, line);
     return p;
 }
-#endif
+#endif /* !DEBUG */
 
 static void
 free_wrap (void **ptr)
@@ -1036,6 +1064,24 @@ str_concat (const char *str1, const char *str2)
     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)
 {