0

I am writing a program that asks the user for a linux bash command and then stores them in pointer arrays (kind of like char *argv[]). The program must then make a check if this command is a normal bash command or a cd (change directory) command. If its a cd command then it should use something like chdir(). If the command is anything else I wanna use some variation of the exec() system call to execute that command.

However I am not succeeding with the first part (chdir()).

int ii=-1
printf("Enter the command: ");
fgets(command, 100, stdin);
command[strlen(command)-1]=0;
printf("Command = %s\n", command);


if (command[0]=='c' && command[1]=='d' && command[2]==' ')
{
    printf("I am inside CD now.\n");
    cd_dump[0] = strtok(command," ");
    while(sub_string[++ii]=strtok(NULL, " ") != NULL)
    {
        printf("%s\n", sub_string[0]);
    }

    chdir(sub_string[0]);
}

Edit: I have also tried the following if statement without luck.

if (command[0]=='c' && command[1]=='d' && command[2]==' ')
{
    printf("I am inside CD now.\n");
    chdir(command+3);
}

Sadly the program isn´t doing what I want it to, and even after hours trying to solve the issue I have no idea why. What have I done wrong? Also if I input cd /home/ why does the output result in sub_string[0] end up with an extra "Enter key" on the output? Does strtok save the Enter key into the string?

Any help on the subject is very much appreciated.

Arman Iqbal
  • 725
  • 2
  • 8
  • 14
  • 1
    are you sure you want `command[3]` inside that if? (The ones before it are 0 and 1) – FDinoff Jan 31 '14 at 05:41
  • 1
    fgets works that way. [The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or ***a is read and transferred to s***, or an end-of-file condition is encountered.](http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html) – Joachim Isaksson Jan 31 '14 at 05:45
  • What is the initial value of `ii`? Unless it is `-1`, the preincrement means that `sub_string[0]` doesn't contain the directory name. – Jonathan Leffler Jan 31 '14 at 05:52
  • I find inputs in c to be one of the most complicated things that I have ever encountered in my education. Is there a way to get rid of the EOL in fgets when you do not know how long the input string is? Or is there perhaps a better system call for that purpose? – Arman Iqbal Jan 31 '14 at 06:00
  • @ Jonathan Leffler: `ii` is `-1`. I will edit the initial post to clarify this. – Arman Iqbal Jan 31 '14 at 06:09

2 Answers2

2

Calling chdir() only affects the current process, not its parent process.

If you chdir() and exit immediately, it is pointless - the shell you call it from keeps its old cwd. That's why cd is always a shell builtin.

Use

char buffer[PATH_MAX];
if (getcwd(buffer, sizeof buffer) >= 0) {
    printf("Old wd: %s\n", buffer);
}
chdir(command+3);
if (getcwd(buffer, sizeof buffer) >= 0) {
    printf("New wd: %s\n", buffer);
}

to verify chdir() works correctly.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

I think I'd do something like this:

if (command[0]=='c' && command[1]=='d' && command[2]==' ')
{
    for(i=2, i++, command[i]!=' ');  /* Skip to nonspace */
    chdir(command+i);
}
user2624417
  • 67
  • 1
  • 1
  • Isnt `chdir(command+3)` exactly the same thing? I also do not quite understand this loop. when `i=3` in the loop the `chdir()` command would point to `command+3`, what happens after? When does the loop exit? – Arman Iqbal Jan 31 '14 at 06:06
  • That for loop is not correct. It lacks any `;`, if `i++` was supposed to be the test it will be `true` for, probably, many iterations. – CB Bailey Jan 31 '14 at 06:58
  • Consider what chdir(command+3) would do if: `command="cd\x20\x20\x20\x20\x20fred"` – user2624417 Feb 01 '14 at 17:47