I'm learning C at the moment and have tasked myself with creating a shell within a Minix virtual machine, I'm doing this by using the library functions available to me already in Minix, such as ls, cd, ect...
I've encountered a problem where after forking for a child process, I'm causing core dumps, rather than executing my commands
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
/*Initialise variables*/
int pid;
char *envp[] = { NULL };
char userInput[256];
void isParent(){
int stat;
waitpid(-1, &stat, 0);
}
int main(int argc, char *argv[]) {
/*Infinite loop to cause shell to be "permenant"*/
while(1){
/*"*" to lead every line*/
printf("%s","*");
/*Get user input*/
scanf("%s", userInput);
/*Leave an exit clause, to not be permenantly stuck in loop*/
if(strcmp(userInput, "exit") == 0){
exit(1);
}
/*create my child process*/
pid = fork();
/*if process is parent, wait*/
if (pid != 0){
isParent();
}
/*Perform function typed by the user*/
execve(userInput, &argv[1], envp);
}
}
This is the code I'm working with so far, when passing /bin/ls as a parameter of my shell, I can get it to print ls, twice in one user input, however it exits the shell upon performing the act, which it shouldn't. I would like to be able to use other functions, have them print once, then return to waiting for user input.
When passing no parameters the shell will only accept "exit", no other commands. If I remove the argument clause (argv[]) from my main method, execve, or both, they throw errors, which you would expect.
I have read the documentation on all of the functions I have used and have chosen them specifically, so I'd appreciate not having to change them unless what I'm doing isn't actually possible with them.
Still learning C, so I'd appreciate smaller technical terms or easier to understand phrases. I'm not really sure if whatever my problem has been posed as a question before, but I've googled my problem in about 20 different ways and most versions of my problem have been written for c++, c# or aren't similar to my problem, from my understanding.
I'll be around for a few hours still so if I've missed any information feel free to comment and ask for clarification, information or anything else.