Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UNIX simple shell in C, execve and parameters
- [...] Preprocesser directives
- void read_command()
- {
- int i; //index to the arrays stored in parameter[]
- char *cp; //points to the command[]
- const char *hash = " "; //figures out the strings seperated by spaces
- memset(command, 0, 100); //Clear the memory for array
- parameter[0] = "/bn/"; //Initialize the path
- //Get the user input and check if an input did occur
- if(fgets(command, sizeof(command), stdin) == NULL)
- {
- printf("Exit!n");
- exit(0);
- }
- //Split the command and look store each string in parameter[]
- cp = strtok(command, " "); //Get the initial string (the command)
- strcat(parameter[0], cp); //Append the command after the path
- for(i = 1; i < MAX_ARG; i++)
- {
- cp = strtok(NULL, " "); //Check for each string in the array
- parameter[i] = cp; //Store the result string in an indexed off array
- if(parameter[i] == NULL)
- {
- break;
- cp = NULL;
- }
- }
- //Exit the shell when the input is "exit"
- if(strcmp(parameter[0], "exit") == 0)
- {
- printf("Exit!n");
- exit(0);
- }
- }
- int main()
- {
- [...]
- read_command();
- env = NULL; //There is no environment variable
- proc = fork();
- if(proc == -1) //Check if forked properly
- {
- perror("Error");
- exit(1);
- }
- if (proc == 0) //Child process
- {
- execve(parameter[0], parameter, env); //Execute the process
- }
- else //Parent process
- {
- waitpid(-1, &status, 0); //Wait for the child to be done
- }
- [...]
- }
Add Comment
Please, Sign In to add comment