Ok ive looked at it for ages, improved the code and tried sticking printf's everywhere. I still cant see why it segfaults at the end!
Also now ive tried to add code to find the "'s around links and remove them but that doesnt work either!
I hope c gets easier, im really banging my head againts a wall at the moment.
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void readfile(FILE *fp, char *p);
bool findtag(char **p, char *start, char finish, char *result);
void cleanup(char *tag);
main(int argc, char *argv[])
{
FILE *fp;
char c[10000], tag[100];
char *p, *o;
if ((argc != 1) && ((fp = fopen(*++argv, "r")) != NULL))
{
readfile(fp,c);
fclose(fp);
p = c;
while(findtag(&p,"<a",'>',tag))
{
o = tag;
findtag(&o,"href=",' ',tag);
cleanup(tag);
printf("%s\n",tag);
}
}
else
printf("Arf\n");
}
void readfile(FILE *fp, char *p)
{
int i;
i = 0;
while ((p[i++] = getc(fp)) != EOF)
;
p[i - 1] = '\0';
}
bool findtag(char **p, char *start, char finish, char *result)
{
char *a;
int i;
i = 0;
if (a = strstr(*p, start) + strlen(start))
{
while ((result[i++] = *a++) != finish)
;
result[i-1] = '\0';
*p = a;
return 1;
}
else
return 0;
}
void cleanup(char *tag)
{
char *a, temp[100];
int i;
if (tag[0] == '"')
{
i=1;
*a=tag[1];
while((*a++ = tag[i++]) != '"')
temp[i-1] = *a;
tag = temp;
}
}