I am writing a file integrity program. I first scans through all of the directories, gathering file names. The problem I am having is in storing the file name and directory names. File names seem to bleed into the directory tree. The first 0a-9a directories are correct ,but once I hit the bottom of the fuction the files have bleed through. Attached is the entire source code, if you feel the the code sections I have posted are not enough.
Code:
D:\tmp\0a\
D:\tmp\1a\
D:\tmp\2a\
D:\tmp\3a\
D:\tmp\4a\
D:\tmp\5a\
D:\tmp\6a\
D:\tmp\7a\
D:\tmp\8a\
D:\tmp\9a\
D:\tmp\u.txt
D:\tmp\v.txt
D:\tmp\w.txt
D:\tmp\x.txt
D:\tmp\y.txt
D:\tmp\z.txt
D:\tmp\6a\
D:\tmp\7a\
D:\tmp\8a\
D:\tmp\9a\
Code:
struct File {
int i_files;
int a_files;
char **files;
};
struct Directory {
int i_dirs;
int a_dirs;
char **dirs;
};
The following code add directories to a Directory structure and files to a File structure. It does not show the initialization of the structures.
Code:
while ((file = readdir(dir_in)) != NULL) {
if (!((dir_in->dd_dta).attrib == _A_SUBDIR)) {
if (wfiles->i_files >= wfiles->a_files) {
wfiles->files =
realloc(wfiles->files,
wfiles->a_files + MIN_FILE_ALLOC);
if (wfiles->files == NULL) {
fprintf(stderr,
"Something really bad just happend. We lost the file list in realloc.\n");
return NULL;
}
wfiles->a_files += MIN_FILE_ALLOC;
}
wfiles->files[wfiles->i_files] =
calloc(MAX_FILE_NAME, sizeof(char));
if (wfiles->files[wfiles->i_files] == NULL) {
fprintf(stderr, "File name too long.\n");
return NULL;
}
strncpy(wfiles->files[wfiles->i_files], directory,
DIR_IN_LEN);
strncpy(wfiles->files[wfiles->i_files] + DIR_IN_LEN,
"\\", 1);
strncpy(wfiles->files[wfiles->i_files] + DIR_IN_LEN + 1,
file->d_name, file->d_namlen);
wfiles->i_files++;
} else {
if (wdirs->i_dirs >= wdirs->a_dirs) {
wdirs->dirs =
realloc(wdirs->dirs,
wdirs->a_dirs + MIN_FILE_ALLOC);
if (wdirs->dirs == NULL) {
fprintf(stderr,
"Something really bad just happend. We lost the directory list in realloc.\n");
return NULL;
}
wdirs->a_dirs += MIN_FILE_ALLOC;
printf("%i\t%i\n", wdirs->a_dirs,
wdirs->i_dirs);
}
if (strcmp(file->d_name, ".") != 0
&& strcmp(file->d_name, "..") != 0) {
wdirs->dirs[wdirs->i_dirs] =
malloc(MAX_FILE_NAME * sizeof(char));
if (wdirs->dirs[wdirs->i_dirs] == NULL) {
fprintf(stderr,
"Unable to malloc(MAX_FILE_NAME * sizeof( char )).\n");
return NULL;
}
if (memset
(wdirs->dirs[wdirs->i_dirs], '\0',
MAX_FILE_NAME) == NULL) {
fprintf(stderr,
"We had some problem with memset.\n");
return NULL;
}
strncpy(wdirs->dirs[wdirs->i_dirs], directory,
DIR_IN_LEN);
strncpy(wdirs->dirs[wdirs->i_dirs] + DIR_IN_LEN,
"\\", 1);
strncpy(wdirs->dirs[wdirs->i_dirs] +
DIR_IN_LEN + 1, file->d_name,
file->d_namlen);
strncpy(wdirs->dirs[wdirs->i_dirs] +
DIR_IN_LEN + file->d_namlen + 1, "\\",
1);
printf("%s\n", wdirs->dirs[wdirs->i_dirs]);
wdirs->i_dirs++;
}
}
}