Is there a way to execute shell-commands in the same shell-instance? Since, system() leaves the started shell after the command is executed.
2 Answers
You could always contruct your shell command as a single line command using semicolons. Such as:
cd /home/user;mkdir tmp;ls
1 Comment
user2224350
That's not possible in my case
Do you mean execute the command in the same terminal that run your program? You can achieve that with popen:
#include <stdio.h>
int main() {
FILE *f = popen("ls", "r");
char line[1024];
size_t len;
while (fgets(line, 1024, f) != NULL) {
printf("%s", line);
}
pclose(f);
return 0;
}
2 Comments
user2224350
I'm not sure.. I want to make a "web shell". A website which I can use like a remote shell. So the main() function will be executed by a webserver. So fare I use system() to execute the commands transferred from the client. But eg.
cd doesnt work in this wayZiming Song
If you are writing a web server, you might want to consider languages like node.js, python or php. C is not ideal for a website.