NEXT UP previous
Next: My Home Page

Answers

  1. The code which follows is very simple, partly because it does none of the checks that would normally be required to verify the correct number and types of command line parameters and also to verify the correct operation of the system calls made. However, it does make the code very easy to read:

    	#include <stdio.h> 
    	#include <dirent.h>
    
    	main(int argc, char **argv)
    	{
    		DIR *dp;
    		struct dirent *link;
    
    		dp = opendir(argv[1]);
    
    		while ((link = readdir(dp))!=O)
    			printf("%s\n", link->d_name);
    
    		closedir(dp);
    	}
    


  2. The way to tackle this problem is to use either ioctl() or tcgetattr() to pick up the contents of the structure associated with the terminal. The information you need about the meanings and values for each of the flags and bytes in the structure is contained in the header file /usr/include/linux/termios.h, with some extra information in the manual page for the stty command. From this information it is quite easy to work out what flags are set and reset and what their effects will be. This can then be displayed in any appropriate form.

  3. The following shell script performs the required task and will work nicely as a pseudo code program for you to follow for your solution to the problem in C:

    	echo -n "enter name: "
    	read name
    	stty -echo
    	echo -n "enter password: "
    	read password 
    	stty echo
    	echo
    	echo name: $name
        echo password: $password
    


  4. A pseudo code solution to this problem would look something like:
    	main(argc, argv) 
    		if argc!=3
    			give error and usage message and exit
    		if argv[i] or argv[2] outside current directory
    			i.e. if they contain a  "/", give error and exit 
    		if argvEl] not in current directory
    			give error and exit 
    		link(argv[1], argv[2]) 
    		unlink(argv[1])
    

    Your C solution should follow this basic pattern. Try to make it as bullet proof as possible by trying to deal with as many error conditions as you can find, including checking error returns from system calls.


NEXT UP previous
Next: My Home Page