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>
- void commands(char *s)
- {
- int status;
- char *argv[2];
- pid_t childpid;
- argv[0] = s;
- argv[1] = NULL;
- printf("%s", argv[0]);
- childpid = fork();
- if (childpid < 0)
- {
- exit (-1);
- }
- else if (childpid == 0)
- {
- printf("%s", argv[0]);
- if (execve(argv[0], argv, NULL) == -1)
- {
- exit (-1);
- }
- }
- else
- {
- wait(&status);
- }
- }
- int main()
- {
- char *buffer;
- size_t bufsize = 32;
- char cwd[1024];
- char *token;
- printf("\033[1;1H\033[2J");
- while (EOF)
- {
- getcwd(cwd, sizeof(cwd));
- buffer = malloc(bufsize * sizeof(char));
- if (buffer == NULL)
- {
- perror("Unable to allocate buffer");
- exit(-1);
- }
- printf("%s -> $ ", cwd);
- if ((getline(&buffer, &bufsize, stdin)) != -1)
- {
- token = strtok(buffer, " ");
- while (token != NULL)
- {
- commands(token);
- token = strtok(NULL, " ");
- }
- free(buffer);
- }
- else
- {
- free(buffer);
- exit (-1);
- }
- }
- free(buffer);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement