Executing command through a c program on Windows - c

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

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!)

Where to physically write commands for input redirection in C?

This might sound like a retarded question but I am just learning C and all the websites I looked at this showed you the command to do it (project < somefile.txt) but not where to do it. Does it go in my project somewhere or command prompt? And if it is command prompt how do I get to where I need to enter it?
It goes on your command prompt. You first need to cd into location where your compiled binaries are. That command basically says "run project and feed contents of somefile.txt into it"
If you're on Windows hit Win+R and run cmd. On UNIX you should find terminal app somewhere in your menu.
Write your C code to use the built in FILE pointers defined in stdio.h. Read from STDIN and write to STDOUT. When you run your command like this: project < somefile.txt, the shell will open somefile.txt as STDIN before your program runs.

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 ;)

Anyway to get return value of c program from command line?

I understand if I write a bash script I can get the return value, but is there anyway to get the return value without scripting, and just command line?
Yes, the same way you'd do in a Bash script. Run your program like this:
./your_program; echo $?
In light of the invalidation of the previous answer (good point, Carl Norum), let me re-phrase my comment as an answer:
BASH stores the return value of the previously run command in the variable $?. This is independent of the programming langauge used to write said command (the command can also be a shell internal).

Command line commands with 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?

Resources