]>
git.refcnt.org Git - colorize.git/blob - colorize.c
2 * colorize - Read text from standard input stream or file and print
3 * it colorized through use of ANSI escape sequences
5 * Copyright (c) 2011-2014 Steven Schubiger
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #define _XOPEN_SOURCE 700
24 #define _FILE_OFFSET_BITS 64
34 #include <sys/types.h>
44 #define to_str(arg) str(arg)
46 #define streq(s1, s2) (strcmp (s1, s2) == 0)
49 # define xmalloc(size) malloc_wrap(size)
50 # define xcalloc(nmemb, size) calloc_wrap(nmemb, size)
51 # define xrealloc(ptr, size) realloc_wrap(ptr, size)
52 # define xstrdup(str) strdup_wrap(str, NULL, 0)
53 # define str_concat(str1, str2) str_concat_wrap(str1, str2, NULL, 0)
55 # define xmalloc(size) malloc_wrap_debug(size, __FILE__, __LINE__)
56 # define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
57 # define xrealloc(ptr, size) realloc_wrap_debug(ptr, size, __FILE__, __LINE__)
58 # define xstrdup(str) strdup_wrap(str, __FILE__, __LINE__)
59 # define str_concat(str1, str2) str_concat_wrap(str1, str2, __FILE__, __LINE__)
62 #define free_null(ptr) free_wrap((void **)&ptr)
64 #if defined(BUF_SIZE) && (BUF_SIZE <= 0 || BUF_SIZE > 65536)
68 # define BUF_SIZE 4096
74 #define SKIP_LINE_ENDINGS(flags) (((flags) & CR) && ((flags) & LF) ? 2 : 1)
76 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
78 #define STACK_VAR(ptr) do { \
79 stack_var (&vars_list, &stacked_vars, stacked_vars, ptr); \
82 #define RELEASE_VAR(ptr) do { \
83 release_var (vars_list, stacked_vars, (void **)&ptr); \
87 # define MEM_ALLOC_FAIL() do { \
88 fprintf (stderr, "%s: memory allocation failure\n", program_name); \
89 exit (EXIT_FAILURE); \
92 # define MEM_ALLOC_FAIL_DEBUG(file, line) do { \
93 fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
94 exit (EXIT_FAILURE); \
98 #define ABORT_TRACE() \
99 fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
102 #define CHECK_COLORS_RANDOM(color1, color2) \
103 streq (color_names[color1]->name, "random") \
104 && (streq (color_names[color2]->name, "none") \
105 || streq (color_names[color2]->name, "default")) \
107 #define COLOR_SEP_CHAR '/'
109 #define VERSION "0.54"
111 typedef enum { false, true } bool;
118 static struct color_name
*color_names
[3] = { NULL
, NULL
, NULL
};
125 static const struct color fg_colors
[] = {
132 { "magenta", "35m" },
135 { "default", "39m" },
137 static const struct color bg_colors
[] = {
144 { "magenta", "45m" },
147 { "default", "49m" },
165 static const char *formats
[] = {
167 "%s '%s'", /* string */
168 "%s `%s' %s", /* quote */
169 "%s color '%s' %s", /* color */
170 "%s color '%s' %s '%s'", /* random */
171 "less than %u bytes %s", /* error */
173 "%s: %s: %s", /* type */
176 enum { FOREGROUND
, BACKGROUND
};
178 static const struct {
179 struct color
const *entries
;
183 { fg_colors
, sizeof (fg_colors
) / sizeof (struct color
), "foreground" },
184 { bg_colors
, sizeof (bg_colors
) / sizeof (struct color
), "background" },
187 static FILE *stream
= NULL
;
189 static unsigned int stacked_vars
= 0;
190 static void **vars_list
= NULL
;
192 static bool clean
= false;
193 static bool clean_all
= false;
195 static char *exclude
= NULL
;
197 static const char *program_name
;
199 static void print_hint (void);
200 static void print_help (void);
201 static void print_version (void);
202 static void cleanup (void);
203 static void free_color_names (struct color_name
**);
204 static void process_args (unsigned int, char **, bool *, const struct color
**, const char **, FILE **);
205 static void process_file_arg (const char *, const char **, FILE **);
206 static void read_print_stream (bool, const struct color
**, const char *, FILE *);
207 static void find_color_entries (struct color_name
**, const struct color
**);
208 static void find_color_entry (const struct color_name
*, unsigned int, const struct color
**);
209 static void print_line (bool, const struct color
**, const char * const, unsigned int);
210 static void print_clean (const char *);
211 static void print_free_offsets (const char *, char ***, unsigned int);
213 static void *malloc_wrap (size_t);
214 static void *calloc_wrap (size_t, size_t);
215 static void *realloc_wrap (void *, size_t);
217 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
218 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
219 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
221 static void free_wrap (void **);
222 static char *strdup_wrap (const char *, const char *, unsigned int);
223 static char *str_concat_wrap (const char *, const char *, const char *, unsigned int);
224 static bool get_bytes_size (unsigned long, struct bytes_size
*);
225 static char *get_file_type (mode_t
);
226 static bool has_color_name (const char *, const char *);
227 static void vfprintf_diag (const char *, ...);
228 static void vfprintf_fail (const char *, ...);
229 static void stack_var (void ***, unsigned int *, unsigned int, void *);
230 static void release_var (void **, unsigned int, void **);
232 #define SET_OPT_TYPE(type) \
240 static int opt_type
= 0;
243 main (int argc
, char **argv
)
245 unsigned int arg_cnt
= 0;
256 struct option long_opts
[] = {
257 { "clean", no_argument
, &opt_type
, OPT_CLEAN
},
258 { "clean-all", no_argument
, &opt_type
, OPT_CLEAN_ALL
},
259 { "exclude-random", required_argument
, &opt_type
, OPT_EXCLUDE_RANDOM
},
260 { "help", no_argument
, &opt_type
, OPT_HELP
},
261 { "version", no_argument
, &opt_type
, OPT_VERSION
},
262 { NULL
, 0, NULL
, 0 },
267 const struct color
*colors
[2] = {
268 NULL
, /* foreground */
269 NULL
, /* background */
272 const char *file
= NULL
;
274 program_name
= argv
[0];
277 setvbuf (stdout
, NULL
, _IOLBF
, 0);
279 while ((opt
= getopt_long (argc
, argv
, "hv", long_opts
, NULL
)) != -1)
284 case 0: /* long opts */
293 case OPT_EXCLUDE_RANDOM
: {
296 exclude
= xstrdup (optarg
);
298 for (i
= 1; i
< tables
[FOREGROUND
].count
- 1; i
++) /* skip color none and default */
300 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
301 if (streq (exclude
, entry
->name
))
308 vfprintf_fail (formats
[FMT_GENERIC
], "--exclude-random switch must be provided a plain color");
317 default: /* never reached */
322 SET_OPT_TYPE (OPT_HELP
);
324 SET_OPT_TYPE (OPT_VERSION
);
328 default: /* never reached */
333 arg_cnt
= argc
- optind
;
335 if (clean
|| clean_all
)
337 if (clean
&& clean_all
)
338 vfprintf_fail (formats
[FMT_GENERIC
], "--clean and --clean-all switch are mutually exclusive");
341 const char *format
= "%s %s";
342 const char *message
= "switch cannot be used with more than one file";
344 vfprintf_fail (format
, "--clean", message
);
346 vfprintf_fail (format
, "--clean-all", message
);
351 if (arg_cnt
== 0 || arg_cnt
> 2)
353 vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt
);
359 if (clean
|| clean_all
)
360 process_file_arg (argv
[optind
], &file
, &stream
);
362 process_args (arg_cnt
, &argv
[optind
], &bold
, colors
, &file
, &stream
);
363 read_print_stream (bold
, colors
, file
, stream
);
365 RELEASE_VAR (exclude
);
373 fprintf (stderr
, "Type `%s --help' for help screen.\n", program_name
);
381 printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name
, COLOR_SEP_CHAR
);
382 printf ("\tColors (foreground) (background)\n");
383 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
385 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
386 const char *name
= entry
->name
;
387 const char *code
= entry
->code
;
389 printf ("\t\t{\033[%s#\033[0m} [%c%c]%s%*s%s\n",
390 code
, toupper (*name
), *name
, name
+ 1, 10 - (int)strlen (name
), " ", name
);
392 printf ("\t\t{-} %s%*s%s\n", name
, 13 - (int)strlen (name
), " ", name
);
394 printf ("\t\t{*} [Rr]%s%*s%s [--exclude-random=<foreground color>]\n", "andom", 10 - (int)strlen ("random"), " ", "random");
396 printf ("\n\tFirst character of color name in upper case denotes increased intensity,\n");
397 printf ("\twhereas for lower case colors will be of normal intensity.\n");
399 printf ("\n\tOptions\n");
400 printf ("\t\t --clean\n");
401 printf ("\t\t --clean-all\n");
402 printf ("\t\t --exclude-random\n");
403 printf ("\t\t-h, --help\n");
404 printf ("\t\t-v, --version\n\n");
411 # include "version.h"
413 const char *version
= NULL
;
415 const char *version_prefix
, *version_string
;
417 struct bytes_size bytes_size
;
420 c_flags
= to_str (CFLAGS
);
429 version_prefix
= version
? "" : "v";
430 version_string
= version
? version
: VERSION
;
431 printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix
, version_string
, __DATE__
, __TIME__
);
433 printf ("Compiler flags: %s\n", c_flags
);
434 if (get_bytes_size (BUF_SIZE
, &bytes_size
))
436 if (BUF_SIZE
% 1024 == 0)
437 printf ("Buffer size: %u%c\n", bytes_size
.size
, bytes_size
.unit
);
439 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size
.size
, bytes_size
.unit
,
440 BUF_SIZE
% 1024, BUF_SIZE
% 1024 > 1 ? "s" : "");
443 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE
, BUF_SIZE
> 1 ? "s" : "");
444 printf ("Debugging: %s\n", debug
? "yes" : "no");
450 free_color_names (color_names
);
452 if (stream
&& fileno (stream
) != STDIN_FILENO
)
458 for (i
= 0; i
< stacked_vars
; i
++)
460 free_null (vars_list
[i
]);
462 free_null (vars_list
);
467 free_color_names (struct color_name
**color_names
)
470 for (i
= 0; color_names
[i
]; i
++)
472 free_null (color_names
[i
]->name
);
473 free_null (color_names
[i
]->orig
);
474 free_null (color_names
[i
]);
479 process_args (unsigned int arg_cnt
, char **arg_strings
, bool *bold
, const struct color
**colors
, const char **file
, FILE **stream
)
483 char *color
, *p
, *str
;
486 const char *color_string
= arg_cnt
>= 1 ? arg_strings
[0] : NULL
;
487 const char *file_string
= arg_cnt
== 2 ? arg_strings
[1] : NULL
;
489 assert (color_string
);
491 if (streq (color_string
, "-"))
494 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen cannot be used as color string");
496 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen must be preceeded by color string");
499 ret
= lstat (color_string
, &sb
);
501 /* Ensure that we don't fail if there's a file with one or more
502 color names in its path. */
503 if (ret
== 0) /* success */
507 const char *color
= color_string
;
508 const mode_t mode
= sb
.st_mode
;
510 for (c
= 1; c
<= 2 && *color
; c
++)
512 bool matched
= false;
514 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
516 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
517 if (has_color_name (color
, entry
->name
))
519 color
+= strlen (entry
->name
);
524 if (!matched
&& has_color_name (color
, "random"))
526 color
+= strlen ("random");
529 if (matched
&& *color
== COLOR_SEP_CHAR
&& *(color
+ 1))
535 have_file
= (*color
!= '\0');
539 const char *file_exists
= color_string
;
541 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "cannot be used as color string");
544 if (VALID_FILE_TYPE (mode
))
545 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "must be preceeded by color string");
547 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "is not a valid file type");
552 if ((p
= strchr (color_string
, COLOR_SEP_CHAR
)))
554 if (p
== color_string
)
555 vfprintf_fail (formats
[FMT_STRING
], "foreground color missing in string", color_string
);
556 else if (p
== color_string
+ strlen (color_string
) - 1)
557 vfprintf_fail (formats
[FMT_STRING
], "background color missing in string", color_string
);
558 else if (strchr (++p
, COLOR_SEP_CHAR
))
559 vfprintf_fail (formats
[FMT_STRING
], "one color pair allowed only for string", color_string
);
562 str
= xstrdup (color_string
);
565 for (index
= 0, color
= str
; *color
; index
++, color
= p
)
570 if ((sep
= strchr (color
, COLOR_SEP_CHAR
)))
576 p
= color
+ strlen (color
);
579 for (ch
= color
; *ch
; ch
++)
581 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be made of non-alphabetic characters");
583 for (ch
= color
+ 1; *ch
; ch
++)
585 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be in mixed lower/upper case");
587 if (streq (color
, "None"))
588 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be bold");
590 if (isupper (*color
))
598 vfprintf_fail (formats
[FMT_COLOR
], tables
[BACKGROUND
].desc
, color
, "cannot be bold");
600 default: /* never reached */
605 color_names
[index
] = xcalloc (1, sizeof (struct color_name
));
607 color_names
[index
]->orig
= xstrdup (color
);
609 for (ch
= color
; *ch
; ch
++)
612 color_names
[index
]->name
= xstrdup (color
);
617 assert (color_names
[FOREGROUND
]);
619 if (color_names
[BACKGROUND
])
622 unsigned int color_sets
[2][2] = { { FOREGROUND
, BACKGROUND
}, { BACKGROUND
, FOREGROUND
} };
623 for (i
= 0; i
< 2; i
++)
625 unsigned int color1
= color_sets
[i
][0];
626 unsigned int color2
= color_sets
[i
][1];
627 if (CHECK_COLORS_RANDOM (color1
, color2
))
628 vfprintf_fail (formats
[FMT_RANDOM
], tables
[color1
].desc
, color_names
[color1
]->orig
, "cannot be combined with", color_names
[color2
]->orig
);
632 find_color_entries (color_names
, colors
);
633 free_color_names (color_names
);
635 if (!colors
[FOREGROUND
]->code
&& colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
637 struct color_name color_name
;
638 color_name
.name
= color_name
.orig
= "default";
640 find_color_entry (&color_name
, FOREGROUND
, colors
);
643 process_file_arg (file_string
, file
, stream
);
647 process_file_arg (const char *file_string
, const char **file
, FILE **stream
)
651 if (streq (file_string
, "-"))
656 const char *file
= file_string
;
661 ret
= lstat (file
, &sb
);
664 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
666 if (!VALID_FILE_TYPE (sb
.st_mode
))
667 vfprintf_fail (formats
[FMT_TYPE
], file
, "unrecognized type", get_file_type (sb
.st_mode
));
671 s
= fopen (file
, "r");
673 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
688 #define MERGE_PRINT_LINE(part_line, line, flags, check_eof) do { \
689 char *current_line, *merged_line = NULL; \
692 merged_line = str_concat (part_line, line); \
693 free_null (part_line); \
695 current_line = merged_line ? merged_line : (char *)line; \
696 if (!check_eof || *current_line != '\0') \
697 print_line (bold, colors, current_line, flags); \
698 free (merged_line); \
702 read_print_stream (bool bold
, const struct color
**colors
, const char *file
, FILE *stream
)
704 char buf
[BUF_SIZE
+ 1], *part_line
= NULL
;
705 unsigned int flags
= 0;
707 while (!feof (stream
))
712 memset (buf
, '\0', BUF_SIZE
+ 1);
713 bytes_read
= fread (buf
, 1, BUF_SIZE
, stream
);
714 if (bytes_read
!= BUF_SIZE
&& ferror (stream
))
715 vfprintf_fail (formats
[FMT_ERROR
], BUF_SIZE
, "read");
717 while ((eol
= strpbrk (line
, "\n\r")))
724 if (*(eol
+ 1) == '\n')
727 else if (*eol
== '\n')
730 vfprintf_fail (formats
[FMT_FILE
], file
, "unrecognized line ending");
731 p
= eol
+ SKIP_LINE_ENDINGS (flags
);
733 MERGE_PRINT_LINE (part_line
, line
, flags
, false);
737 MERGE_PRINT_LINE (part_line
, line
, 0, true);
739 else if (*line
!= '\0')
741 if (!clean
&& !clean_all
) /* efficiency */
742 print_line (bold
, colors
, line
, 0);
744 part_line
= xstrdup (line
);
747 char *merged_line
= str_concat (part_line
, line
);
749 part_line
= merged_line
;
756 find_color_entries (struct color_name
**color_names
, const struct color
**colors
)
762 gettimeofday (&tv
, NULL
);
763 srand (tv
.tv_usec
* tv
.tv_sec
);
765 for (index
= 0; color_names
[index
]; index
++)
767 const char *color_name
= color_names
[index
]->name
;
769 const unsigned int count
= tables
[index
].count
;
770 const struct color
*const color_entries
= tables
[index
].entries
;
772 if (streq (color_name
, "random"))
778 i
= rand() % (count
- 2) + 1; /* omit color none and default */
782 /* --exclude-random */
783 if (exclude
&& streq (exclude
, color_entries
[i
].name
))
785 else if (color_names
[BACKGROUND
] && streq (color_names
[BACKGROUND
]->name
, color_entries
[i
].name
))
789 if (streq (colors
[FOREGROUND
]->name
, color_entries
[i
].name
))
792 default: /* never reached */
795 } while (excludable
);
796 colors
[index
] = (struct color
*)&color_entries
[i
];
799 find_color_entry (color_names
[index
], index
, colors
);
804 find_color_entry (const struct color_name
*color_name
, unsigned int index
, const struct color
**colors
)
809 const unsigned int count
= tables
[index
].count
;
810 const struct color
*const color_entries
= tables
[index
].entries
;
812 for (i
= 0; i
< count
; i
++)
813 if (streq (color_name
->name
, color_entries
[i
].name
))
815 colors
[index
] = (struct color
*)&color_entries
[i
];
820 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color_name
->orig
, "not recognized");
824 print_line (bool bold
, const struct color
**colors
, const char *const line
, unsigned int flags
)
827 if (clean
|| clean_all
)
831 /* Foreground color code is guaranteed to be set when background color code is present. */
832 if (colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
833 printf ("\033[%s", colors
[BACKGROUND
]->code
);
834 if (colors
[FOREGROUND
]->code
)
835 printf ("\033[%s%s%s\033[0m", bold
? "1;" : "", colors
[FOREGROUND
]->code
, line
);
837 printf (formats
[FMT_GENERIC
], line
);
846 print_clean (const char *line
)
849 char ***offsets
= NULL
;
850 unsigned int count
= 0, i
= 0;
855 if (*p
== 27 && *(p
+ 1) == '[')
857 const char *begin
= p
;
861 while (isdigit (*p
) || *p
== ';')
867 unsigned int iter
= 0;
870 check_values
= false;
879 else /* check range */
884 const unsigned int digits
= p
- digit
;
885 for (i
= 0; i
< digits
; i
++)
889 if (value
== 0) /* reset */
895 else if (value
== 1) /* bold */
897 bool discard
= false;
907 else if ((value
>= 30 && value
<= 37) || value
== 39) /* foreground colors */
909 else if ((value
>= 40 && value
<= 47) || value
== 49) /* background colors */
918 } while (iter
== 1 && check_values
);
922 const char *end
= p
++;
924 offsets
= xmalloc (++count
* sizeof (char **));
926 offsets
= xrealloc (offsets
, ++count
* sizeof (char **));
927 offsets
[i
] = xmalloc (2 * sizeof (char *));
928 offsets
[i
][0] = (char *)begin
; /* ESC */
929 offsets
[i
][1] = (char *)end
; /* m */
940 print_free_offsets (line
, offsets
, count
);
942 printf (formats
[FMT_GENERIC
], line
);
945 #define SET_CHAR(offset, new, old) \
949 #define RESTORE_CHAR(offset, old) \
953 print_free_offsets (const char *line
, char ***offsets
, unsigned int count
)
958 SET_CHAR (offsets
[0][0], '\0', &ch
);
959 printf (formats
[FMT_GENERIC
], line
);
960 RESTORE_CHAR (offsets
[0][0], ch
);
962 for (i
= 0; i
< count
; i
++)
965 bool next_offset
= false;
968 SET_CHAR (offsets
[i
+ 1][0], '\0', &ch
);
971 printf (formats
[FMT_GENERIC
], offsets
[i
][1] + 1);
973 RESTORE_CHAR (offsets
[i
+ 1][0], ch
);
975 for (i
= 0; i
< count
; i
++)
976 free_null (offsets
[i
]);
982 malloc_wrap (size_t size
)
984 void *p
= malloc (size
);
991 calloc_wrap (size_t nmemb
, size_t size
)
993 void *p
= calloc (nmemb
, size
);
1000 realloc_wrap (void *ptr
, size_t size
)
1002 void *p
= realloc (ptr
, size
);
1009 malloc_wrap_debug (size_t size
, const char *file
, unsigned int line
)
1011 void *p
= malloc (size
);
1013 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1014 vfprintf_diag ("malloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size
, file
, line
);
1019 calloc_wrap_debug (size_t nmemb
, size_t size
, const char *file
, unsigned int line
)
1021 void *p
= calloc (nmemb
, size
);
1023 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1024 vfprintf_diag ("calloc'ed %lu bytes [source file %s, line %u]", (unsigned long)(nmemb
* size
), file
, line
);
1029 realloc_wrap_debug (void *ptr
, size_t size
, const char *file
, unsigned int line
)
1031 void *p
= realloc (ptr
, size
);
1033 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1034 vfprintf_diag ("realloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size
, file
, line
);
1040 free_wrap (void **ptr
)
1047 # define do_malloc(len, file, line) malloc_wrap(len)
1049 # define do_malloc(len, file, line) malloc_wrap_debug(len, file, line)
1053 strdup_wrap (const char *str
, const char *file
, unsigned int line
)
1055 const size_t len
= strlen (str
) + 1;
1056 char *p
= do_malloc (len
, file
, line
);
1057 strncpy (p
, str
, len
);
1062 str_concat_wrap (const char *str1
, const char *str2
, const char *file
, unsigned int line
)
1064 const size_t len
= strlen (str1
) + strlen (str2
) + 1;
1067 p
= str
= do_malloc (len
, file
, line
);
1068 strncpy (p
, str1
, strlen (str1
));
1070 strncpy (p
, str2
, strlen (str2
));
1078 get_bytes_size (unsigned long bytes
, struct bytes_size
*bytes_size
)
1080 const char *unit
, units
[] = { '0', 'K', 'M', 'G', '\0' };
1081 unsigned long size
= bytes
;
1085 while (size
>= 1024 && *(unit
+ 1))
1090 bytes_size
->size
= (unsigned int)size
;
1091 bytes_size
->unit
= *unit
;
1096 get_file_type (mode_t mode
)
1100 else if (S_ISDIR (mode
))
1102 else if (S_ISCHR (mode
))
1103 return "character device";
1104 else if (S_ISBLK (mode
))
1105 return "block device";
1106 else if (S_ISFIFO (mode
))
1107 return "named pipe";
1108 else if (S_ISLNK (mode
))
1109 return "symbolic link";
1110 else if (S_ISSOCK (mode
))
1117 has_color_name (const char *str
, const char *name
)
1121 assert (strlen (str
));
1122 assert (strlen (name
));
1124 if (!(*str
== *name
|| *str
== toupper (*name
)))
1126 else if (*(name
+ 1) != '\0'
1127 && !((p
= strstr (str
+ 1, name
+ 1)) && p
== str
+ 1))
1133 #define DO_VFPRINTF(fmt) \
1135 fprintf (stderr, "%s: ", program_name); \
1136 va_start (ap, fmt); \
1137 vfprintf (stderr, fmt, ap); \
1139 fprintf (stderr, "\n"); \
1142 vfprintf_diag (const char *fmt
, ...)
1148 vfprintf_fail (const char *fmt
, ...)
1151 exit (EXIT_FAILURE
);
1155 stack_var (void ***list
, unsigned int *stacked
, unsigned int index
, void *ptr
)
1157 /* nothing to stack */
1161 *list
= xmalloc (sizeof (void *));
1165 for (i
= 0; i
< *stacked
; i
++)
1169 return; /* reused */
1171 *list
= xrealloc (*list
, (*stacked
+ 1) * sizeof (void *));
1173 (*list
)[index
] = ptr
;
1178 release_var (void **list
, unsigned int stacked
, void **ptr
)
1181 /* nothing to release */
1184 for (i
= 0; i
< stacked
; i
++)
1185 if (list
[i
] == *ptr
)