如何从C文件在终端中更改目录 [英] How to change directory in terminal from a C file

查看:58
本文介绍了如何从C文件在终端中更改目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过C程序更改将在终端上生效的目录.

实际上,不提供有关system(")函数或chdir(")函数的信息.那些仅在C中的进程或子外壳中起作用.

说我正在从bash shell执行C程序,其进程ID为10223.因此,我可以编写一个会影响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>使用名称空间std;int main(){system("pwd");chdir("test");system("pwd");返回0;} 

此更改仅在C代码运行之前.从C代码返回后,如何继续更改当前环境.

UPD 1:

在终端中的

如果我写ps,则会得到以下输出.现在,我可以具有通过10991进程执行命令的任何功能.我认为这可以解决问题.

  sbmaruf @ kryptonyte:〜$ psPID TTY TIME CMD10991点/14 00:00:00重击11005点/14 00:00:00 ps 

解决方案

您没有.你不能 (*).工作目录是进程的属性.终端托管一个外壳;这是一个过程.您的程序是另一个过程.吐温永远也不会见面.

但是,您正在解决错误的问题.假设您有一个C程序,它将一个inode编号转换为路径,然后将结果路径打印到标准输出.现在,您只需要一个运行 cd $(my-program $ 1)的shell别名,就可以了-一个由inode cd的shell命令.

(*)如果使用 ptrace 编写要附加到Shell进程的程序,并且具有必要的权限,则可以强制Shell更改其工作目录.但是,由于外壳尤其是正在维护 PWD 之类的东西,因此其结果可能是不完全有效的.

您可以编写一个shell函数,该函数在每个检查文件的提示符下运行;如果文件存在,则函数将读取该文件并将工作目录设置为其中的路径.可能这不是您的初衷.

由于您使用的外壳是开源的(可能是bash,也许是zsh或fish),因此您也可以考虑制作自己的fork并添加自己的极其慢的shell命令,该命令将inode编号映射到目录,并将cd映射到它们./p>

How to change the directory from a C program that will effect on the terminal.

Actually don't tell about system("") function or chdir("") function. Those work only the the process or the sub-shell in C.

Say I am execution a C program from a bash shell whose process id is 10223. So, can I write a C code that will effect on the 10223 process which is a bash shell and whose path will be changed by my C program.

#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 ;
}

this change only until C code is running. after returning from C code how can I keep change the current environment.

UPD 1 :

in terminal if I write ps then I get the following output. Now can I have any function to execute a command via 10991 process. I think this solves the problem.

sbmaruf@kryptonyte:~$ ps
  PID TTY          TIME CMD
10991 pts/14   00:00:00 bash
11005 pts/14   00:00:00 ps

解决方案

You don't. You cannot (*). The working directory is an attribute of a process. The terminal hosts a shell; that's a process. Your program is another process. Never the twain shall meet.

However, you are solving the wrong problem. Assume that you have a C program that converts an inode number to a path, and then prints the resulting path to standard output. Now, all you need is a shell alias that runs cd $(my-program $1), and there you are -- a shell command that cd's by inode.

(*) If you write a program to attach to the shell process with ptrace, and you have the necessary permissions, you might be able to force the shell to change its working directory. However, since the shell in particular is maintaining things like PWD, the result of this may be incompletely effective.

You could write a shell function that ran at each prompt that checked for a file; if the file existed, the function would read it and set the working directory to the path in it. Probably this is not what you had in mind.

Since the shells you are using are open source (probably bash, perhaps zsh or fish), you might also consider making your own fork and adding your own extremely slow shell command that maps inode numbers to directories and cd's to them.

这篇关于如何从C文件在终端中更改目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆