Click to See Complete Forum and Search --> : Linux shell commands (eg, cd, ls, .. ) Making my own for an assignment


dualcyclone
02-24-2004, 08:32 PM
Hi,

I have an assignment whereby I have to make myself a basic shell and also make my own versions of some of the core commands, these are:

pwd
ls
cd
kill

I have managed to figure out pwd (only because I asked someone for their code, then i worked out how it worked and made my own version from there)

I just wanted to know if anyone knew where i could find any versions of these written in C? I need to understand how the code works, once ive done that, its easy to build my own and cut up to my own standard.

I'm not asking you guys to do my work for me - obviously! I wouldnt learn anything that way.

I just need to find a decent resource that shows me exactly how these functions work, and their in-built arguments, etc...

Please step me in the right direction, Linux is very new to me, and being thrown in at the deep end of the ocean before you can swim isnt very nice at all!

Cheers guys

serz
02-24-2004, 09:55 PM
I can tell you in what packages these commands are.

cd is a shell built in.

ls fileutils
pwd sh-utils
kill util-linux

Now, you should be able to find the sources of these and take a look at them.

dualcyclone
02-25-2004, 01:25 PM
Thanks,

Where can i find these packages? And i assume when you say 'source' - that would be the source-code for these commands?

Thanks

serz
02-25-2004, 02:08 PM
Yes, the source codes.

fileutils and sh-utils (http://www.gnu.org/software/coreutils/)
util-linux (http://freshmeat.net/projects/util-linux/)

dualcyclone
02-29-2004, 09:19 AM
Thanks for those files, they will be very useful!

I didnt realise 'ls' would be so huge tho, you dont know a place where there is an 'ls' that is a bit simpler?

And do you know where i can find the source for 'cd' on its own?

Thanks

bwkaz
02-29-2004, 04:47 PM
You can't get 'cd' on its own. A program that runs, changes directories, and then exits (with its parent inheriting the changed directory), is impossible to write. That's why when you make a shell script, and change directories inside the script, the shell that runs the script doesn't end up in the directory that the script changed to when the script is finished. Children can't modify their parent's environment like that. (Unless you run the script using "source" or "." so that the shell doesn't create a child process for it.)

cd MUST be part of the shell, in other words. There's no way to separate it.

However, you can look at the system calls used by cd -- basically, the syscall's name is chdir(). Look at man 2 chdir for the reference on its arguments and its effects. At that point, writing a simple version of cd (as a part of your shell) shouldn't be too hard.

For ls, look at the opendir() and readdir() manpages (in section 3 of the manpages, so you'd do a man 3 opendir and man 3 readdir to read their reference material). Build some sort of list of the filenames that readdir() returns, then print that list. Most of the stuff in the version of "ls" in coreutils is just extra baggage used for formatting the output in various ways.

For pwd, look at the getcwd(3) manpage -- that is, section 3.

For kill, look at the kill(2) manpage.