В чем проблема, что какие-то полные траблы проиисходят со стабильностью ....
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DBG 1
int count (const char *dirpath);
int scan (const char *dirpath, char **dir);
int cmp (const char **dir_1, const char **dir_2, int len_1, int len_2);
//void print_dir (const char **);
int main (int argc, const char *argv[]) {
int len_1, len_2;
char **dir_1, **dir_2;
int i;
if (argc != 3) {
printf ("Must be two arguments.\n%s [path_to_dir_1] [path_to_dir_2]\n", argv[0]);
return 0;
}
len_1 = count (argv[1]);
len_2 = count (argv[2]);
dir_1 = (char **)(malloc(len_1*sizeof(char*)));
dir_2 = (char **)(malloc(len_2*sizeof(char*)));
i = 0;
while (i < len_1) {
dir_1 = (char *)(malloc(256*sizeof(char)));
i++;
}
i = 0;
while (i < len_2) {
dir_2 = (char *)(malloc(256*sizeof(char)));
i++;
}
scan (argv[1],dir_1);
scan (argv[2],dir_2);
cmp ((const char **)dir_1, (const char **)dir_2, len_1, len_2);
i = 0;
while (i < len_1) {
free (dir_1);
i++;
}
i = 0;
while (i < len_2) {
free (dir_2);
i++;
}
free (dir_1);
free (dir_2);
return 0;
}
int count (const char *dirpath) {
/* FILE *sh; */
int pipedes[2];
int cpid;
int status;
int len = 0;
const char *call_argv[] = { "./fprint", "c", "n", dirpath, NULL };
const char *call_envp[] = { NULL };
/*
sh = fopen ("./temporary.sh", "w");
if (sh == 0) {
printf ("Can not open file [2]\n");
return 0;
}
fprintf (sh, "#!/bin/bash\n./fprint c %s\n", dirpath);
fclose (sh);
system ("chmod +x ./temporary.sh");
*/
pipe (pipedes);
cpid = fork();
if (cpid == 0) {
close (pipedes[0]);
dup2 (pipedes[1], 2);
/* execve ("./temporary.sh", NULL, NULL); */
execve ("./fprint", call_argv, call_envp);
}
else {
char buf[1024];
int len_i;
close (pipedes[1]);
while ((len_i = read(pipedes[0],buf,1024)) != 0) {
len +=len_i;
/*printf ("%d\n", len_i);*/
}
close (pipedes[0]);
wait (&status);
/* remove ("./temporary.sh"); */
}
return len;
}
int scan (const char *dirpath, char **dir) {
int pipedes[2];
int cpid;
int status;
int len;
const char *call_argv[] = { "./fprint", "p", "n", dirpath, NULL };
const char *call_envp[] = { NULL };
pipe (pipedes);
cpid = fork();
if (cpid == 0) {
close (pipedes[0]);
dup2 (pipedes[1], 1);
execve ("./fprint", call_argv, call_envp);
}
else {
char buf[256];
int i = 0, j;
close (pipedes[1]);
while ((len = read(pipedes[0], buf, 256)) != 0) {
//strcpy (dir, buf);
j = 0;
while (j < len) {
dir[j] = buf[j];
j++;
}
i++;
}
close (pipedes[0]);
wait (&status);
}
return 0;
}
int cmp (const char **dir_1, const char **dir_2, int len_1, int len_2) {
int i = 0, j;
while (i < len_1) {
j = 0;
while (j < len_2) {
if (EQ(dir_1, dir_2[j], 256)) fprintf (stdout, "%s\n", dir_1);
j++;
}
i++;
}
return 0;
}
int EQ (const char *text_1, const char *text_2, int len) {
int i = 0, check = 1;
if (text_1 == 0 || text_2 == 0) return -1;
while (i < len && check) {
if (text_1 != text_2) check = check^check;
i++;
}
if (check) return 1;
return 0;
}