How to best implement your own C library

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to best implement your own C library

Post by Combuster »

My personal pragmatic solution is to return an error on every fopen() call instead of dealing with ungetting. (Which actually is a 100% standards compliant solution :wink:)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: How to best implement your own C library

Post by Kevin »

In other words: Before you implement a function, read the spec for it. (Though it's amazing how often you get away with some rather bad approximation of the real function for quite a while until it breaks down...)
Developer of tyndur - community OS of Lowlevel (German)
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: How to best implement your own C library

Post by sortie »

Combuster wrote:My personal pragmatic solution is to return an error on every fopen() call. :wink:
That's worse than useless. Don't even bother implementing fopen if you're not going to have it. That'll break software at compile time instead of at runtime.
Kevin wrote:In other words: Before you implement a function, read the spec for it. (Though it's amazing how often you get away with some rather bad approximation of the real function for quite a while until it breaks down...)
Indeed. stdio is hard to fully comprehend if you just read the specs for the individual functions, you want to read the ISO C and POSIX sections that give an overview. Do note how ISO C makes some stdio behaviour undefined that POSIX defines. I think you mean terrifying instead of amazing. :P
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: How to best implement your own C library

Post by Kevin »

sortie wrote:That's worse than useless. Don't even bother implementing fopen if you're not going to have it. That'll break software at compile time instead of at runtime.
Failing at runtime can be useful. Perhaps not for fopen(), that's just too basic, but often programs work quite well with dummy implementations of some functions (that's what I meant by my previous posting). In tyndur, config.h has a #define CONFIG_LIBC_NO_STUBS, so if you're looking for unimplemented functions that a program uses, you disable the stubs, and if you want it to run as good as currently possible, you enable stubs.
I think you mean terrifying instead of amazing. :P
If that terrifies you, OS development is probably not for you. :P
Developer of tyndur - community OS of Lowlevel (German)
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: How to best implement your own C library

Post by sortie »

Kevin wrote:If that terrifies you, OS development is probably not for you. :P
On the contrary. I explicitly desire to be terrified by that which is terrifying and then do something about it. It's too easy to ignore the terror and do nothing. Examples like openssl and bash immediately come to mind as terrifying code no-one wanted to read and everyone assumed someone else could maintain well. In my porting duties, I regularly come across terrifyingly bad code that make me question whether I even want to port this software in the first place. You'll often find me in #osdev yelling at code that offends me.
Post Reply