Domanda

I am trying to run a minimal terminal shell program through a socket on a unix machine. When I use system("cd directory") it doesn't work. So I did a bit of research and came across chdir(), which looked like it would work. When I tried it, though, it gave me errors every single time. Here is my code:

if (chdir(argument) < 0) {
     send(sock, "[*] Directory does not exits!\n", strlen("[*] Directory does not exits!\n"), 0);
}
else {
    string argumentString(argument);
    string entireMessage = "[*] New working directory: " + argumentString;
    char entireMessageChar[64];
    int i;
    for (i = 0; entireMessage[i] != '\0'; i++) {
        entireMessageChar[i] = entireMessage[i];
    }
    send(sock, &entireMessage, strlen(entireMessageChar), 0);
}

Could anyone please tell me why chdir() isn't changing directories? Thanks.

È stato utile?

Soluzione

The value of errno after chdir() has exited needs to be checked to determine why it doesn't work.

Likely exit status values include:

  • EACCESS (no permission for the target)
  • ENOENT (file does not exist)
  • ENOTDIR (target is a file, not a directory).

Check the documentation for your operating system's implementation for a complete list.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top