> I want to have getch() and ungetch() without a static buffer.

Why, exactly? I ask because 1) it may not be necessary and 2) what you want to accomplish alters how you would go about it.

> I want to realize it with malloc and realloc, but I don`t know how.

Change char buf[BUFSIZE]; to char *buf; for starters. Though now you need to call an initialization function which calls malloc, and that puts an onus on the calling code of your library to remember the initialization function. Or if you're lucky enough to be writing your own implementation of C, that initialization can be done invisibly before main is called. Otherwise you're boned, and this becomes a question of whether you want to complicate the library interface and if the benefit is worth the complication.