Where to find stdio.h functions implementations? - c

I study C and I noticed that I can't find the implementation file for some header files like, for example, stdio.h which is a library which contains a lot of input/output functions, like printf. Where can I find its implementation?

Download one of these:
glibc
uclibc
dietlibc
BSD libc
Or, even better, download several of these and compare their implementations. Of course, these are likely doing a lot of things different compared to your particular standard library implementation, but would still be quite interesting for e.g. non-platform-specific functionality such as sprintf.

You need to find the source code for a C standard library like glibc: http://www.gnu.org/s/libc/
You can download the source here: http://ftp.gnu.org/gnu/glibc/
It contains source for all the library functions.

For example here. Google is your friend - just search for stdio.c. But note that you should handle these as "one implementation of many possible" - you don't know how your compiler is doing it by reading those, you just get an idea of how it can be done.

On Ubuntu or other OS that uses aptitude for package management, you can use:
apt-get source libc6
for example.
Also, running gcc in verbose mode will tell you the details of the paths it is using. This should help you find which include and library paths it's using.
gcc -v
or
gcc -Wl,--verbose

If you install the Windows SDK, there is an option to include the standard library source code, so you can also see how it is implemented on Windows.

Related

Alternative to dir command to query a directory in C

I am trying to create a program where the user can add different path regex-s so that a specific set of operations on the files that match the regex.
I tried using opendir() of the dirent.h header file but soon realized that it does not use the concept of regex.
The dir command I am trying to emulate is
dir [regex] /b
I need the output in a (char) buffer**. Piping the output could be a solution but I am looking for a more efficient way to do it.
Is there any predefined function in the standard (C90) library or will we have to create our own implementation?
C does not know about directories. They are operating system specific, usually provided by your OS kernel (look however inside GNU Hurd as an exception, and into unikernels). Read the C11 standard n1570 and forget, in 2020, about the obsolete C89 standard and TurboC. Consider trying some Linux distribution (such as Ubuntu or Debian or others). Most of them provide GCC or Clang (or the non-optimizing TinyCC compiler) and are very developer-friendly. My recommendation: use GCC as gcc -Wall -Wextra -g. Choose a good enough built automation tool (maybe GNU make) with an appropriate source-code editor (such as GNU emacs or vim or geany or others). Learn how to debug small programs and use the GDB debugger and the git version control tool.
POSIX does know about directories (it is an API specification written in English, also defining regex(3)). See here, and read the Linux man pages. And also the WinAPI.
On Linux, see mkdir(2), chdir(2), readdir(3), getcwd(3), unlink(2), stat(2), open(2), nftw(3), path_resolution(7) etc etc; you could want to study the source code of a Linux kernel and of some common C library for it, such as GNU glibc or musl-libc. Budget for that several months full time of your efforts. They are open source, so with some conditions you are allowed to study, improve and reuse their source code. See also http://linuxfromscratch.org/
Notice also popen. You probably don't want to use it and would prefer using more primitive system calls (see syscalls(2) for their list on Linux). You could use a library like Glib (from GTK).
Remember that C programs (of the freestanding kind) could run on the bare metal (e.g. Arduino). In those cases, speaking of directories does not make any sense. See also osdev.org for more, and observe that the Linux kernel is written in C (with a tiny amount of assembler code).
GrassHopper was an OS written mostly in C without any files or directories. See also old discussions archived on tunes.org and tccboot.
Use the function findfirst to start querying a directory and then findnext to iterate. The functions find all files in one directory matching a given pattern, so you possibly need to append \*.* to your directory name to list all files in that directory.
Refer to the Turbo C documentation for details.

Where can I find the source code for all the C standard libraries?

I'm looking for the whole source code of all the C standard libraries. That said, I'm looking for the source code of stdio.h, stdlib.h, string.h, math.h, etc... I would like to see how they were created. I think that it depends from platform to platform, but Linux or Windows one will be welcomed.
If you want the actual implementations, as everyone else has assumed you don't, many Linux distributions currently use glibc to implement the C standard library. Common alternatives include musl libc, diet libc, uClibc, and Bionic
PJ Plauger wrote a book about the standard C library. Includes references from the (now dated) standard, and source code.
Microsoft Visual Studio generally has the system headers under <InstallDir>\VC\include, and the source, if installed, is under <InstallDir>\VC\crt\src.
Whether its installed with an IDE or you have installed explicitly, you have to look in the directory "Include" in respective location.
Ex: I use MinGW. So, I would go to
c:/MinGW/include to find those header files. Similarly, for an IDE (say Dev-cpp), you need to go to c:/dev-cpp/include.

where to find c source code

I am a beginner in C, and i would like to read how various libraries are implemented. I looked under /usr/src/include and all i found was .h files. For example , i was looking at malloc.h and all that it does is , declare extern functions.
So i am trying to find the source for those functions. I downloaded gcc source rpm, but looking at the gcc source, it looks more like compiler code,rather than code for libraries such as stdlib.
Can you please help me by pointing me to the right direction.
Thank you.
You need to get the source code of the associated C library, probably glibc or eglibc in your case.
In the /usr/include/ folder, only the headers of the libc are present, along with some linux kernel headers in the linux/ subfolder.
You're looking for glibc, rather than gcc.
.h files never contains code (well, at least they should. Macros are exceptions)
the basic C functions are either in glibc or linux kernel
gcc, linux and glibc code are huge beasts, if you are a beginning beginner, you should go for simpler things
you probably should take a simple library, one that offer a few tools, on a narrow subject.
then go for linux and glibc (or uclibc)
I personnaly learnt many interesting stuff by reading microcontroller system libraries such as http://www.nongnu.org/avr-libc/, but this depends on what you need to do, and it requires a microcontroller to run it.
An idea: depending of what you're looking for, you could go for busybox: it is a reimplementation of many usefull system commands, you can learn a lot from it and running it do not require a dedicated computer.
Update: i asked a question related to glibc functions that got a few very interesting answers from my not-guru p.o.v:
where to find select() source code in glibc source?

Where to find my system's implementation of standard C library functions?

for example, the strrev() function. i know that it's declared in string.h, and i wanna to figure out how it is implemented. so where could i the source code?
OS: Windows XP SP3
IDE: Pelles C 6.50 RC3
If you just want to see the source to an implementation of the standard C library (albeit one that is in wide use), the source to glibc is available. You can clone the git repository:
git clone git://sourceware.org/git/glibc.git
You can also examine the source in a browser. Here's the string library, for instance.
glibc does not include strrev(). However, if you Google for
strrev c
you'll get hits to various implementations. For example: http://www.cas.mcmaster.ca/~kahl/SE2S03/2006/C/strrev.c and ftp://ftp.ecs.vuw.ac.nz/pub/libs/libstrings/strrev.c.
It depends where your particular compiler puts it. Many compilers don't provide the library source code.

Where can I find the string.c file itself (to read it)?

I'm interested in reviewing some of the functions included in the string library for c, but I can't find the source code (literally, i.e. functions and all) online and I have no idea where to find it.
Thanks.
EDIT:
the link provided by pmg shows those functions divided into c files, so it's about what I needed. Thanks.
Take a look at redhat glibc. It appears to be somewhat current.
You'll find it in the source code of the gcc compiler.
http://www.gnu.org/software/gcc/
Usually included with the compiler that you install so this may vary. Also depends on the operating system your running. If you're using windows, I recommend you run a Windows search for strings.c and if you're running linux then you can use the find command.
Disregard the file I linked to prior to this edit. I should have verified the code before sending it. It didn't apply to your question. Sorry
Maybe you're looking for GNU C string.h?
You can check the source in any standard libc implementation http://www.gnu.org/software/libc/

Resources