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

void my_strcat(char **str, char **buf)
{
    char *ptr;
    int n = strlen(*str) + strlen(*buf);
    int len = strlen(*str);
    if (len) {
        ptr = *str + len - 1;
        if (*ptr == '\n') {
            *ptr = '\0';
        }
    }
    ptr = (char *) realloc(*str, n + 1);    /*strとbuf分の領域確保 */
    if (ptr == NULL) {
        free(*str);
        free(*buf);
        exit(1);
    }
    *str = ptr;
    strcat(*str, *buf);         /*bufをstrに結合する */
    return;
}

int main(void)
{
    int n = 2;
    char *str, *buf;
    str = (char *) malloc(30 * sizeof(char));
    buf = (char *) malloc(30 * sizeof(char));
    for (n = 1;; ++n) {
        printf("input string_%d :", n);
        fgets(buf, 30, stdin);
        if (strcmp(buf, "quit\n") == 0) {
            break;
        }                       /*quitと入力されたらループを抜ける */
        my_strcat(&str, &buf);
    }
    printf("str = %s", str);
    free(str);
    free(buf);
    return 0;
}
