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>
- void doStuff(char *input);
- 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 {
- doStuff(input);
- exit(7);
- }
- }
- return EXIT_SUCCESS;
- }
- void doStuff(char *input) {
- COMMAND comm;
- char *temp = strtok(input, " ");
- char *arg = strtok(NULL, " ");
- comm.args = NULL;
- int count = 0;
- comm.args = (char**)realloc(comm.args, (count + 1) * sizeof(*comm.args));
- comm.args[0] = (char*)malloc(sizeof(temp));
- strcpy(comm.args[count], temp);
- count++;
- while (arg != NULL) {
- comm.args = (char**)realloc(comm.args, (count + 1) * sizeof(*comm.args));
- comm.args[count] = (char*)malloc(sizeof(arg));
- strcpy(comm.args[count], arg);
- count++;
- arg = strtok(NULL, " ");
- }
- comm.args = (char**)realloc(comm.args, (count + 1) * sizeof(*comm.args));
- comm.args[count] = (char*)malloc(sizeof(NULL));
- comm.args[count] = NULL;
- comm.prog = malloc(strlen(path) + strlen(temp) + 1);
- if (comm.prog == NULL) {
- printf("Malloc failed.\n");
- exit(69);
- }
- strcat(comm.prog, path);
- strcat(comm.prog, input);
- printf("%s\n", comm.prog);
- pid_t PID = fork();
- if (PID == 0) {
- int status;
- status = execvp(comm.prog, comm.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, comm.args[1], WEXITSTATUS(status));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement