Thread: having trouble with my chdir() function

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Tallahassee, Florida, United States
    Posts
    2

    Wink having trouble with my chdir() function

    for some reason its only allowing me to change my cwd once i was just needing some insight if i was using the chdir() correctly or not. Thanks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    
    
    
    
    #define MAX_COMMAND_SIZE 80
    #define MAX_ARGS 9
    #define HIS_SIZE 100
    
    
    
    
    typedef struct
    {
      int argument;                                   // userCom arguments
      char *arg[MAX_ARGS + 1];                  // userCom arguments array  
      //char *history[HIS_SIZE];                    //history array 
      char *input;                          // hold input file
      char *output;                        // hold output file
    } Command;
    
    
    
    
    
    
        
    
    
    int main()
    {
        Command userCom = {0};                         //holds userCom struct
        const char *whitespace = " \n\r\t\v\f";      // userCom delimiting chars
        char* username = getenv("USER");              //Get user name 
        char* curDirect = getenv("HOME");             //get cwd
        char* token[MAX_ARGS];    
        char* history[HIS_SIZE];
        char* cwd;
        char* buf;
        char* cmd;
        char buffer[MAX_COMMAND_SIZE + 1];         //hold userCom line
        //char history[HIS_SIZE][MAX_ARGS +1];    //2D array for history 
        int tok = 0;
        int new;
        int i;
        int limit;
        int last = 0; 
        int hist = 0;                            //initialize history size to 0 
        long size;
        struct stat buff;                        //holds file information
         
         
        //gets current working directory and also changes buffer if necessary 
        size = pathconf(".", _PC_PATH_MAX);
        if ((buf = (char *)malloc((size_t)size)) != NULL)
        cwd = getcwd(buf, (size_t)size);
        
        while(1){
        userCom.argument = 0; 
        userCom.input = NULL;
        userCom.output = NULL; 
        
          
        //prints users prompt 
        printf("\n%s@myshell:%s>", username,cwd); 
        
        //gets string from userCom line
        fgets(buffer, MAX_COMMAND_SIZE + 1, stdin);
        
        buffer[strlen(buffer)-1] = 0 ;//set null
        
        //set-up history function 
         history[last% HIS_SIZE] = cmd;
         last++;
        
        //parses tokens and looks for delimiters 
        token[tok] = strtok(buffer,whitespace);
        while(token[tok])
        {
            ++tok;
            if(tok == MAX_ARGS){
            printf("Reached MAX userCom arguments");
            break;
            }
            token[tok] = strtok(NULL, whitespace);
        }
        
        i =0;
        //sort tokens based on special characters
        for (;i<tok;++i)
    
    
        {
            if(!strcmp(token[i], "<"))
                {
                 userCom.output = token[++i];
                }
            else if(!strcmp(token[i], ">"))
            {
                userCom.input = token[++i];
            }
            else if (token[i][0] == '$')
            {
              char* toktok = getenv((const char*)&token[i][1]);
    
    
              if (!toktok)
                {
                  printf("%s: ERROR: variable.\n", token[i]);
                  return 0;
                }
              else
                {
                  userCom.arg[userCom.argument] = toktok;
                  ++(userCom.argument);
                }
            }
    
    
          else
            {
              userCom.arg[userCom.argument] = token[i];
              ++(userCom.argument);
        
            }
        }
            tok = 0;
            userCom.arg[userCom.argument] = 0;    
            
        //handles the "cd" command
            if((strcmp(userCom.arg[0],"cd") == 0))
            {    
                if (userCom.argument > 2){
              printf("cd: Too many arguments\n");
              }
              // change directories if valid target and update cwd
    
    
              else if (userCom.argument == 2)
                    {
                     new = chdir(cwd);
                      if (new != 0)
                          printf("%s: No such file or directory\n",cwd);
                          else{
                          size = pathconf(".", _PC_PATH_MAX);
                            if ((buf = (char *)malloc((size_t)size)) != NULL)
                            cwd = getcwd(buf, (size_t)size);
                          }
                    }
                    // if no argument is given, new directory should be $HOME
                      else{
                        new = chdir(curDirect);
                
                        size = pathconf(".", _PC_PATH_MAX);
                        if ((buf = (char *)malloc((size_t)size)) != NULL)
                        cwd = getcwd(buf, (size_t)size);
                    }
            }//end "cd" function

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A more thorough description of your problem would be good, with sample input and any output/error messages you see. Note, if your program is failing, but you get no error message, then you are probably not checking for all the errors you should be. Also, code that compiles: you're missing a few closing braces at least, and possibly other code that between line 166 above and the end of your program; who knows what else.

    Lastly, make sure you give all your variables (e.g. cmd) a value before you use them. Using uninitialized variables results in undefined behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with mkdir chdir
    By scripterJack in forum C Programming
    Replies: 10
    Last Post: 05-22-2010, 03:16 PM
  2. Need help with chdir()
    By ammochck21 in forum C Programming
    Replies: 8
    Last Post: 09-11-2008, 09:26 PM
  3. Replies: 12
    Last Post: 04-25-2007, 02:48 PM
  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

Tags for this Thread