The question is
Implement a function
char* readLine();which returns single lines from
a buffer. To read the buffer, you can makes use of a function int
read(char* buf, int len)which fills buf with upto len chars and
returns the actual number of chars filled in. Function readLine can be
called as many times as desired. If there is no valid data or newline
terminated string available, it must block. In order to block, it can
use read function which in turn will block when it doesn’t have
anything to fill the buf.
I don’t know how to proceed and what kind of approach is expected.
I could not understand what the interviewer expects here. I just need direction to move ahead and not the exact function.
Allocate an appropriately sized buffers.
If you don’t have characters in your read buffer, read in a new chunk.
If the next character from the read buffer is a newline, return the result buffer.
If the result buffer is full, bug out and whine about lines being too long.
Otherwise, add the next character from the read buffer into the result buffer.
NOTE: The answer to the question as asked is a security issue waiting to happen, and also a potential memory leak.