Thread: Need help with chdir()

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

    Need help with chdir()

    Hey all,

    This is my first time programming in C, but i am used to C++ so i am trying to use the same layout as I would with C++ for my program. I am a little confused about the chdir() part of my code. If someone could help me that would be great. Just disregard the other "crap" in the program for now, unless you see something that could be beneficial to me. Right now I am trying to focus on getting my chdir() to work!

    I am able to read in the cwd. The prompt is supposed to take in 'cd ..' and then go back one (as you all know how that works!). When I put cd in my prompt, it just exits out of the program. So that is where I am now....Thanks in advance!!

    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 mystring 100
    
      int changedir();
      unsigned int pid;                                                   //variable to hold id number
      int status;
    
    //------------------------
    void signal_handler(int sig)                                     //for the ctrl-z part
    {
      char answer[mystring];
      char *buf;
      long size;
      char *dir;
      printf("\n\n%s%d\n\n%s",
             "The Shell Has Been Locked ", sig ,
             "Please Enter the Password: ");
      scanf("%s", answer);
      if (*answer == 'I love operating systems')
        {
        signal(SIGTSTP, signal_handler);
        dir=  getcwd(buf, size);
        printf(dir);
        }
      else{
        printf("That is the wrong password.  Try again.");
    }
    }
    //------------------------
    void read_parse(char input_string[mystring], char command[], int  pipecount)
    {
      char *comm;
      command = strtok(input_string, " ");
    
      if(comm == "|")
        {
          command = strtok(input_string, "|");
          pipecount = pipecount + 1;
        }
    
      while(command != NULL)
        {
         command = strtok (NULL,"\n" ,"\n" );
        }
    }
    //------------------------
    int main() {
    
      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;          //variable pointer to the changed directory
      char input_string[mystring];
      char command[mystring];
      int flag = 1;
      int pipecount = 0;
    
    
          signal(SIGTSTP, signal_handler);
          printf("mysh$>");
          if ((buf = (char *)malloc((size_t)size)) != NULL)
            dir = getcwd(buf, size);                                          //this is the call to get the directory
          printf(dir); printf(" > ");                                            //will print the directory
          fgets (input_string, mystring, stdin);                       //gets command the user enters
          read_parse(input_string, command, pipecount);    //reads command the user enters
          if (input_string == 'cd')
            {
             chdir(command);   ///?????? I dont think my parameter is correct.......
             }
      return 0;
    
    }

    PS....also my signal handler wont lock the program...I can enter any password in and it will just exit.....

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You haven't got C strings yet. You cannot assign to arrays (in C or C++), so command = strtok(etc.) is doomed to failure. (You want to do comm = strtok(etc.), since that's why you declared the variable, and then use strcpy to actually copy the string.) 'cd' isn't a string (in C or C++), and to compare strings you need to use strcmp.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Wherever you thought you were assigning something to a string, you should use strcpy. You had it in read_parse, and that seems like a perfectly ordinary place to put it.

    (Also: any changes you make to pipecount in your function will NOT be reflected in main().)

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Ok well going back to my read_parse.......

    can you explain to me what char *comm, char command[], and input_string[mystring] should be doing with relation to each other?

    I think I am confusing myself now..... I am really sorry for the stupid questions...

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ammochck21 View Post
    Code:
    void signal_handler(int sig)                                     //for the ctrl-z part
    {
      char answer[mystring];
      char *buf;
      long size;
      char *dir;
      printf("\n\n&#37;s%d\n\n%s",
             "The Shell Has Been Locked ", sig ,
             "Please Enter the Password: ");
      scanf("%s", answer);
      if (*answer == 'I love operating systems')
        {
        signal(SIGTSTP, signal_handler);
        dir=  getcwd(buf, size);
        printf(dir);
        }
      else{
        printf("That is the wrong password.  Try again.");
    }
    }
    Calling library functions from inside a signal handler is INSANE. Don't do it.

    (Not to mention the other problems in this code.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I turned your read_parse into actual C, instead of "C++ with all the syntax errors removed", it might look like this:
    Code:
    int read_parse(char input_string[mystring], char command[], int pipecount)
    {
      int newpipecount = pipecount;
      char *comm;
      comm = strtok(input_string, " ");
    
      if(strcmp(comm,"|") == 0)
        {
          comm = strtok(input_string, "|");
          newpipecount++;
        }
    
      while(comm != NULL)
        {
         strcpy(command, comm); /*This is probably redundant if you have lots of lines, but see below*/
         comm = strtok (NULL,"\n" ,"\n" ); /*I have no idea what this is supposed to do*/
        }
      return newpipecount;
    }
    Basically, comm is your "scratchpad" that holds (pointers to) the strings you're working with. Edit: Oh, and pipecount will only ever go up by 1, unless that if was supposed to be a while.
    Last edited by tabstop; 09-11-2008 at 09:24 PM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    yeah i know i took those out...

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    and what is pipecount for?

    i know pipe() is used for process to communicate with each...right? my TA said I needed pipecount in there, but i guess I dont really understand it....boy do I feel dumb! I really appreciate all your help.


    Ok I kind of see what you/I am doing here! But I am trying to compare the string with "cd" so I can then go to chdir(). I feel like that is a long way off.......


    I need to do the strcmp now with cd correct?
    Last edited by ammochck21; 09-11-2008 at 09:30 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ammochck21 View Post
    and what is pipecount for?

    i know pipe() is used for process to communicate with each...right? my TA said I needed pipecount in there, but i guess I dont really understand it....boy do I feel dumb! I really appreciate all your help.
    Um, a pipe looks like this: "|". Soooo, I'm guessing pipecount is counting the number of pipes in the command that gets typed in. What you're going to do with it later is up to you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Replies: 12
    Last Post: 04-25-2007, 02:48 PM
  3. how to do...
    By kirmelyte in forum C Programming
    Replies: 1
    Last Post: 05-17-2006, 12:59 AM
  4. chdir()
    By YankeePride13 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2005, 03:49 PM
  5. Chdir()
    By Calavera in forum C Programming
    Replies: 1
    Last Post: 11-30-2004, 01:13 PM