Can you help to make this working?
files.c
Code:
#include "include/types.h"
#include "include/gaussian.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <dirent.h>
#include <string.h> // strcmp, strtok
#include <sys/types.h> // mkdir
#include <sys/stat.h> // mkdir
int makeConfig(char ** path)
{
char configFile [12] = "kernels.cfg";
DIR *dir; struct dirent *ent;
int count = -1;
if ( ( dir = opendir (*path) ) != NULL)
{
char ch;
FILE* f;
FILE* f2;
f = fopen(configFile, "r");
if( f != NULL ) // if config already exists, backup
{
f2 = fopen("kernels.bak", "w");
while( ( ch = fgetc(f) ) != EOF )
fputc(ch, f2);
fclose(f);
fclose(f2);
}
// write new file
f = fopen(configFile, "w");
if( f == NULL )
perror("Cannot write to file kernels.cfg");
int ckey = 0;
size_t len;
while ((ent = readdir (dir)) != NULL)
{
if ( ckey<2 && ( strcmp(".",ent->d_name) == 0 || strcmp("..",ent->d_name) == 0 ) )
{ ckey++; continue; }
// backup ent->d_name because chTmp2 is changed by strtok
len = strlen(ent->d_name);
// char * dot = strchr( ent->d_name, '.' );
if ( len >4 )
{
char name[64];
char * pName = ent->d_name;
char * ext;
ext = pName + len - 3;
memcpy( name, ent->d_name, len-3 );
if ( 0 == strcmp( ".", name) )
{
printf("Incorrect kernel source name. File name %s, must not contain dots, but must have .cl extension.\n",ent->d_name);
}
else
{
name[len-3]='\0';
fprintf(f, "%s\n", name);
printf (".'%s'\n", ent->d_name);
}
}
else
{
printf("Incorrect kernel source name. File name %s too short.\n",ent->d_name);
}
}
closedir (dir); fclose(f);
}
else
{
/* could not open directory */
char error [255];
sprintf(error, "Directory not found: %s", *path);
perror (error);
return -1;
}
return count;
}
int readConfig(char ** path, FILES ** files )
{
char configFile [12] = "kernels.cfg";
DIR *dir;
if ( ( dir = opendir (*path) ) == NULL)
{
perror("directory of binary kernels not found");
return -2;
}
else
closedir(dir);
FILE* f;
f = fopen(configFile, "r");
if( f == NULL ) // if config already exists, backup
{
perror("Config file kernels.cfg not found. Run the program with -install argument.");
return -3;
}
int ch; int c = 0; int prevc = 0; int count = 0;
char name[64];
while ((ch=fgetc(f)) != EOF )
{
name[c]=ch;
c++;
if (ch == '\n' && c-prevc>1 )
{
files[count]->name=malloc(64);
files[count]->filename=malloc(64);
// strncpy(files[count].name, name, 64);
strcpy(files[count]->name,name);
files[count]->name[c-prevc-1]='\0'; // remove linefeed \n
strcpy(files[count]->filename,name);
// files[count].filename[c-prevc+1]='\0';
files[count]->name[c-prevc+4]='\0';
strcat(files[count]->filename,".bin");
count++;
prevc=c;
}
}
return count;
}
types.c
Code:
#include <stdbool.h>
typedef struct {
char * name;
char * filename;
} FILES;
main.c
Code:
#include "include/types.h"
#include "include/gaussian.h"
#include "include/args.h"
#include "include/files.h"
#include "refu/Time/rfc_timer.h"
#include <stdio.h>
int main ( int argc, char *argv[] )
{
char * path = "kernels";; // used as needed
FILES files[256];
int count;
count = readConfig(&path, &files));
After calling readConfig(&path, &files));
I need to access the members .name and .filename to allocate memory and copy strings in there.
Code:
files[count]->name=malloc(64);
files[count]->filename=malloc(64);
SIGSEGV error
How to make the reference working?