All homework submissions are to be made via Git. You must submit a detailed list of references as part your homework submission indicating clearly what sources you referenced for each homework problem. You do not need to cite the course textbooks and instructional staff. All other sources must be cited.
The Git repository you will use can be cloned using git clone gitosis@os1.cs.columbia.edu:UNI/hmwk1.git (Replace UNI with your own UNI). A public/private SSH key-pair will be emailed to you which you will use to access the Git server. Be aware that commits pushed after the deadline will not be considered. Refer to the homework policy section on the class web site for further details.
fd = open("textfile", O_RDONLY);
fptr = lseek(fd, (off_t)-1, SEEK_END);
while (fptr != -1) {
read(fd, buf, 1);
write(1, buf, 1);
fptr = lseek(fd, (off_t)-1, SEEK_CUR);
}
Explain why it is not a good technique for doing so. Present a
program that does it better.
You should make sure your programs compile and run using the VMware virtual appliance used for this class. Information on downloading and using the appliance is listed in the intro section of the class in Courseworks.
Simple Shell (60 Pts.) The shell itself is just another user program. The files /bin/sh, /bin/bash, and /bin/tcsh are all executable files for shells. The only thing special about your login shell is that it is listed in your login record so that /bin/login (the program that prompts you for your password) knows what program to start when you log in. If you run ``ypmatch id passwd'', replacing id with your user ID, you'll see your login record, with your login shell program listed as the last field.
Write a simple shell in C. Your shell should read a
line from standard input, parse the line to get the command
and its arguments, fork, and exec
the command. Your shell should wait for commands to finish,
and should exit when it receives the command
exit.
Using the system function is not allowed,
as it just invokes the system's /bin/sh shell
to do all the work.
You may assume command line arguments are separated by whitespace. Don't do anything special for backslashes, quotes, ampersands or other characters that are ``special'' in other shells. Note that this means commands in your shell will not be able to operate on filenames with spaces in them!
You may set a reasonable maximum on the number of command line arguments, but your shell should handle input lines of any length.
The executable file for your shell should be named w4118_sh. When testing, make sure you execute your w4118_sh and not /bin/sh.
A few shell commands are not separate programs at all,
but are handled directly by the shell program. You have
already implemented exit, which is an example of
this. Now implement cd, the change directory
command. You will need to invoke the chdir
system call to do this. Note that if the call to
chdir fails, you probably don't want to exit
the shell but instead should handle the error appropriately.
Some convenient builtins present in many shells are pushd, popd, and dirs. Implement these in your shell.
pushd works like cd, but pushes the directory you switched from onto a stack.
popd pops the top directory off the stack, and cd's to it. In other words, a pushd followed by a popd should bring you back to the directory you were in before the pushd.
dirs prints the contents of the stack.
The path variable holds a list of possible paths in which to search for executables. The list of paths is empty by default. You should implement a builtin command to control this variable: path [+|- /some/dir]
path (without arguments) displays all the entries in the list separated by colons, e.g. "/bin:/usr/bin".
path + /some/dir appends the given pathname to the path list.
path - /some/dir removes the given pathname from the path list.
errno variable (see the function's man page to find out if it does), you should use the result of strerror(errno) as part of your error message.
As far as system calls are concerned, you will want to use one of the
mechanisms described in Reporting Errors or Error Reporting.