]>
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
33 #include <sys/types.h>
43 #define to_str(arg) str(arg)
45 #define streq(s1, s2) (strcmp (s1, s2) == 0)
48 # define xmalloc(size) malloc_wrap(size)
49 # define xcalloc(nmemb, size) calloc_wrap(nmemb, size)
50 # define xrealloc(ptr, size) realloc_wrap(ptr, size)
52 # define xmalloc(size) malloc_wrap_debug(size, __FILE__, __LINE__)
53 # define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
54 # define xrealloc(ptr, size) realloc_wrap_debug(ptr, size, __FILE__, __LINE__)
57 #define free_null(ptr) free_wrap((void **)&ptr)
58 #define xstrdup(str) strdup_wrap(str)
60 #if BUF_SIZE <= 0 || BUF_SIZE > 65536
64 # define BUF_SIZE 4096
70 #define SKIP_LINE_ENDINGS(flags) (((flags) & CR) && ((flags) & LF) ? 2 : 1)
72 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
74 #define STACK_VAR(ptr) do { \
75 stack_var (&vars_list, &stacked_vars, stacked_vars, ptr); \
78 #define RELEASE_VAR(ptr) do { \
79 release_var (vars_list, stacked_vars, (void **)&ptr); \
83 # define MEM_ALLOC_FAIL() do { \
84 fprintf (stderr, "%s: memory allocation failure\n", program_name); \
88 # define MEM_ALLOC_FAIL_DEBUG(file, line) do { \
89 fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
94 #define ABORT_TRACE() \
95 fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
98 #define CHECK_COLORS_RANDOM(color1, color2) \
99 streq (color_names[color1]->name, "random") \
100 && (streq (color_names[color2]->name, "none") \
101 || streq (color_names[color2]->name, "default")) \
103 #define COLOR_SEP_CHAR '/'
105 #define VERSION "0.54"
107 typedef enum { false, true } bool;
114 static struct color_name
*color_names
[3] = { NULL
, NULL
, NULL
};
121 static const struct color fg_colors
[] = {
128 { "magenta", "35m" },
131 { "default", "39m" },
133 static const struct color bg_colors
[] = {
140 { "magenta", "45m" },
143 { "default", "49m" },
161 static const char *formats
[] = {
163 "%s '%s'", /* string */
164 "%s `%s' %s", /* quote */
165 "%s color '%s' %s", /* color */
166 "%s color '%s' %s '%s'", /* random */
167 "less than %u bytes %s", /* error */
169 "%s: %s: %s", /* type */
172 enum { FOREGROUND
, BACKGROUND
};
174 static const struct {
175 struct color
const *entries
;
179 { fg_colors
, sizeof (fg_colors
) / sizeof (struct color
), "foreground" },
180 { bg_colors
, sizeof (bg_colors
) / sizeof (struct color
), "background" },
183 static FILE *stream
= NULL
;
185 static unsigned int stacked_vars
= 0;
186 static void **vars_list
= NULL
;
188 static bool clean
= false;
189 static bool clean_all
= false;
191 static char *exclude
= NULL
;
193 static const char *program_name
;
195 static void print_hint (void);
196 static void print_help (void);
197 static void print_version (void);
198 static void cleanup (void);
199 static void free_color_names (struct color_name
**);
200 static void process_args (unsigned int, char **, bool *, const struct color
**, const char **, FILE **);
201 static void process_file_arg (const char *, const char **, FILE **);
202 static void read_print_stream (bool, const struct color
**, const char *, FILE *);
203 static void find_color_entries (struct color_name
**, const struct color
**);
204 static void find_color_entry (const struct color_name
*, unsigned int, const struct color
**);
205 static void print_line (bool, const struct color
**, const char * const, unsigned int);
206 static void print_clean (const char *);
207 static void print_free_offsets (const char *, char ***, unsigned int);
209 static void *malloc_wrap (size_t);
210 static void *calloc_wrap (size_t, size_t);
211 static void *realloc_wrap (void *, size_t);
213 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
214 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
215 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
217 static void free_wrap (void **);
218 static char *strdup_wrap (const char *);
219 static char *str_concat (const char *, const char *);
220 static bool get_bytes_size (unsigned long, struct bytes_size
*);
221 static char *get_file_type (mode_t
);
222 static bool has_color_name (const char *, const char *);
223 static void vfprintf_diag (const char *, ...);
224 static void vfprintf_fail (const char *, ...);
225 static void stack_var (void ***, unsigned int *, unsigned int, void *);
226 static void release_var (void **, unsigned int, void **);
228 #define SET_OPT_TYPE(type) \
236 static int opt_type
= 0;
239 main (int argc
, char **argv
)
241 unsigned int arg_cnt
= 0;
252 struct option long_opts
[] = {
253 { "clean", no_argument
, &opt_type
, OPT_CLEAN
},
254 { "clean-all", no_argument
, &opt_type
, OPT_CLEAN_ALL
},
255 { "exclude-random", required_argument
, &opt_type
, OPT_EXCLUDE_RANDOM
},
256 { "help", no_argument
, &opt_type
, OPT_HELP
},
257 { "version", no_argument
, &opt_type
, OPT_VERSION
},
258 { NULL
, 0, NULL
, 0 },
263 const struct color
*colors
[2] = {
264 NULL
, /* foreground */
265 NULL
, /* background */
268 const char *file
= NULL
;
270 program_name
= argv
[0];
273 setvbuf (stdout
, NULL
, _IOLBF
, 0);
275 while ((opt
= getopt_long (argc
, argv
, "hv", long_opts
, NULL
)) != -1)
280 case 0: /* long opts */
289 case OPT_EXCLUDE_RANDOM
: {
292 exclude
= xstrdup (optarg
);
294 for (i
= 1; i
< tables
[FOREGROUND
].count
- 1; i
++) /* skip color none and default */
296 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
297 if (streq (exclude
, entry
->name
))
304 vfprintf_fail (formats
[FMT_GENERIC
], "--exclude-random switch must be provided a plain color");
313 default: /* never reached */
318 SET_OPT_TYPE (OPT_HELP
);
320 SET_OPT_TYPE (OPT_VERSION
);
324 default: /* never reached */
329 arg_cnt
= argc
- optind
;
331 if (clean
|| clean_all
)
333 if (clean
&& clean_all
)
334 vfprintf_fail (formats
[FMT_GENERIC
], "--clean and --clean-all switch are mutually exclusive");
337 const char *format
= "%s %s";
338 const char *message
= "switch cannot be used with more than one file";
340 vfprintf_fail (format
, "--clean", message
);
342 vfprintf_fail (format
, "--clean-all", message
);
347 if (arg_cnt
== 0 || arg_cnt
> 2)
349 vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt
);
355 if (clean
|| clean_all
)
356 process_file_arg (argv
[optind
], &file
, &stream
);
358 process_args (arg_cnt
, &argv
[optind
], &bold
, colors
, &file
, &stream
);
359 read_print_stream (bold
, colors
, file
, stream
);
361 RELEASE_VAR (exclude
);
369 fprintf (stderr
, "Type `%s --help' for help screen.\n", program_name
);
377 printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name
, COLOR_SEP_CHAR
);
378 printf ("\tColors (foreground) (background)\n");
379 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
381 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
382 const char *name
= entry
->name
;
383 const char *code
= entry
->code
;
385 printf ("\t\t{\033[%s#\033[0m} [%c%c]%s%*s%s\n",
386 code
, toupper (*name
), *name
, name
+ 1, 10 - (int)strlen (name
), " ", name
);
388 printf ("\t\t{-} %s%*s%s\n", name
, 13 - (int)strlen (name
), " ", name
);
390 printf ("\t\t{*} [Rr]%s%*s%s [--exclude-random=<foreground color>]\n", "andom", 10 - (int)strlen ("random"), " ", "random");
392 printf ("\n\tFirst character of color name in upper case denotes increased intensity,\n");
393 printf ("\twhereas for lower case colors will be of normal intensity.\n");
395 printf ("\n\tOptions\n");
396 printf ("\t\t --clean\n");
397 printf ("\t\t --clean-all\n");
398 printf ("\t\t --exclude-random\n");
399 printf ("\t\t-h, --help\n");
400 printf ("\t\t-v, --version\n\n");
407 # include "version.h"
409 const char *version
= NULL
;
412 struct bytes_size bytes_size
;
415 c_flags
= to_str (CFLAGS
);
425 printf ("colorize %s (compiled at %s, %s)\n", version
, __DATE__
, __TIME__
);
427 printf ("colorize v%s (compiled at %s, %s)\n", VERSION
, __DATE__
, __TIME__
);
428 printf ("Compiler flags: %s\n", c_flags
);
429 if (get_bytes_size (BUF_SIZE
, &bytes_size
))
431 if (BUF_SIZE
% 1024 == 0)
432 printf ("Buffer size: %u%c\n", bytes_size
.size
, bytes_size
.unit
);
434 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size
.size
, bytes_size
.unit
,
435 BUF_SIZE
% 1024, BUF_SIZE
% 1024 > 1 ? "s" : "");
438 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE
, BUF_SIZE
> 1 ? "s" : "");
439 printf ("Debugging: %s\n", debug
? "yes" : "no");
445 free_color_names (color_names
);
447 if (stream
&& fileno (stream
) != STDIN_FILENO
)
453 for (i
= 0; i
< stacked_vars
; i
++)
455 free_null (vars_list
[i
]);
457 free_null (vars_list
);
462 free_color_names (struct color_name
**color_names
)
465 for (i
= 0; color_names
[i
]; i
++)
467 free_null (color_names
[i
]->name
);
468 free_null (color_names
[i
]->orig
);
469 free_null (color_names
[i
]);
474 process_args (unsigned int arg_cnt
, char **arg_strings
, bool *bold
, const struct color
**colors
, const char **file
, FILE **stream
)
478 char *color
, *p
, *str
;
481 const char *color_string
= arg_cnt
>= 1 ? arg_strings
[0] : NULL
;
482 const char *file_string
= arg_cnt
== 2 ? arg_strings
[1] : NULL
;
484 assert (color_string
);
486 if (streq (color_string
, "-"))
489 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen cannot be used as color string");
491 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen must be preceeded by color string");
494 ret
= lstat (color_string
, &sb
);
496 /* Ensure that we don't fail if there's a file with one or more
497 color names in its path. */
502 const char *color
= color_string
;
503 const mode_t mode
= sb
.st_mode
;
505 for (c
= 1; c
<= 2 && *color
; c
++)
507 bool matched
= false;
509 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
511 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
512 if (has_color_name (color
, entry
->name
))
514 color
+= strlen (entry
->name
);
519 if (!matched
&& has_color_name (color
, "random"))
521 color
+= strlen ("random");
524 if (matched
&& *color
== COLOR_SEP_CHAR
&& *(color
+ 1))
530 have_file
= (*color
!= '\0');
534 const char *file_exists
= color_string
;
536 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "cannot be used as color string");
539 if (VALID_FILE_TYPE (mode
))
540 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "must be preceeded by color string");
542 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "is not a valid file type");
547 if ((p
= strchr (color_string
, COLOR_SEP_CHAR
)))
549 if (p
== color_string
)
550 vfprintf_fail (formats
[FMT_STRING
], "foreground color missing in string", color_string
);
551 else if (p
== color_string
+ strlen (color_string
) - 1)
552 vfprintf_fail (formats
[FMT_STRING
], "background color missing in string", color_string
);
553 else if (strchr (++p
, COLOR_SEP_CHAR
))
554 vfprintf_fail (formats
[FMT_STRING
], "one color pair allowed only for string", color_string
);
557 str
= xstrdup (color_string
);
560 for (index
= 0, color
= str
; *color
; index
++, color
= p
)
565 if ((sep
= strchr (color
, COLOR_SEP_CHAR
)))
571 p
= color
+ strlen (color
);
574 for (ch
= color
; *ch
; ch
++)
576 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be made of non-alphabetic characters");
578 for (ch
= color
+ 1; *ch
; ch
++)
580 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be in mixed lower/upper case");
582 if (streq (color
, "None"))
583 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be bold");
585 if (isupper (*color
))
593 vfprintf_fail (formats
[FMT_COLOR
], tables
[BACKGROUND
].desc
, color
, "cannot be bold");
595 default: /* never reached */
600 color_names
[index
] = xcalloc (1, sizeof (struct color_name
));
602 color_names
[index
]->orig
= xstrdup (color
);
604 for (ch
= color
; *ch
; ch
++)
607 color_names
[index
]->name
= xstrdup (color
);
612 assert (color_names
[FOREGROUND
]);
614 if (color_names
[BACKGROUND
])
617 unsigned int color_sets
[2][2] = { { FOREGROUND
, BACKGROUND
}, { BACKGROUND
, FOREGROUND
} };
618 for (i
= 0; i
< 2; i
++)
620 unsigned int color1
= color_sets
[i
][0];
621 unsigned int color2
= color_sets
[i
][1];
622 if (CHECK_COLORS_RANDOM (color1
, color2
))
623 vfprintf_fail (formats
[FMT_RANDOM
], tables
[color1
].desc
, color_names
[color1
]->orig
, "cannot be combined with", color_names
[color2
]->orig
);
627 find_color_entries (color_names
, colors
);
628 free_color_names (color_names
);
630 if (!colors
[FOREGROUND
]->code
&& colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
632 struct color_name color_name
;
633 color_name
.name
= color_name
.orig
= "default";
635 find_color_entry (&color_name
, FOREGROUND
, colors
);
638 process_file_arg (file_string
, file
, stream
);
642 process_file_arg (const char *file_string
, const char **file
, FILE **stream
)
646 if (streq (file_string
, "-"))
651 const char *file
= file_string
;
656 ret
= lstat (file
, &sb
);
659 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
661 if (!VALID_FILE_TYPE (sb
.st_mode
))
662 vfprintf_fail (formats
[FMT_TYPE
], file
, "unrecognized type", get_file_type (sb
.st_mode
));
666 s
= fopen (file
, "r");
668 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
683 #define MERGE_PRINT_LINE(part_line, line, flags, check_eof) do { \
684 char *current_line, *merged_line = NULL; \
687 merged_line = str_concat (part_line, line); \
688 free_null (part_line); \
690 current_line = merged_line ? merged_line : (char *)line; \
691 if (!check_eof || *current_line != '\0') \
692 print_line (bold, colors, current_line, flags); \
693 free (merged_line); \
697 read_print_stream (bool bold
, const struct color
**colors
, const char *file
, FILE *stream
)
699 char buf
[BUF_SIZE
+ 1], *part_line
= NULL
;
700 unsigned int flags
= 0;
702 while (!feof (stream
))
707 memset (buf
, '\0', BUF_SIZE
+ 1);
708 bytes_read
= fread (buf
, 1, BUF_SIZE
, stream
);
709 if (bytes_read
!= BUF_SIZE
&& ferror (stream
))
710 vfprintf_fail (formats
[FMT_ERROR
], BUF_SIZE
, "read");
712 while ((eol
= strpbrk (line
, "\n\r")))
719 if (*(eol
+ 1) == '\n')
722 else if (*eol
== '\n')
725 vfprintf_fail (formats
[FMT_FILE
], file
, "unrecognized line ending");
726 p
= eol
+ SKIP_LINE_ENDINGS (flags
);
728 MERGE_PRINT_LINE (part_line
, line
, flags
, false);
732 MERGE_PRINT_LINE (part_line
, line
, 0, true);
734 else if (*line
!= '\0')
736 if (!clean
&& !clean_all
) /* efficiency */
737 print_line (bold
, colors
, line
, 0);
739 part_line
= xstrdup (line
);
742 char *merged_line
= str_concat (part_line
, line
);
744 part_line
= merged_line
;
751 find_color_entries (struct color_name
**color_names
, const struct color
**colors
)
757 gettimeofday (&tv
, NULL
);
758 srand (tv
.tv_usec
* tv
.tv_sec
);
760 for (index
= 0; color_names
[index
]; index
++)
762 const char *color_name
= color_names
[index
]->name
;
764 const unsigned int count
= tables
[index
].count
;
765 const struct color
*const color_entries
= tables
[index
].entries
;
767 if (streq (color_name
, "random"))
773 i
= rand() % (count
- 2) + 1; /* omit color none and default */
777 /* --exclude-random */
778 if (exclude
&& streq (exclude
, color_entries
[i
].name
))
780 else if (color_names
[BACKGROUND
] && streq (color_names
[BACKGROUND
]->name
, color_entries
[i
].name
))
784 if (streq (colors
[FOREGROUND
]->name
, color_entries
[i
].name
))
787 default: /* never reached */
790 } while (excludable
);
791 colors
[index
] = (struct color
*)&color_entries
[i
];
794 find_color_entry (color_names
[index
], index
, colors
);
799 find_color_entry (const struct color_name
*color_name
, unsigned int index
, const struct color
**colors
)
804 const unsigned int count
= tables
[index
].count
;
805 const struct color
*const color_entries
= tables
[index
].entries
;
807 for (i
= 0; i
< count
; i
++)
808 if (streq (color_name
->name
, color_entries
[i
].name
))
810 colors
[index
] = (struct color
*)&color_entries
[i
];
815 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color_name
->orig
, "not recognized");
819 print_line (bool bold
, const struct color
**colors
, const char *const line
, unsigned int flags
)
822 if (clean
|| clean_all
)
826 /* Foreground color code is guaranteed to be set when background color code is present. */
827 if (colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
828 printf ("\033[%s", colors
[BACKGROUND
]->code
);
829 if (colors
[FOREGROUND
]->code
)
830 printf ("\033[%s%s%s\033[0m", bold
? "1;" : "", colors
[FOREGROUND
]->code
, line
);
832 printf (formats
[FMT_GENERIC
], line
);
841 print_clean (const char *line
)
844 char ***offsets
= NULL
;
845 unsigned int count
= 0, i
= 0;
850 if (*p
== 27 && *(p
+ 1) == '[')
852 const char *begin
= p
;
856 while (isdigit (*p
) || *p
== ';')
862 unsigned int iter
= 0;
865 check_values
= false;
874 else /* check range */
879 const unsigned int digits
= p
- digit
;
880 for (i
= 0; i
< digits
; i
++)
884 if (value
== 0) /* reset */
890 else if (value
== 1) /* bold */
892 bool discard
= false;
902 else if ((value
>= 30 && value
<= 37) || value
== 39) /* foreground colors */
904 else if ((value
>= 40 && value
<= 47) || value
== 49) /* background colors */
913 } while (iter
== 1 && check_values
);
917 const char *end
= p
++;
919 offsets
= xmalloc (++count
* sizeof (char **));
921 offsets
= xrealloc (offsets
, ++count
* sizeof (char **));
922 offsets
[i
] = xmalloc (2 * sizeof (char *));
923 offsets
[i
][0] = (char *)begin
; /* ESC */
924 offsets
[i
][1] = (char *)end
; /* m */
935 print_free_offsets (line
, offsets
, count
);
937 printf (formats
[FMT_GENERIC
], line
);
940 #define SET_CHAR(offset, new, old) \
944 #define RESTORE_CHAR(offset, old) \
948 print_free_offsets (const char *line
, char ***offsets
, unsigned int count
)
953 SET_CHAR (offsets
[0][0], '\0', &ch
);
954 printf (formats
[FMT_GENERIC
], line
);
955 RESTORE_CHAR (offsets
[0][0], ch
);
957 for (i
= 0; i
< count
; i
++)
960 bool next_offset
= false;
963 SET_CHAR (offsets
[i
+ 1][0], '\0', &ch
);
966 printf (formats
[FMT_GENERIC
], offsets
[i
][1] + 1);
968 RESTORE_CHAR (offsets
[i
+ 1][0], ch
);
970 for (i
= 0; i
< count
; i
++)
971 free_null (offsets
[i
]);
977 malloc_wrap (size_t size
)
979 void *p
= malloc (size
);
986 calloc_wrap (size_t nmemb
, size_t size
)
988 void *p
= calloc (nmemb
, size
);
995 realloc_wrap (void *ptr
, size_t size
)
997 void *p
= realloc (ptr
, size
);
1004 malloc_wrap_debug (size_t size
, const char *file
, unsigned int line
)
1006 void *p
= malloc (size
);
1008 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1013 calloc_wrap_debug (size_t nmemb
, size_t size
, const char *file
, unsigned int line
)
1015 void *p
= calloc (nmemb
, size
);
1017 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1022 realloc_wrap_debug (void *ptr
, size_t size
, const char *file
, unsigned int line
)
1024 void *p
= realloc (ptr
, size
);
1026 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1032 free_wrap (void **ptr
)
1039 strdup_wrap (const char *str
)
1041 const size_t len
= strlen (str
) + 1;
1042 char *p
= xmalloc (len
);
1043 strncpy (p
, str
, len
);
1048 str_concat (const char *str1
, const char *str2
)
1050 const size_t len
= strlen (str1
) + strlen (str2
) + 1;
1053 p
= str
= xmalloc (len
);
1054 strncpy (p
, str1
, strlen (str1
));
1056 strncpy (p
, str2
, strlen (str2
));
1064 get_bytes_size (unsigned long bytes
, struct bytes_size
*bytes_size
)
1066 const char *unit
, units
[] = { '0', 'K', 'M', 'G', '\0' };
1067 unsigned long size
= bytes
;
1071 while (size
>= 1024 && *(unit
+ 1))
1076 bytes_size
->size
= (unsigned int)size
;
1077 bytes_size
->unit
= *unit
;
1082 get_file_type (mode_t mode
)
1086 else if (S_ISDIR (mode
))
1088 else if (S_ISCHR (mode
))
1089 return "character device";
1090 else if (S_ISBLK (mode
))
1091 return "block device";
1092 else if (S_ISFIFO (mode
))
1093 return "named pipe";
1094 else if (S_ISLNK (mode
))
1095 return "symbolic link";
1096 else if (S_ISSOCK (mode
))
1103 has_color_name (const char *str
, const char *name
)
1107 assert (strlen (str
));
1108 assert (strlen (name
));
1110 if (!(*str
== *name
|| *str
== toupper (*name
)))
1112 else if (*(name
+ 1) != '\0'
1113 && !((p
= strstr (str
+ 1, name
+ 1)) && p
== str
+ 1))
1119 #define DO_VFPRINTF(fmt) \
1121 fprintf (stderr, "%s: ", program_name); \
1122 va_start (ap, fmt); \
1123 vfprintf (stderr, fmt, ap); \
1125 fprintf (stderr, "\n"); \
1128 vfprintf_diag (const char *fmt
, ...)
1134 vfprintf_fail (const char *fmt
, ...)
1137 exit (EXIT_FAILURE
);
1141 stack_var (void ***list
, unsigned int *stacked
, unsigned int index
, void *ptr
)
1143 /* nothing to stack */
1147 *list
= xmalloc (sizeof (void *));
1151 for (i
= 0; i
< *stacked
; i
++)
1155 return; /* reused */
1157 *list
= xrealloc (*list
, (*stacked
+ 1) * sizeof (void *));
1159 (*list
)[index
] = ptr
;
1164 release_var (void **list
, unsigned int stacked
, void **ptr
)
1167 /* nothing to release */
1170 for (i
= 0; i
< stacked
; i
++)
1171 if (list
[i
] == *ptr
)