Need help with program that crashes - probably an allocation problem
Hello everyone , i have tried everything (or so i think) to solve this problem and keep the program logic intact (allocate memory dynamically while everything is unknown [string length and array size alike]).The problem seems to be happening at function GetDirectoryFiles(...) , where i believe i am making some mistake with the allocation of memory , but i just cant find it.The program should take an unknown size string from the user (movies directory / path) , then it should open and read the directory if it exists and then store all the names of the files(filenames) that exist in this directory.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define INITIAL_STRING_SIZE 64
#define INITIAL_STRING_ARRAY_SIZE 16
char* ReadDirectory();
char** GetDirectoryFiles(char *directory,int *numberoffiles);
void DeleteDirectory(char **dir);
void DeleteFilenames(char ***filenames,int size);
char* ReallocOrFreeString(char *sp,size_t sz);
void ErrorMessage(const char *msg);
int main()
{
int number,i;
char *maindir = NULL,**mainfilenames = NULL;
maindir = ReadDirectory();
mainfilenames = GetDirectoryFiles(maindir,&number);
for(i=0;i<number;i++)
{
printf("%s\n",mainfilenames[i]);
}
DeleteDirectory(&maindir);
DeleteFilenames(&mainfilenames,number);
system("PAUSE");
return 0;
}
char* ReadDirectory()
{
int c,size = 0,allocated = INITIAL_STRING_SIZE;
char *maindir = NULL;
maindir = malloc(allocated * sizeof(char));
if(maindir == NULL)
{
ErrorMessage("ReadDirectory 1");
}
printf("Give the directory where the movies are: ");
while((c = getchar()) != EOF && c != '\n')
{
if(size == allocated - 1)
{
maindir = ReallocOrFreeString(maindir,(allocated *= 2) * sizeof(char));
if(maindir == NULL)
{
ErrorMessage("ReadDirectory 2");
}
}
maindir[size] = c;
size++;
}
maindir[size] = '\0';
return ReallocOrFreeString(maindir,size+1);
}
char** GetDirectoryFiles(char *directory,int *numberoffiles)
{
char **filenames = NULL;
struct dirent *dent;
DIR *dir;
int i;
if((dir = opendir(directory)) == NULL)
{
ErrorMessage("GetDirectoryFiles 1");
}
*numberoffiles = 0;
while((dent = readdir(dir)) != NULL)
{
if(!strcmp(dent->d_name,".") || !strcmp(dent->d_name,".."))
{
continue;
}
(*numberoffiles)++;
}
filenames = malloc(*numberoffiles * sizeof(char*));
if(filenames == NULL)
{
ErrorMessage("GetDirectoryFiles 2");
}
closedir(dir);
if((dir = opendir(directory)) == NULL)
{
ErrorMessage("GetDirectoryFiles 1");
}
while((dent = readdir(dir)) != NULL)
{
if(!strcmp(dent->d_name,".") || !strcmp(dent->d_name,".."))
{
continue;
}
filenames[*numberoffiles] = malloc((strlen(dent->d_name) + 1) * sizeof(char));
if(filenames[*numberoffiles] == NULL)
{
ErrorMessage("GetDirectoryFiles 3");
}
strcpy(filenames[*numberoffiles],dent->d_name);
}
closedir(dir);
for(i=0;i<*numberoffiles;i++)
{
printf("%s\n",filenames[i]);
}
return filenames;
}
void DeleteDirectory(char **dir)
{
free(*dir);
*dir = NULL;
}
void DeleteFilenames(char ***filenames,int size)
{
int i;
for(i=0;i<size;i++)
{
free((*filenames)[i]);
}
free(*filenames);
*filenames = NULL;
}
char* ReallocOrFreeString(char *sp,size_t sz)
{
char *p;
p = realloc(sp,sz);
if(p == NULL)
{
free(sp);
}
return p;
}
void ErrorMessage(const char *msg) {
fprintf(stderr, "Error: %s\n", msg);
exit(EXIT_FAILURE);
}