*
*/
-#define VERSION_NUMBER "1.1"
+#define VERSION_NUMBER "1.2"
#define _BSD_SOURCE
char *command_to_exec = 0; /* the name of the command to exec for each
group of identical files */
+char *result_file_prefix = 0; /* The prefix to use to write result */
+
/********************************************************************/
/* malloc with error checking. */
return result;
}
-void print_file(struct file_node *node) {
+void print_file(FILE *out, struct file_node *node) {
char tmp[PATH_MAX + 1];
if(show_realpaths) {
if(realpath(node->name, tmp)) {
if(show_groups) {
- printf("%d %s\n", node->group_id, tmp);
+ fprintf(out, "%d %s\n", node->group_id, tmp);
} else {
- printf("%s\n", tmp);
+ fprintf(out, "%s\n", tmp);
}
} else {
fprintf(stderr,
}
} else {
if(show_groups) {
- printf("%d %s\n", node->group_id, node->name);
+ fprintf(out, "%d %s\n", node->group_id, node->name);
} else {
- printf("%s\n", node->name);
+ fprintf(out, "%s\n", node->name);
}
}
}
free(args);
}
+void write_groups_in_files(int nb, struct file_node **nodes) {
+ FILE *file = 0;
+ int current_group = -1, n;
+ char filename[PATH_MAX + 1];
+
+ for(n = 0; n < nb; n++) {
+ if(nodes[n]->group_id != current_group) {
+ if(file) { fclose(file); }
+ sprintf(filename, "%s%06d", result_file_prefix, nodes[n]->group_id);
+ file = fopen(filename, "w");
+ current_group = nodes[n]->group_id;
+ printf("Writing %s.\n" , filename);
+ }
+ print_file(file, nodes[n]);
+ }
+
+ if(file) { fclose(file); }
+}
+
void print_result(struct file_node *list1, struct file_node *list2) {
struct file_node *node1, *node2;
struct file_node **nodes;
if(command_to_exec) {
exec_command(nb, nodes);
+ } else if(result_file_prefix) {
+ write_groups_in_files(nb, nodes);
} else {
for(n = 0; n < nb; n++) {
if(!show_groups && n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
printf("\n");
}
- print_file(nodes[n]);
+ print_file(stdout, nodes[n]);
}
}
fprintf(out, " -e <command>, --exec <command>\n");
fprintf(out, " execute the provided command for each group of\n");
fprintf(out, " identical files, with their names as arguments\n");
+ fprintf(out, " -f <string>, --result-prefix <string>\n");
+ fprintf(out, " for each group of identical files, write one\n");
+ fprintf(out, " result file whose name is the given prefix string\n");
+ fprintf(out, " followed the group number, and containing one\n");
+ fprintf(out, " filename per line\n");
fprintf(out, "\n");
fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
}
{ "ignore-empty", no_argument, 0, '0' },
{ "show-progress", no_argument, 0, 'p' },
{ "exec", 1, 0, 'e' },
+ { "result-prefix", 1, 0, 'f' },
{ 0, 0, 0, 0 }
};
setlocale (LC_ALL, "");
- while ((c = getopt_long(argc, argv, "vhircgtd0pme:",
+ while ((c = getopt_long(argc, argv, "vhircgtd0pme:f:",
long_options, NULL)) != -1) {
switch (c) {
command_to_exec = strdup(optarg);
break;
+ case 'f':
+ if(result_file_prefix != 0) {
+ free(result_file_prefix);
+ }
+ result_file_prefix = strdup(optarg);
+ show_groups = 0;
+ break;
+
default:
usage(stderr);
exit(EXIT_FAILURE);