parsing command line problem

This is a discussion on parsing command line problem within the C Programming forums, part of the General Programming Boards category; I'm writing a C program to implement a shell that supports a few commands... one of them being "exit" I ...

  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,006
    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, 10:13 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21