简体   繁体   English

在 C 程序中执行 /bin/ls 并且无法访问路径中带有 ~ 的目录

[英]executing /bin/ls in a C program and cannot access directories with ~ in path

I want to implement some pipes and reproduce a ls |sort |grep command so i started with the ls part and for testreasons i wanted to print out the conclusion from bin/ls part but i cant access upper directories我想实现一些管道并重现 ls |sort |grep 命令,所以我从 ls 部分开始,出于测试原因,我想从 bin/ls 部分打印出结论,但我无法访问上层目录

execl("/bin/ls", "ls", "-lR", directy, NULL); 

I want to access for example ~/Desktop which works fine from the shell but not from the program itself.我想访问例如 ~/Desktop ,它可以从 shell 正常工作,但不能从程序本身访问。 I tried out multiple different versions and can access a subdirectory of hte current folder我尝试了多个不同的版本,可以访问 hte 当前文件夹的子目录

As pointed out by Ian Abbott, expansion of ~ is a feature of the shell program.正如 Ian Abbott 所指出的,~ 的扩展是 shell 程序的一个特性。 Instead you can use the getenv function and read the HOME environment variable so you can construct the string $HOME/Desktop.相反,您可以使用getenv函数并读取 HOME 环境变量,以便构建字符串 $HOME/Desktop。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM