It would work twice, but no more. Also guard explicitely against buffer overflow, since once that code actually works, sizeof(buf)-buflen will be interpreted as large positive, and allow overwriting the stack.