简体   繁体   中英

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

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. I tried out multiple different versions and can access a subdirectory of hte current folder

As pointed out by Ian Abbott, expansion of ~ is a feature of the shell program. Instead you can use the getenv function and read the HOME environment variable so you can construct the string $HOME/Desktop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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