I am trying to write a shell for Linux in C and/or C++. What functions should I use to interact with the kernel? Someone said to use system(), but I think it calls the shell, which would result in infinite recursion. Someone else said something about fork() and exec().
Obviously a good shell needs some way to access the file system. I assume the kernel provides an interface of some sort for this, does it not? How would I read the output from the kernel? Is it text or integers?
Is there a place where I can find good documentation on the necessary functions? Is there a particular man page or source code file that I sould read?
The kernel provides a set of ‘system calls’ for low-level process execution and file system access. Process execution is generally done using
fork()and one of theexec()family calls. I/O can be done with calls such asopen(),stat(),opendir(), etc.A list of most of the more-portable calls (both C library and system calls) can be found at the Open Group Base Specification (click ‘system interfaces’). There are some linux-specific ones as well – the ones intended for ordinary use will be documented in section 2 of the manual pages (
ls /usr/share/doc/man/man2orman 2 somefunction).