Command line commands with C - c

I'm sorry if this was covered before, but I can't find it anywhere on StackOverflow.
Basically I'm trying to run things that you usually run at a Windows command prompt:
msiexec /i file.msi /q
and other sort of commands from my C program. Is this possible?
Thanks.

In windows using the Win API ShellExecute will give you best control of your child process. However the other two methods mentioned by Dave18 and Pablo work as well.

Try C system function
#include <stdlib.h>
int main ()
{
system ("msiexec /i file.msi /q");
return 0;
}

You need to use one of the functions from the exec family of function. Here's a list of them.
So, to run your example you can use:
execl("msiexec","/i","file.msi","/q",NULL);

Pablo and Dave are right, depending on what you want to do.
execl loads the new application into memory and runs it in place of the current process. Your program will end after the execl() call.
System runs the application in a subshell, you can retrieve it's exit status but not any information about it's stdin/stdout data.
How interested are you in what happens after you start the process?

Related

A way to exit the system shell (/bin/sh) from inside C

I am doing a project which requires me to exit a Busybox shell using a C program instead of just typing "exit" when the shell is opened to exit /bin/sh.
To call /bin/sh, i just need to run
system("/bin/sh");
but when i want to exit using:
system("exit");
it doesn't work, i am quite new to C and system shell in C, and have been looking for answers for a while. Hope that someone can enlighten me on this problem. (other ways or solutions is also okay!)

Executing command through a c program on Windows

I want to know how to execute a command from a c program,on windows os.
To be more specific how to write a c program whose output will not be printed but directly goes to command prompt and get executed there? please help me
I think you need to use system() command in your C code.
For example:
system("pause");
where "pause" is the command to be executed in cmd.
reference: http://www.cplusplus.com/reference/cstdlib/system/
I hope i got your question right.
I'm not sure I understand the question correctly. But if I do, you're looking for the system() function.
I suspect what you are describing is the the back-ticks in shells in Linux/Unix.
However, I don't know how to do that in Windows.
Unix way
myprompt> `./a.out`
If the C program was basically: printf("ls -l .\n");, then this should list the files.
Is that what you wanted?
Like I said, I don't know how to do that in the Win Cmd Prompt, but maybe this clarifies your question.
Looks like you could try:
C:\MyDir> MyProgram.exe | cmd.exe /C

(Linux, C) Two threads, two separate text windows in the screen, how to do it?

I have two threads. The main thread and two others created with *pthread_create*, for instance thread_1 and thread_2.
The main thread writes the output in the terminal that i used to run the program. Then, i want to create two new windows, in which thread_1 and thread_2 will write some text.
i tried to use the system call "system()" and run "/bin/bash" but there is no new terminal appearing after that.
now, i am thinking to use some graphic library (g2, SDL...).
i searched the web for some hours and cant find a good solution. All i just need is to output text in independent windows, i don't want any kind of drawings, only text lines.
i am using Linux mint and C language.
Do you need the program to open the other two windows?
If not:
Have each thread write to a separate file.
Manually open two more windows.
Use the bash command 'tail -f' in each of the new windows to display the output of each file.
The closest non-gui solution that comes to mind is ncurses
Thanks for the advices.
I achieved a solution for my problem with the Jay's hint.
It's very simple. i put the program writing the data to a file. (closest to the fifo idea)
then, inside the program i run this:
system("mate-terminal -e --command='tail -f filename.txt'");
and voilá! It creates a new terminal window and runs the command tail -f.
Thanks for your answers, they helped me thinking in that solution. I tried the ncurses but had problems in installing the package and then compiling with the -lncurses flag. I will try ncurses someday, but now the problem is solved.
bye

C: IFS System() Vulnerability

For educational reasons I have to exploit an C-Code
The Programm set the egid first, and then the vulnerability with the system("/usr/bin/..."); Command.
So I made an 'usr' executeable in my Home-Directory and set the Path to the Home PATH=$HOME:$PATH
And I want to change the IFS Variable in the bash to /: export IFS='/'
Unfortunatelly, when i call the C-Programm: my exploit doesn't work
Is anybody able to tell me what is wrong?
Add the IFS as part of your program's call to system(). System executes the code with /usr/bin/sh -c. So you can do similar to what you'd in the shell prompt.
system("export IFS='/'; /usr/bin/cmd");
Note that once the child process is terminated, the IFS set will no longer be available in the parent.
I suppose we are studying at the same university, because I am currently confronted with the same problem. I don't want to give you the whole solution, because that would be too easy =)
Your IFS variable is not ignored, but it doesn't work as you might think. When you call the C-Programm there is an additional output in the shell, which refers to the lesspipe. With the information in this link and this german link you are able to solve the challenge1 ;)

Catching shell script output in program c

I have C program ( program.c ) that calls a shell script ( command.sh ).
command.sh returns an output (a password), i need to get this output in my program.c.
using system(command); , i can't get hold of the output.
is there any other way in C to solve my problem?
Not in pure C. You want POSIX. Specifically popen function.
The specification has a nice example, that you can just copy 1:1
http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html
Sounds like you're afraid to use libraries. Please try and use libraries, they're just as much part of Unix as shell tools.
In pure C (well ... ignoring the contents of the system() call) you can redirect the shell script output to a file, then read that file.
system("whatever > file");
handle = fopen("file", "r");
/* if ok use handle */
fclose(handle);
You can use popen() as suggested above, but note that in this case you have no control over the process you created (e.g. you will not be able to kill it), you also will not know its exit status.
I suggest using classical pipe/fork/exec combination. Then you will know the pid of your child process, so you will be able so send signals, also with pipe() you are able to redirect process standard output, so you can easily read it in your parent process. As example you can see my accepted answer to popen() alternative.
You should open a pipe using popen but this sounds tedious. A C program calling a shell script for a password.

Resources