Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- struct command {
- char *prog;
- char **args;
- };
- int main(int argc, char *argv[])
- {
- char *path = "/bin/";
- while (1) {
- char input[512];
- printf("$sshell ");
- scanf("%s", input);
- if (strcmp(input, "exit") == 0) {
- exit(0);
- } else {
- char *cmd = malloc(strlen(path) + strlen(input) + 1);
- if (cmd == NULL) {
- printf("Malloc failed.\n");
- exit(69);
- }
- strcat(cmd, path);
- strcat(cmd, input);
- printf("%s\n", cmd);
- char *args[] = {cmd, NULL};
- pid_t PID = fork();
- if (PID == 0) {
- int status;
- status = execvp(cmd, args);
- if (status != 0) {
- fprintf(stderr, "Error: command not found.\n");
- exit(42);
- } else {
- exit(0);
- }
- } else {
- int status;
- wait(&status);
- fprintf(stderr, "+ completed '%s %s' [%d]\n", path, args[1], WEXITSTATUS(status));
- }
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement