]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
Merge duplicated version printing code
[colorize.git] / colorize.c
index b7974b1e7f721c284e9edbf5f7471d59860caca9..7704c3b8f94af8e2befcdd95f03310d0dc9d5151 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
 
 #define COLOR_SEP_CHAR '/'
 
-#define VERSION "0.53"
+#define VERSION "0.54"
 
 typedef enum { false, true } bool;
 
@@ -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");
 }
 
@@ -626,7 +651,7 @@ process_file_arg (const char *file_string, const char **file, FILE **stream)
             FILE *s;
             const char *file = file_string;
             struct stat sb;
-            int errno, ret;
+            int ret;
 
             errno = 0;
             ret = lstat (file, &sb);
@@ -1002,7 +1027,7 @@ realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
       MEM_ALLOC_FAIL_DEBUG (file, line);
     return p;
 }
-#endif
+#endif /* !DEBUG */
 
 static void
 free_wrap (void **ptr)
@@ -1036,6 +1061,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)
 {