cant get my input to work...

This is a discussion on cant get my input to work... within the C Programming forums, part of the General Programming Boards category; hey guys, ok right now I am trying to enter some input, for example, 'cd'. right now it will get ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    cant get my input to work...

    hey guys,

    ok right now I am trying to enter some input, for example, 'cd'. right now it will get and print the current directory I am in. but when I enter anything, it exits out of my prog. I dont know if my input is being tokenized properly or what. So I am basically just working on the cd input right now. I want it to change my directory but its not doing anything! Also the reason I have my while loop commented out (in main) is because it stays in the infinite loop and doesnt do anything. So I just dont know whats going on! I have been working on this for a week and I just cant get it!

    Please help! Thanks!

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/param.h>
    #define maxbuf 256
    
    unsigned int pid;                                                   //variable to hold id number
    int status;
    
    void signal_handler(int);
    void parse_string(char [], char *[], int);
    char arith(char *[]);
    
    //------------------------
    char arith(char *command[]){
      int finalvalue;
      int temp1;
      int temp2;
    
      if (command[2] == '/') {
        temp1 = atoi(command[1]);
        temp2 = atoi(command[3]);
        if(temp2 == 0)
          return 0;
        finalvalue = temp1 / temp2;
      }
      else if (command[2] == '*') {
        temp1 = atoi(command[1]);
        temp2 = atoi(command[3]);
        finalvalue = temp1 * temp2;
      }
      else if (command[2] == '+'){
        temp1 = atoi(command[1]);
        temp2 = atoi(command[3]);
        finalvalue = temp1 + temp2;
      }
      else if (command[2] == '-'){
        temp1 = atoi(command[1]);
        temp2 = atoi(command[3]);
        finalvalue = temp1 - temp2;
      }
    
      return finalvalue;
    }
    //------------------------
    void signal_handler(int sig)                                        //for the ctrl-z part
    {
      char *answer[maxbuf];
      printf("\n", "The Shell Has Been Locked ", "Please Enter the Password: ");
      scanf("&#37;s", answer);
      if (*answer == 'I love operating systems')
        signal(SIGTSTP, signal_handler);
    
      return;
    }
    //------------------------
    void parse_string(char input_string[], char *command[], int size_buf)
    {
      char *token;
      int i =0;
      for((token = strtok(input_string, " \n")); token; (token = strtok(NULL, " \n")), i++)
        {
          if( i < size_buf - 1)
          command[i]=token;
        }
          command[i] = NULL;
    
      return;
    }
    //------------------------
    int main() {
      int x = 0;                       //holds answer from calculations
      char *buf;                       //pointer to where the directory is
      long size;                       //size of the array, which is the directory name
      char *dir;                       //this is the variable that will store the direcory name and then print it out
      char *path;                      //this is the array that will hold the changed directory's name
      char input_string[maxbuf];       //this is the array which will hold the input
      char *command[maxbuf];           //this is the array which will hold the tokenized input
      int flag = 1;
      int pipecount = 0;
    
      signal(SIGTSTP, signal_handler);                        //called when the user hit ctrl-z
          printf("mysh$>");
          if ((buf = (char *)malloc((size_t)size)) != NULL)
            dir = getcwd(buf, size);                          //this is the call to get the directory
          printf("%s >", dir);                                //will print the directory
          fgets (input_string, maxbuf, stdin);                //this will get the command the user enters
          parse_string(input_string, command, maxbuf);        //this will tokenize the command the user enters
          //while(command[0] != NULL) {                       //stays in infinite loop????????
           if (command[0] == 'cd'){
            dir = chdir(path);
            printf("%s%", dir); }
          // else if (command[0] == 'quit')
          //   exit(0);
          // else if (command[0] == 'dir')
          //   command[0] = "ls";
          // else if (command[0] == 'type')
          //   command[0] = "cat";
          // else if (command[0] == 'cls')
          //   command[0] = "clear";
          // else if (command[0] == 'move')
      //   command[0] = "mv";
          // else if (command[0] == 'copy')
          //   command[0] = "cp";
          // else if (command[0] == 'del')
          //   command[0] = "rm";
          // else if (command[0] == '='){
            //x = arith(command);
            // printf("%s", x); }
          //else
          //        printf("Bad Command.  Please try again.");
          //}                                                                       //closes while loop
          //execvp(command[0], command);                     //will execute the commands
    
      return 0;
    }

    SO I just tried something, and it seems to me that my string isnt even being tokenized......I think thats where the prob is!
    Last edited by ammochck21; 09-24-2008 at 06:06 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Is proper indentation a modern tabboo in computer science that I have been left uninformed about? I am not seeing any reason for your program to even ever have knowledge of the current working directory.... The only information it might know is the directory of the executable that is argument[0] in main... but even at that you are not using arguments of main (btw. you should have main(void) not just leave it blank like that... in C that is the same as saying main(...)).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (command[0] == 'cd'){
    That compares the pointer in command[0] with the constant 'cd', which is the numeric value of 'c' + 'd' * 256 (or 'c' * 256 + 'd', depending on the byte order of your machine, but it's less important what the exact value is). Your pointer, most likely, isn't that value.

    You need to use
    Code:
    strcmp(command[0], "cd") == 0
    to do what I think you want to do.

    Since there is no loop or such in your code, if the if-statement is false, it just falls out the bottom of main with return 0 and that finishes your program, so that's why it's not asking again or doing anything else after you entered "cd".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Thanks that makes sense to me! So then would I still need that while loop I have right above it if I do all string compares?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > if (command[0] == 'cd')
    Ok, did you try this?
    gcc -W -Wall prog.c

    It seems to me that winding up the warning levels will tell you a hell of a lot of things which are wrong right off the bat, rather than spending days "debugging" to find the same information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > if (command[0] == 'cd')
    Ok, did you try this?
    gcc -W -Wall prog.c

    It seems to me that winding up the warning levels will tell you a hell of a lot of things which are wrong right off the bat, rather than spending days "debugging" to find the same information.
    As my signature says... Those poor guys that write compilers aren't adding warnings JUST because it's so much fun...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    As my signature says... Those poor guys that write compilers aren't adding warnings JUST because it's so much fun...

    --
    Mats
    Bah, anyone can write programs with help from the compiler... :P

    Which reminds me of a quote I heard the other day:
    Never try to fool the compiler, the compiler will retaliate
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Guy takes his car back to the garage in a big huff.

    Guy: "You said this thing would do 100MPH no problem, but I can't get it past 20 no matter how much I rev the engine"
    Salesman: "Which gear were you in?"
    Guy: "Gears, wtf are gears?"

    Having some sense to explore all the features that a tool provides would be a good idea. Not just taking it out of the box without reading the manual, achieving some simple action, then complaining when it doesn't work as advertised.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 03:16 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 09:45 PM

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