Thread: parsing command line problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    parsing command line problem

    I'm writing a C program to implement a shell that supports a few commands... one of them being "exit"

    I made a table of commands that are supported and I wanted to use strcmp to match it to the command in the table. As of write now I cannot get the program match exit when typed.

    Thanks for any help

    here is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    main() {
    	char *delimeter= " ";
    	char *tkn;
    	char s[30];
    	char *cmd[]={"dir", "md", "sort", "cd", "type", "del", 
    "exit","|",">"};
    	int i,f_des[2];
    	while(1) {
    		printf(">");
    		fgets(s,30,stdin);
    		tkn = strtok(s, delimeter);
    		while(tkn != NULL) {
    			if( strcmp(cmd[6],tkn) == 0){/*check if exit*/
    				exit(1);
    			}			
    			if( strcmp(cmd[7],tkn) == 0){/*Check for piping*/
    			
    			}
    			if( strcmp(cmd[8],tkn) == 0){/*Check for > */
    
    			}
    		}
    
    
    	}
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    fgets does not remove the newline from the entered string.

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem parsing 16-bit Hex to ASCII
    By black_spot1984 in forum C Programming
    Replies: 12
    Last Post: 12-05-2006, 11:13 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 05:02 PM