Hello, guys!
This code works, but I not certain that it is correct :-)
i do this
tmp = (char *) malloc(strlen(s)+1);
but i don't free memory, possible to do it?
Possible to do this more effectively and beautifully? ;-)
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXFIELD 15
char *get_field(const char *s, char *name)
{
char *tmp, *field;
int i;
tmp = (char *) malloc(strlen(s)+1);
if (!tmp)
return NULL;
strcpy(tmp, s);
if ((field = strstr(tmp, name)) != NULL) {
field += strlen(name);
for(i=0; *tmp != '&' && *tmp != '\0' && i < MAXFIELD; i++)
tmp++;
*tmp = '\0';
}
return field; /* NULL if not found */
}
int main(void)
{
char *str = "http://www.google.com/search?q=FreeBSD";
char *fn;
if (fn = get_field(str, "q="))
printf ("%s\n", fn);
return 0;
}