Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/wait.h>
- /**
- * exe - execute the cmd
- * @str: string that contains the cmd
- */
- void exe(char str[])
- {
- char *cmd;
- const char del[] = " \t\r\n\v\f";
- char *token;
- cmd = strdup(str);
- /* get the first token */
- token = strtok(cmd, del);
- if (token != NULL)
- {
- char *av[] = {token, NULL}; /* set av[0] in the token */
- if (execve(av[0], av, NULL) == -1)
- {
- perror("Error");
- }
- }
- }
- /**
- * main - a super simple shell that can run commands with their full path,
- * without any argument.
- *
- * Return: 0 success
- */
- int main(void)
- {
- size_t size = 100;
- char *cmd;
- /*system("clear"); clear screen */
- printf("press Ctrl + D to exit\n");
- while (1) /* allows the loop to run indefinitely */
- {
- cmd = (char *)malloc(size * sizeof(cmd)); /* set memory space for cmd */
- if (cmd == NULL)
- {
- exit(-1);
- }
- printf("SHELL >> ");
- /* if it throws error, it frees memory and ends */
- if (getline(&cmd, &size, stdin) == -1)/* get command entered in console */
- {
- free(cmd);
- putchar(10);
- exit(-1);
- }
- /* create the child process and send the command to execute */
- pid_t pid = fork();
- if (!pid)
- {
- exe(cmd);
- break;
- }
- else
- wait(NULL);
- free(cmd);
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement