문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top