B
BMarsh
Hi all,
I have the following piece of code which i've been wrestling with for
sometime, which I attribute, still, to my lack of understanding of
pointers. (this is probably rining alarm bells atm for you seasoned
pro's...).
I am trying to parse a file with the following format:
hostname {
file1;
file2;
file3;
};
Now, i could get this from the BIND source code (OT: named.conf
parsing..) but I really need to crack this myself...
I have the following code:
load_config(char *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;
buf=malloc(1024);
host=malloc(32);
if((fp=fopen(fn,"r+")) < 0)
{
fatal("fopen"); // my own wrapper..
}
memset(buf,'\0',1024);
while(fgets(buf,sizeof(buf),fp)) {
buf[strlen(buf)-1]='\0';
if(*buf == '{') {
inblock=1;
printf("%s",buf);
buf++;
}
}
}
This occasionally segfaults depending on wether I use an array for buf
or a pointer..
I don't even get up to the left brace.. I basically need to gobble up
the next three files, which I think I can do with fgets, blast the
newline and strtok with the semi-colon no problem, but I can't seem to
move through the file!!
Thanks in advance for any help you can provide
Cheers
Bryan.
I have the following piece of code which i've been wrestling with for
sometime, which I attribute, still, to my lack of understanding of
pointers. (this is probably rining alarm bells atm for you seasoned
pro's...).
I am trying to parse a file with the following format:
hostname {
file1;
file2;
file3;
};
Now, i could get this from the BIND source code (OT: named.conf
parsing..) but I really need to crack this myself...
I have the following code:
load_config(char *fn)
{
FILE *fp;
char *host;
char *buf;
int inblock=0;
buf=malloc(1024);
host=malloc(32);
if((fp=fopen(fn,"r+")) < 0)
{
fatal("fopen"); // my own wrapper..
}
memset(buf,'\0',1024);
while(fgets(buf,sizeof(buf),fp)) {
buf[strlen(buf)-1]='\0';
if(*buf == '{') {
inblock=1;
printf("%s",buf);
buf++;
}
}
}
This occasionally segfaults depending on wether I use an array for buf
or a pointer..
I don't even get up to the left brace.. I basically need to gobble up
the next three files, which I think I can do with fgets, blast the
newline and strtok with the semi-colon no problem, but I can't seem to
move through the file!!
Thanks in advance for any help you can provide
Cheers
Bryan.