Reputation: 131
I am writing code for a target platform with NO C-runtime
. No stdlib
, no stdio
. I need a string formatting function like snprintf
but that should be able to run without any dependencies
, not even the C library.
At most it can depend on memory alloc functions
provided by me.
I checked out Trio
but it needs stdio.h header
. I can't use this.
Edit
Target platform : PowerPC64 home made OS
(not by me). However the library shouldn't rely on OS specific stuff.
Edit2
I have tried out some 3rd-party open source libs, such as Trio(http://daniel.haxx.se/projects/trio/), snprintf and miniformat(https://bitbucket.org/jj1/miniformat/src) but all of them rely on headers like string.h, stdio.h, or(even worse) stdlib.h. I don't want to write my own implementation if one already exists, as that would be time-wasting and bug-prone.
Upvotes: 7
Views: 4863
Reputation: 51
I am guessing you are in a norming enivronment where you need to explicitly document and verify COTS code.
However, I think in the case of stdarg.h this is worthwhile. You could pull in the source for just this and treat it like handwritten code (review, lint, unit-test, etc.). Any self-written replacement will be a lot of work, probably less stable and absolutely not portable.
That said, the actual snprintf implementation should not be too hard, and you could do this yourself, probably. Especially if you might be able to strip a few features away.
Keep in mind that vararg code has no typechecking and is prone to errors. For library snprintf you may find gcc's warnings helpful.
Upvotes: 0
Reputation: 93534
You will probably at least need stdarg.h
or low level knowledge of the specific compiler/architecture calling convention in order to be able to process the variadic arguments.
I have been using code based on Kustaa Nyholm's implementation It provides printf()
(with user supplied character output stub) and sprintf()
, but adding snprintf()
would be simple enough. I added vprintf()
and vsprintf()
for example in my implementation.
No dynamic memory application is required, but it does have a dependency on stdarg.h, but as I said, you are unlikely to be able to get away without that for any variadic function - though you could potentially implement your own.
Upvotes: 2
Reputation: 20307
Try using the snprintf implementation from uclibc. This is likely to have the fewest dependencies. A bit of digging shows that snprintf is implemented in terms of vsnprintf which is implemented in terms of vfprintf (oddly enough), it uses a fake "stream" to write to string.
This is a pointer to the code: http://git.uclibc.org/uClibc/tree/libc/stdio/_vfprintf.c
Also, a quick google search also turned up this:
Hopefully one is suitable for your purposes. This is likely to not be a complete list.
There is a different list here: http://trac.eggheads.org/browser/trunk/src/compat/README.snprintf?rev=197
Upvotes: 7