Even more unwieldy than the GPL? Wow. Didn't know that was possible.
In short, there seem to be two good public domain printfs out there, just use those. Or, better yet, read them and figure out how they work, then do your own.
printf... how to do it?
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
That's a very bad assumption to make, and IMHO.. and illegal one..Jeremiah Griffin wrote:There doesn't seem to be a copyright anywhere, so I'm assuming it's public domain.
http://en.wikipedia.org/wiki/Berne_Conv ... stic_Works
My point exactly. A common misbelief (especially among hobbyist coders) is that if a license doesn't forbid something, it's implicitly allowed. The contrary is the case in most countries. The original Linux license said you are allowed to redistribute the kernel as long as you provide source and don't ask a commercial fee for it. It doesn't say zilch about modifying, using parts of it or anything. Linus might agree to you using early code snippets under GPL terms, but legally speaking that would still require written consent of all authors involved...Brynet-Inc wrote:That's a very bad assumption to make, and IMHO.. and illegal one..Jeremiah Griffin wrote:There doesn't seem to be a copyright anywhere, so I'm assuming it's public domain.
Ah, copyright law. Good thing I haven't had breakfast yet.
Every good solution is obvious once you've found it.
for the begin:
But i have problems: strings(%s) don't work, and it prints the % symbol(normally, it mustn't) chars(%c) work
Code: Select all
void printf (char format[], ... )
{
va_list arguments; // A place to store the list of arguments
int i, dz_fl=0;
char ch;
unsigned char uctmp;
signed char sctmp;
unsigned short ustmp;
signed short sstmp;
unsigned int uitmp;
signed int sitmp;
va_start(arguments, format);
for(i=0;i<strlen(format);i++)
{
ch = format[i];
if(dz_fl)
{
switch(ch)
{
case '%':
putch('%');
break;
case 'c':
putch(va_arg(arguments, char));
break;
case 's':
puts(va_arg(arguments, char));
break;
default:
break;
}
}
else
{
switch(ch)
{
case '%':
dz_fl=1;
break;
default:
if(ch != '%')
putch(ch);
break;
}
}
}
va_end(arguments);
}
Uh-huh...
Note that printf() is effectively a call to fprintf() with stdout as target stream. fprintf() in turn is simply a wrapper for vfprintf()... and you need the same format-parsing logic for the sprintf() family, too.
And perhaps you want to keep in mind that "%#+6.3d" is a valid format specifier, and that for implementing that you have to go far beyond a simple switch...
- None of your temporary variables is actually used.
- dz_fl - which I take is the flag for "now I am within a format specifier" - is never reset to 0.
- For strings, you pull a char from the stack (should be char *).
Note that printf() is effectively a call to fprintf() with stdout as target stream. fprintf() in turn is simply a wrapper for vfprintf()... and you need the same format-parsing logic for the sprintf() family, too.
And perhaps you want to keep in mind that "%#+6.3d" is a valid format specifier, and that for implementing that you have to go far beyond a simple switch...
Every good solution is obvious once you've found it.
In my system, I just ported over Solar's excellent printf from PDCLib. As he says, it doesn't do floats yet, but that's not a big deal to me.
I also ported some functions from OpenBSD's libc.
Once long ago I did use Chris Giese's printf() implementation. That one is safe to use from a licensing standpoint; he explicity denies copyright to the code and places it in the public domain. Okay, well, some countries don't have the concept of public domain, but that code should still be safe to use. Just don't strip the header information from those files. Beware there are limitations to this implementation as well (it also doesn't do floats, and a lot of other things that PDCLib does do).
So, IMO, when it comes to printf() and friends, you have three choices.
1) Solar's PDCLib
2) Chris Geise's printf() implementation (http://my.execpc.com/~geezer/osd/libc/printf.c)
3) Rip it from OpenBSD. Their code is ISC and BSD licensed, so it's safe to use even for those of you that are creating closed-source projects.
I also ported some functions from OpenBSD's libc.
Once long ago I did use Chris Giese's printf() implementation. That one is safe to use from a licensing standpoint; he explicity denies copyright to the code and places it in the public domain. Okay, well, some countries don't have the concept of public domain, but that code should still be safe to use. Just don't strip the header information from those files. Beware there are limitations to this implementation as well (it also doesn't do floats, and a lot of other things that PDCLib does do).
So, IMO, when it comes to printf() and friends, you have three choices.
1) Solar's PDCLib
2) Chris Geise's printf() implementation (http://my.execpc.com/~geezer/osd/libc/printf.c)
3) Rip it from OpenBSD. Their code is ISC and BSD licensed, so it's safe to use even for those of you that are creating closed-source projects.