0

I want to write a shell in the Linux operating system with the C language.

What is the name of the libraries and functions to use in this project?

thank you.

4
  • 3
    What do you want the library to do? Commented Nov 4, 2011 at 7:48
  • Please clarify, you want to write a whole shell program?, you want to invoke a shell command from a program?, you want to pipe data to a shell? Commented Nov 4, 2011 at 7:49
  • You'll want a line editor (readline / libedit), some linked and doubly linked list helpers, a parser (I highly recommend lemon) and a whole lot of patience. This question, unfortunately is just too broad as-is. If you run into a specific problem, feel free to ask. Writing a shell is a fantastic exercise to sharpen your skills. I wrote the shell for HelenOS, and it was the most fun I've had while programming. Commented Nov 4, 2011 at 10:42
  • i want to invoke a shell command from a program. Commented Nov 5, 2011 at 14:35

4 Answers 4

2

You will probably want, at the very least:

  • fork, execv - for invoking a child process.
  • waitpid - for reaping dead child processes spawned with fork+exec.
  • sigaction - for installing signal handlers to:
    • Catch interruption (CTRL+C).
    • Reap dead child processes.
  • open - for opening files when redirecting to/from files.
  • dup2 - for replacing STDIN/STDOUT with an opened file when implementing redirection.
  • pipe - for creating pipes.

The Single UNIX Specification Man Pages are a good resource for any additional functionality needed.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't understand your question. You are starting a new thing, you can use whatever name you want. And the notion of Project is usually specific to some IDEs.

To code a shell, you first must know well the C programming language, and understand well several important Linux system calls (like fork, execve, pipe, chdir, dup etc.). So read a good textbook on these first. The system calls are available thru the standard C library, you don't need to link an extra one.

And probably, studying the source code of small shells (like sash) would help you a lot.

Comments

1

Actually, that's an interesting question and it could be a good exercise for something to know the IO mechanism of Unix. As you might know, Shell is responsible for interpreting Input , invoking the system calls and displaying the output. The program as such runs on top of the kernel. So, the exercise might give a lot of info on process execution family such as exec*.

Here is one tutorial covering it for you in C and this one in Python would help you design a quick prototype for understanding.

2 Comments

Image
The shell don't pass its input to the kernel. It interprets its input and invoke system calls accordingly.
I was oversimplifying. Corrected it.
0

Unfortunately, there isn't some magical emulateShell() API call you can make for this, you are going to need to look into (in great depth) things like exec, fork, popen, signal/sigaction and a host of other process control things.

In addition, you'll probably need to get involved with terminal handling and so forth, things like fcntl and ioctl.

It's not really a subject that can be covered comprehensively is an answer box this size. I would suggest trying to break it down in smaller jobs so as to make your (and our) job easier.

Perhaps start with a simple program that accepts commands from the user and breaks them down into tokens for execution. That would be a good first step.

1 Comment

Image
I'm not sure he needs popen, he needs to understand the pipe system call (among others). popen is like system, it uses a shell, dont provide one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.