Is it possible/how to tail output sprintf?

TL;DR: is it possible using any of the variants of sprintf and not using additional memory to the last n characters of output?

Details: there is a system with very limited memory. A portion of this memory allocated for the ring buffer, which is output symbols overridden functions from the printf family. I.e. printf("Hello world\n"); puts Hello world\n to the current position of the record in this ring buffer and advances the current position forward. When the pointer approaches the end of the buffer and the length of the output string is longer than available space, the string must be "broken", i.e. the beginning of the line must be at the end of the buffer, and the end of the row -- in the beginning (assuming that at the beginning already released), ie should be able to, for example: char buf[16] = "orld\n\0\0\0\0Hello w"; to cut off the tail of the output string I can use snprintf but how can I cut off the head of the output string? And, Yes, of course, the answer in the style of "back off n bytes from the beginning of the string and print" is not suitable, because the string is not Hello world, "%s: a[%d] = %d".
I suspect that you can't do that, but you never know, maybe there is.
June 3rd 19 at 19:18
2 answers
June 3rd 19 at 19:20
Solution
interesting question, a rarity here.

come to mind two variants of the organization of the ring buffer is transparent to the rest of stdlib.

first: if this system with limited memory is the MMU, it is possible, slappity on the adjacent page is the same piece of memory, so that after the end of the buffer again was its beginning. and after each sprintf to check for overflow and move the pointer to the current position in the first page (take the size of the buffer). or just do and if the size is a multiple of power of two. it would be virtually free hardware and performance, would work generally with all the features stdlib, but troublesome and not always possible.

second option: you can make your stream (man fopencookie). for your purpose, it is sufficient to write only a write handler, which actually will loop buffer: copy how many got to the end, and the rest in the beginning. and print all in this stream fsprinf-AMI and other functions for working with threads. may even be able to redirect stdout to this manufactured flow, then the usual printf'you earn.
if this system with limited memory is the MMU

no, MMU is not, and even if it was too hardware-dependent work.

you can make your stream (man fopencookie)

here thank you for it, that's it! And, it seems, that it even implemented in our libc. - George.Douglas commented on June 3rd 19 at 19:23
may even be able to redirect stdout in this homemade stream

Works:
stdout = fopencookie((void *)p, "w", ring_buffer_ops);
- George.Douglas commented on June 3rd 19 at 19:26
June 3rd 19 at 19:22
If you have a termination character, such as 0x00, it is possible to scan the buffer and upon detection of a transition from 0x00 to something else to begin to read in a circle until you hit to 0x00 again.

Usually, the ring buffer includes a pointer to the current position in the buffer. Since the ring buffer has a fixed size, it can be read backwards starting from the current position until it encounters the end character.

Find more questions by tags C