#define COLOR_SEP_CHAR '/'
+#define DEBUG_FILE "debug.txt"
+
#define VERSION "0.54"
typedef enum { false, true } bool;
};
static FILE *stream = NULL;
+#if DEBUG
+static FILE *log = NULL;
+#endif
static unsigned int stacked_vars = 0;
static void **vars_list = NULL;
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 FILE *open_file (const char *, const char *);
static void vfprintf_diag (const char *, ...);
static void vfprintf_fail (const char *, ...);
static void stack_var (void ***, unsigned int *, unsigned int, void *);
setvbuf (stdout, NULL, _IOLBF, 0);
+#if DEBUG
+ log = open_file (DEBUG_FILE, "w");
+#endif
+
while ((opt = getopt_long (argc, argv, "hv", long_opts, NULL)) != -1)
{
PARSE_OPT:
if (stream && fileno (stream) != STDIN_FILENO)
fclose (stream);
+#if DEBUG
+ if (log)
+ fclose (log);
+#endif
if (vars_list)
{
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);
+ fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
return p;
}
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);
+ fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
return p;
}
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);
+ fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
return p;
}
#endif /* !DEBUG */
return true;
}
+static FILE *
+open_file (const char *file, const char *mode)
+{
+ FILE *stream;
+
+ errno = 0;
+ stream = fopen (file, mode);
+ if (!stream)
+ vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
+
+ return stream;
+}
+
#define DO_VFPRINTF(fmt) \
va_list ap; \
fprintf (stderr, "%s: ", program_name); \