| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <alloca.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <string.h> |
| 25 |
#include <pathmax.h> |
| 26 |
#include <error.h> |
| 27 |
|
| 28 |
#include "idfile.h" |
| 29 |
#include "xnls.h" |
| 30 |
#include "iduglobal.h" |
| 31 |
|
| 32 |
static char const *root_name (char const *path); |
| 33 |
static char const *suff_name (char const *path); |
| 34 |
static int common_prefix_suffix (struct file_link const *flink_1, |
| 35 |
struct file_link const *flink_2); |
| 36 |
|
| 37 |
extern void usage (void); |
| 38 |
static struct file_link *cw_dlink; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static char const * |
| 43 |
root_name (char const *file_name) |
| 44 |
{ |
| 45 |
static char file_name_buffer[BUFSIZ]; |
| 46 |
char const *dot = strrchr (file_name, '.'); |
| 47 |
|
| 48 |
if (dot) |
| 49 |
stzncpy (file_name_buffer, file_name, dot - file_name); |
| 50 |
else |
| 51 |
strcpy (file_name_buffer, file_name); |
| 52 |
return file_name_buffer; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static char const * |
| 59 |
suff_name (char const *file_name) |
| 60 |
{ |
| 61 |
char const *dot = strrchr (file_name, '.'); |
| 62 |
return dot ? dot : ""; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static int |
| 70 |
common_prefix_suffix (struct file_link const *flink_1, struct file_link const *flink_2) |
| 71 |
{ |
| 72 |
return (flink_1->fl_parent == flink_2->fl_parent |
| 73 |
&& strequ (suff_name (flink_1->fl_name), suff_name (flink_2->fl_name))); |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
print_filenames (struct file_link **flinkv, enum separator_style separator_style) |
| 78 |
{ |
| 79 |
struct file_link const *arg; |
| 80 |
struct file_link const *dlink; |
| 81 |
int brace_is_open = 0; |
| 82 |
char *file_name = alloca (sizeof(char) * PATH_MAX); |
| 83 |
|
| 84 |
while (*flinkv) |
| 85 |
{ |
| 86 |
arg = *flinkv++; |
| 87 |
if (*flinkv && (separator_style == ss_braces) |
| 88 |
&& common_prefix_suffix (arg, *flinkv)) |
| 89 |
{ |
| 90 |
if (brace_is_open) |
| 91 |
printf (",%s", root_name (arg->fl_name)); |
| 92 |
else |
| 93 |
{ |
| 94 |
dlink = arg->fl_parent; |
| 95 |
if (dlink && dlink != cw_dlink) |
| 96 |
{ |
| 97 |
maybe_relative_file_name (file_name, dlink, cw_dlink); |
| 98 |
fputs (file_name, stdout); |
| 99 |
putchar ('/'); |
| 100 |
} |
| 101 |
printf ("{%s", root_name (arg->fl_name)); |
| 102 |
} |
| 103 |
brace_is_open = 1; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
if (brace_is_open) |
| 108 |
printf (",%s}%s", root_name (arg->fl_name), suff_name (arg->fl_name)); |
| 109 |
else |
| 110 |
{ |
| 111 |
maybe_relative_file_name (file_name, arg, cw_dlink); |
| 112 |
fputs (file_name, stdout); |
| 113 |
} |
| 114 |
brace_is_open = 0; |
| 115 |
if (*flinkv) |
| 116 |
{ |
| 117 |
if (separator_style == ss_newline) |
| 118 |
putchar ('\n'); |
| 119 |
else |
| 120 |
putchar (' '); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
putchar ('\n'); |
| 125 |
} |
| 126 |
|
| 127 |
enum separator_style |
| 128 |
parse_separator_style (char const *arg) |
| 129 |
{ |
| 130 |
MAYBE_RETURN_PREFIX_MATCH (arg, "braces", ss_braces); |
| 131 |
MAYBE_RETURN_PREFIX_MATCH (arg, "space", ss_space); |
| 132 |
MAYBE_RETURN_PREFIX_MATCH (arg, "newline", ss_newline); |
| 133 |
error (0, 0, _("invalid `--separator' style: `%s'"), arg); |
| 134 |
usage (); |
| 135 |
return ss_bogus; |
| 136 |
} |