+void exec_command(int nb, struct file_node **nodes) {
+ char **args;
+ int max_group_size = 0, group_size = 0, m, n, status;
+ pid_t pid;
+
+ for(n = 0; n < nb; n++) {
+ if(n > 0 && nodes[n]->group_id != nodes[n-1]->group_id) {
+ group_size = 0;
+ }
+ group_size++;
+ if(group_size > max_group_size) {
+ max_group_size = group_size;
+ }
+ }
+
+ args = safe_malloc((max_group_size + 2) * sizeof(char *));
+ args[0] = command_to_exec;
+
+ n = 0;
+ while(n < nb) {
+ m = n;
+ if(trim_first) { m++; }
+ while(n < nb && nodes[n]->group_id == nodes[m]->group_id) {
+ if(n >= m) {
+ args[n - m + 1] = nodes[n]->name;
+ }
+ n++;
+ }
+ args[n - m + 1] = 0;
+ pid = fork();
+ if(pid < 0) {
+ } else if(pid == 0) {
+ if(execvp(command_to_exec, args) < 0) {
+ perror("execvp");
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ while(wait(&status) != pid);
+ if(status > 0) {
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+
+ free(args);
+}
+
+void write_groups_in_files(int nb, struct file_node **nodes) {
+ FILE *file = 0;
+ int current_group = -1, n, first_of_group;
+ char filename[PATH_MAX + 1];
+
+ for(n = 0; n < nb; n++) {
+ first_of_group = (n == 0);
+ 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);
+ first_of_group = 1;
+ }
+
+ if(!trim_first || !first_of_group) {
+ write_one_entry_to_file(file, nodes[n]);
+ }
+ }
+
+ if(file) { fclose(file); }
+}