can you explain me this function? : i don't understand what this function do, can you explain me in detail please?


    char *my_getline(FILE *stream) {
        char *line = NULL;
        size_t pos = 0;
        int c;
    
        while ((c = getc(stream)) != EOF) {
            char *newp = realloc(line, pos + 2);
            if (newp == NULL) {
                free(line);
                return NULL;
            }
            line = newp;
            if (c == '\n')
                break;
            line[pos++] = (char)c;
        }
        if (line) {
            line[pos] = '\0';
        }
        return line;
    }

if we can add a comment on my code, i think this should be perfect, i try to found a substring in a string and i found this function

this is the main

    int main(void) {
        char *str, *sub;
        size_t len1, len2, i, count = 0;
        printf("Insert string :\n");
        str = my_getline(stdin);
        printf("insert substring :\n");
        sub = my_getline(stdin);
    
        if (str && sub) {
            len1 = strlen(str);
            len2 = strlen(sub);
            for (i = 0; i + len2 <= len1; i++) {
                if (!memcmp(str + i, sub, len2)) {
                    count++;
                    printf("Substring found at index : %d\n", i);
                }
            }
    		printf("in the number of: %d\n", count);
            if (count == 0) {
                printf("Substring not found\n");
            }
        }
        free(str);
        free(sub);
        return 0;
    }

i understand the main but the function no, can anyone explain me?