0

I'm another CS beginner working on a simple Shell. At the moment I am trying to change the current directory if an argument is passed, else, report to the current directory.

I tried using chdir() in my program, but it's apparently not working. I tried passing a char* arguments which is tokenized. I also tried with argv[1], but I must be doing something wrong because neither seems to work.

Also, I'm not exactly sure how to make the argument pointer (containing the directory string) static, so that when i use putenv(ARGUMENT HERE) there are no issues.

Here is the pertaining part of my code:

else if (strncmp(command[0], "cd", 2) == 0)
    {
        char *argmnts = strtok(0, " ");

        if (arguments != NULL)
        {
            chdir(argmnts); 
            putenv(argmnts); // THE ARG STRING NEEDS TO BE A STATIC COPY
            getcwd(promptBuff, sizeof(argmnts));
        }
    }

The pointer argmnts points to the tokenized argument part from: char strnBuffer[1000] which has already been tokenized for the command: command[0] = strtok(strnBuffer, " ");

I really appreciate any help/insight.

Thank you.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Gus
  • 215
  • 1
  • 6
  • 16
  • 1
    sizeof(argmnts) is going to be 4. Probably not what you want. Maybe sizeof(promptBuff). – Charlie Burns Oct 15 '13 at 18:56
  • chdir() expects a path, putenv() expects a string of the form name=value. – Charlie Burns Oct 15 '13 at 18:57
  • 1
    more then one problems [read](http://pic.dhe.ibm.com/infocenter/zos/v1r12/index.jsp?topic=%2Fcom.ibm.zos.r12.bpxbd00%2Frtgtc.htm) – Grijesh Chauhan Oct 15 '13 at 18:58
  • 1
    chdir(), putenv() and getcwd() all return values telling if they succeeded or not. You are ignoring them all. Not good programming practice. – Charlie Burns Oct 15 '13 at 18:59
  • I took out all the lines using the return values to check for errors, I just wanted to focus on figuring out the core issue. But i have been using them, that's how i know chdir() isn't working properly. It's returning -1 :-/ – Gus Oct 15 '13 at 19:10
  • You say in your post: *but it's apparently not working* and *neither seems to work*. Could you explain what you mean by that? Do you mean it does nothing? Do you get an error? Some other unexpected result? – lurker Oct 15 '13 at 19:28
  • chdir() is not returning 0. it returns -1. I'm not sure what exactly i'm doing wrong, but it doesn't return 0. – Gus Oct 15 '13 at 19:37
  • You probably have a `'\n'` left over on the end of the input line. Your `strtok` only recognizes space as separator, so it won't touch the newline. `chdir("dir\n")` will fail unless you actually have a directory with the newline at the end of its name. –  Oct 15 '13 at 20:28
  • @WumpusQ.Wumbley, Sir, you are brilliant. I knew it was something simple, I've just looked at it so much, I can't see mistakes anymore. Thank you! – Gus Oct 15 '13 at 21:25
  • copied my apparently brilliant comment as an answer –  Oct 15 '13 at 23:51

1 Answers1

0

You probably have a '\n' left over on the end of the input line. Your strtok only recognizes space as separator, so it won't touch the newline. chdir("dir\n") will fail unless you actually have a directory with the newline at the end of its name.