【发布时间】:2015-11-01 00:45:36
【问题描述】:
如何从影响终端的 C 程序更改目录。
其实别说 system("") 函数或 chdir("") 函数。这些仅适用于 C 中的进程或子 shell。
假设我正在从进程 ID 为 10223 的 bash shell 执行 C 程序。 那么,我能否编写一个影响 10223 进程的 C 代码,该进程是一个 bash shell,其路径将被我的 C 程序更改。
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include<dirent.h>
#include <unistd.h>
using namespace std ;
int main(){
system("pwd") ;
chdir("test");
system("pwd") ;
return 0 ;
}
此更改仅在 C 代码运行之前进行。从 C 代码返回后,如何继续更改当前环境。
更新 1:
如果我在终端中写 ps 然后我得到以下输出。现在我可以有任何功能通过 10991 进程执行命令。我认为这可以解决问题。
sbmaruf@kryptonyte:~$ ps
PID TTY TIME CMD
10991 pts/14 00:00:00 bash
11005 pts/14 00:00:00 ps
【问题讨论】:
-
您不能更改另一个进程的当前工作目录。你为什么要这样做?
-
好的,我正在编写一个可以通过 inode 值更改目录的 C 代码。有可能吗?
-
毕竟,为什么需要通过inode cd?知道inode但不知道路径的情况是什么?