Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #define MAX_ARGS 256
- int main()
- {
- char* input = NULL;
- size_t n = 0;
- printf("***MIRKOSHELL***\n");
- printf("type exit to exit\n");
- while (1)
- {
- printf("> ");
- ssize_t bytes_read = getline(&input, &n, stdin);
- if (bytes_read <= 0) {
- fprintf(stderr, "Invalid input\n");
- continue;
- }
- if (input[bytes_read - 1] == '\n')
- input[bytes_read - 1] = 0;
- if (strcmp(input, "exit") == 0) {
- break;
- }
- char* args[MAX_ARGS + 1] = { strtok(input, " "), 0 };
- char* ptr;
- int i = 1;
- while ((ptr = strtok(NULL, " ")) && i < MAX_ARGS)
- args[i++] = ptr;
- if (pid == -1) {
- perror("fork");
- exit(EXIT_FAILURE);
- } else if (pid == 0) {
- execvp(args[0], args);
- perror("execvp");
- exit(EXIT_FAILURE);
- } else {
- waitpid(pid, NULL, 0);
- }
- }
- exit(EXIT_SUCCESS);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement