A way to exit the system shell (/bin/sh) from inside C - 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!)

Related

Get information from stream 1

I have to do a little project for my school and I would like to have some help for a little problem.
For this project I have to recreate a command interpreter like bash/shell without advanced option like redirection or pipe for now.
I finished it and i decided to do a little more. I created a prompt like this :
"my_login" ~/home/"my_login"/rendu/PSU_2014_minishell1 $
But when i do "cd" my prompt needs to change , problem is I can't refresh it because i took the environment variable PWD.
And to complicate a little more the work, i can't use every systems call I should use only:
opendir
readdir
closedir
malloc
free
exit
chdir
fork
stat
lstat
fstat
open
close
read
write
execve
access
wait
waitpid
wait3
wait4
signal
kill
I have an idea but I don't know how to do it. I thought I can execute the binairy "/bin/pwd" to have my location , but I don't how how to get this information after execute it, have you any idea?
Thanks !
And sorry for my bad english, I am French.

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

using the Unix alias command in C program

So I'm not exactly sure if this is possible, but I would like to use unix's alias command for creating aliases of commands I have created inside of my C program.
So far the only idea I have had to try is system(alias ='someCommand');
This resulted in nothing happening
Is this even possible? Or do I have to essentially create my own alias function for my c program (which is a simple shell program)
Thanks, I'm new to this and would appreciate any insight!
Aliases are meant as shortcuts that you create to reduce your typing when working inside the shell. When you execute a program that you created by compiling your C source code, the shell forks and execs the program. Any changes that you intend to make to the shell happen inside that execed code. As soon as the program is terminated, your changes disappear with it. Hence, what you observe: "Nothing happens".

Running program in background

Ive got my program in C, 6 source files, and the aim is to copy those files to any other Linux OS computer, and (probably compile, im newbie, so not sure what is needed here) run this program in background. Something like:
user#laptop:~$ program
Program is running in a background. In order to stop Program, type
XXX.
Any tips on this?
Thanks in advance!
Put a daemon(0,0); call in your C program.
stopping it is a bit trickier, I suppose there is only one copy of the program running. Put the program's PID in a file, write another utility (XXX) which reads the PID from the file and kills it.
Important: daemon forks, get the PID of the program after calling daemon.
But maybe you are too newby and just want to execute your program with program& and later kill it.
I completely missunderstood the question. You need shell scripting for this.
For file copying you can use scp. Execute command on the other host with ssh. It should be something like (not tested):
pid=`ssh user#host "make >/dev/null 2>&1; nohup ./program; echo $!`
later you can stop it with
ssh user#host "kill $pid"
First, you should fork().
In parent, you should just exit, in child process - you should handle SIGHUP signal.
In such way - you have daemon.

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