Thread: Creating a shell - conflicting types error

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Creating a shell - conflicting types error

    I'm creating a shell in C and I'm getting an error. I don't know what to do, can any of you guys help? Sorry, it's probably really easy for you..

    http://i176.photobucket.com/albums/w...ontworktim.png

    full code:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    main()
    {
    	char buf[1024];
    	char *args[64];
    	
    	for(;;)
    	{
    	printf("Command:");
    
    	if(gets(buf) == NULL)
    	{
    	printf("\n");
    	exit(0);
    	}
    	if(strcmp(buf, "exit")==0)
    	{
    	exit(0);
    	}
    	
    	parse(buf, args);
    	
    	execute(args);
    	}
    }
    
    parse(buf, args)
    char *buf;
    char **args;
    {
    	while(*buf != NULL)
    	{
    		while((*buf == ' ') || (*buf == '\t'))
    			*buf++ = NULL;
    		*args++ = buf;
    	
    	while((*buf != NULL) && (*buf != ' ' ) && (*buf != '\t'))
    		buf++;
    	}
    	*args= NULL;
    }
    
    execute(args)
    char **args;
    {
    	int pid,status;
    
    	if ((pid= fork()) <0)
    	{
    	perror("fork");
    	exit(1);
    	}
    	
    	if (pid==0)
    	{
    		if(strcmp(*args, "help") ==0)
    			{
    				help();
    			}
    		if(strcmp(*args, "clr") ==0)
    			{
    				clr();
    			}
    		if(strcmp(*args, "host") ==0)
    			{
    				host();
    			}
    		if(strcmp(*args, "dir") ==0)
    			{
    				dir();
    			}
    		if(strcmp(*args, "environ") ==0)
    			{
    				environment();
    			}
    		if(strcmp(*args, "echo") ==0)
    			{
    				echo();
    			}
    		if(strcmp(*args, "cp") ==0)
    			{
    				cp();
    			}
    		if(strcmp(*args, "cd") ==0)
    			{
    				cd();
    			}
    		else
    			{
    			execvp(*args, args);
    		}
    			perror(*args);
    			exit(1);
    		}
    	while(wait(&status) !=pid)
    	;
    }
    
    help()
    {
    	printf("help - prints the list of commands supported\n");
    	printf("exit - exits the shell\n");
    	printf("clr - clears the screen\n");
    	printf("host - prints the hostname of the machine\n");
    	printf("dir - lists the contents within a directory ***\n");
    	printf("environ - lists the environment settings\n");
    	printf("echo - lists the environment settings ***\n");
    	exit(0);
    }
    
    clr()
    {
    	system("clear");
    	exit(0);
    }
    
    host()
    {
    	system("hostname");
    	exit(0);
    }
    
    dir()
    {
    	system("ls");
    	exit(0);
    }
    
    environment()
    {
    	extern char **environ;
    	 int i =0;
    	for(i=0;environ[i] !=NULL;i++)
    	{
    		printf(environ[i]);
    		printf("\n");
    	}
    
    }
    
    echo()
    {
    	puts("*args");
    	exit(0);
    }
    
    cp()
    {
    	system("cp -piv");
    	exit(0);
    }
    
    cd()
    {
    	int chdir(const char **args[2]);
    	int fchdir(int fd);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    cd()
    {
    	int chdir(const char **args[2]);
    	int fchdir(int fd);
    }
    Simple. Your declaration of "chdir" conflicts with the standard library signature.

    EDIT: As far as your warnings go, don't compare/assign characters to pointers (in this case, NULL).
    Last edited by Sebastiani; 03-14-2010 at 09:16 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    OK thanks, have you got any ideas about what I can do to gegt around that?

    Sorry I've never done C programming in my life I've only ever done basic Java, I have no idea about these errors and stuff yet I'm being expected to create a Shell in C that allows different commands such as change directory, echo, copy and delete files, show hostname, etc.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by h2o View Post
    OK thanks, have you got any ideas about what I can do to gegt around that?
    Well, the definition either needs to agree with the standard signature or else removed altogether, right? If this is a function you plan on implementing yourself then just rename it to something that doesn't clash with the standard library.

    The pointer/integer warnings can be eliminated by using 0 or '\0' instead of NULL.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by Sebastiani View Post
    Well, the definition either needs to agree with the standard signature or else removed altogether, right? If this is a function you plan on implementing yourself then just rename it to something that doesn't clash with the standard library.

    The pointer/integer warnings can be eliminated by using 0 or '\0' instead of NULL.
    Ah I think I get it, thanks.

    So just change chdir to something else, like chdir2?

    Also am I doing the write thing to get the input from what the user types? The **args[2] thing? Originally it was (constant char *path); so I changed it but I don't know if that's right? The string to get the users input is *args so is it right to put that in?

    Thanks.

    Code:
    cd()
    {
    	int chdir2(const char **args[2]);
    	int fchdir2(int fd);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM