[Solved] Removing Tags from a file parsed in C


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *getfield(char **sp){
    char *left; //point to <
    char *right;//point to >

    if((left = strchr(*sp, '<')) == NULL)
        return NULL;
    if((right = strchr(left, '>')) == NULL)
        return NULL;

    size_t len = right - left;//if len == 1, tag is nothing(<>)
    char *tag = malloc(len);
    memcpy(tag, left + 1, len -1);
    tag[len-1] = '\0';

    char *etag = malloc(len + 3);
    sprintf(etag, "</%s>", tag);
    left = right + 1;
    if((right = strstr(left, etag)) == NULL)//right point to end tag
    {
        free(tag);
        free(etag);
        return NULL;
    }
    len = right - left;
    char *text = malloc(len + 1);
    memcpy(text, left, len);
    text[len] = '\0';

    *sp = right + strlen(etag);
    free(tag);
    free(etag);
    return text;
}

int main(void){
    char line[500000];

    while (fgets(line, sizeof line, stdin)){
        char *arg = line;
        char *text;

        while ((text = getfield(&arg)) != NULL){
            printf("%s\n", text);
            free(text);
        }
    }
    return 0;
}

2

solved Removing Tags from a file parsed in C