Thread: segmentation fault... first time with unix...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    segmentation fault... first time with unix...

    I have never programmed in C on UNIX earlier and the first time I did returned some surprising results. I first coded the programmed in Turbo C in Windows and it compiled and executed perfectly well. Then I compiled the program in GCC in UNIX and some surprise I got when I learned a new thing called "Segmentation Fault". The programmed compiled with just a warning against using "gets" function and produced an executable. Running it, I encounter "Segmentation Fault" and the program terminates whenever (as I discovered through running the program couple of times) either the character array is tokenized or the pointer token's string is copied into an empty character array: don't know exactly which one of them is causing the trouble! I have never used UNIX before and have absolutely no idea what to do! Here's my code and also the exact output:-

    Code:
    //declaring preprocessor directives
    #include <stdio.h>
    #include <string.h>
    
    //method to validate user's input as  a legal Mynix command
    int parseInput(char checkCommand[])
    {
    	int counter;
    	char *inputToken, tempString[30];
    
    	//loop to verify any character in the input is either a small alphabet or a hyphen or a space
    	for (counter= 0; counter<strlen(checkCommand); counter++)
    	{
    		if (checkCommand[counter]<'a' || checkCommand[counter]>'z')
    		{
    			if (checkCommand[counter]!='-' && checkCommand[counter]!=' ')
    			{
    				printf("mynix> - No such thing!");
    				return 0;
    			}
    		}
    	}//character verification loop ends here
    
    	//checking if input is an exit command
    	if (strcmp(checkCommand, "exit")==0)
    	{
    		return 1;	//returning a non-zero value to exit the do-while loop in main
    	}
    
    	//checking for commands that can work without (or do not need) any switch or parameter like 'cd', 'ls', 'pwd' and 'cat'
    	else if(strcmp(checkCommand, "cd")==0 || strcmp(checkCommand, "ls")==0 || 
    	        strcmp(checkCommand, "pwd")==0 || strcmp(checkCommand, "cat")==0)
    	{
    		printf("mynix> - Done!");
    		return 0;
    	}
    
    	//checking for commands that can have (or need) switches or parameters like 'cd', 'ls', 'cat', 'man', 'mkdir', 'whereis'
    	else
    	{
    		inputToken= strtok(checkCommand, " ");	//tokenizing by space for first part
    		
    		//checking first token for a valid command
    		if (strcmp(inputToken, "man")==0 || strcmp(inputToken, "ls")==0 || 
    		    strcmp(inputToken, "cd")==0 || strcmp(inputToken, "mkdir")==0 || 
    		    strcmp(inputToken, "cat")==0 || strcmp(inputToken, "whereis")==0)
    		{
    			inputToken= strtok(NULL, " ");	//tokenizing by space for second part
    			strcpy(tempString, inputToken);	//storing token's value in a string
    
    			//checking that second part does not start with a hyphen and that its a valid parameter
    			if (tempString[0]!='-' && strlen(tempString)>0)
    			{
    
    				inputToken= strtok(NULL, " ");	//tokenizing by space for third part
    				strcpy(tempString, inputToken); //storing token's value in a string
    
    				//checking that there is no third part after a parameter
    				if (strcmp(tempString, NULL)==0)
    				{
    					printf("mynix> - Done!");
    					return 0;
    				}
    
    				else
    				{
    					printf("mynix> - No such thing!");
    					return 0;
    				}
    			}//third part verification ends here
    
    			//checking validity if second part starts with a hyphen
    			else
    			{
    				//checking that there is only one character after a hyphen in the second part
    				if (strlen(tempString)==2)
    				{
    					inputToken= strtok(NULL, " ");	//tokenizing by space for third part
    					strcpy(tempString, inputToken);	//storing token's value in a string
    
    					//checking that after a valid switch the third part does not start with a hyphen
    					if (tempString[0]!='-' && strcmp(tempString, NULL)!=0)
    					{
    						inputToken= strtok(NULL, " ");	//tokenizing by space for fourth part
    						strcpy(tempString, inputToken);	//storing token's value in a string
    
    						//checking that there is no fourth part
    						if (strcmp(tempString, NULL)==0)
    						{
    							printf("mynix> - Done!");
    							return 0;
    						}
    
    						//if more than three parts are there in the command
    						else
    						{
    							printf("mynix> - No such thing!");
    							return 0;
    						}
    					}
    					
    					//if parameter is incorrectly declared
    					else
    					{
    						printf("mynix> - No such thing!");
    						return 0;
    					}
    				}
    				
    				//if switch is incorrectly delcared
    				else
    				{
    					printf("mynix> - No such thing!");
    					return 0;
    				}
    			}//second part verification if-else ladder ends here
    		}
    		
    		//if first part not valid then
    		else
    		{
    			printf("mynix> - No such thing!");
    			return 0;
    		}//first part verification if-else ladder ends here
    	}//total command verification if-else ladder ends here
    }//method parseInput ends here
    
    //main method begins here
    int main()
    {
    	char userInput[30];
    	int exitVariable= 0;
    
    	//do-while loop to prompt user for next command till exit command is executed
    	do
    	{
    		printf("\nmynix> ");
    
    		gets(userInput);
    
    		exitVariable= parseInput(userInput);
    	}
    	while(exitVariable==0);
    	//do-while loop ends here
    	
    	return 0;	//no value required to be returned by main method
    }//main method ends here
    //program ends here
    The output is:-

    Code:
    mynix> hey
    mynix> - No such thing!
    mynix> ls
    mynix> - Done!
    mynix> ls -a
    Segmentation fault
    Please help! Thanks a lot for taking out some time!
    Last edited by Salem; 09-30-2008 at 11:39 AM. Reason: Fold long lines for readability

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  3. Replies: 7
    Last Post: 12-10-2004, 01:58 AM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM