0

How do I write a function of some commands of shell (UNIX) in C language? E.g what is the source code of the exit function? How can we write its function in C lanugage?

OR

For example; exit command: Implement the exit command that quits the shell. (how to do this/ from which link/book ; a concept can be taken?)

and similarly For pwd command: Implement the pwd command that prints the user’s current working directory.

1
  • exit is necessarily a shell built-in. Commented Dec 7, 2011 at 15:00

2 Answers 2

2

If you want the source code, you can find it at the GNU Coreutils page.

If you're asking how to exit a shell from within a program, the exit command line command is a bash builtin, there's no /bin/exit. You'd have to find the appropriate PID and kill it. The exit builtin probably performs some cleanup and then uses exit(int).

If you want the source code for the shell builtins, look at the bash source.

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

Comments

2

There are many shells which are open-source or free software, so you can download them and study their source code. A simple example is sash, a more complex example is zsh

The exit function (called from the shell's exit builtin) is inside GNU libc, it runs the registered handlers (e.g. by atexit etc..) and then call the _exit system call.

system calls are mostly handled by the linux kernel whose source code is on kernel.org

I strongly recommend reading a good textbook on operating systems, like e.g. Modern Operating Systems by Andrew Tanenbaum, or Understanding Operating Systems by Ann McHoes &, Ida M. Flynn.

I also recommend reading a good Posix/Unix programming book like e.g. Advanced Unix Programming by Rochkind & the late Stevens

6 Comments

which text book would u prefer to read?? i want to implement some commands of SHELL (UNIX) in C language like exit, pwd, ls etc.
I gave some references in my edited answer. And I really suggest you to follow all the links I gave you. At last, I don't understand why you are asking this question? Are you coding a tiny shell as a home work?
yes; the task is to create tiny shell ! and only a few days left ! hardly a week. so couldn't get a proper idea to do all of these. so 1st step is to executing these commands and to write some codes of taking input and so..
No, first step is to understand why most of these commands are builtin, and what is a syscall.
thats what; i want to know ; from where, i can have these concepts. the question posted above is one of da part of assignment. moreover i have to do also this : The prompt should have the following format: <user>@<host> <cwd> > (with space at the end) Here, <user> should be your login name, <host> the host name you are logged in to, and <cwd> the current working directory... so dont have a clear idea from where to start.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.