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>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <signal.h>
- char *dir(void)
- {
- char *token2, *last = 0, cwd[1024];
- getcwd(cwd, sizeof(cwd));
- token2 = strtok(cwd, "/");
- for (; (token2 = strtok(NULL, "/")) != NULL; last = token2)
- ;
- return (last);
- }
- void welcome(void)
- {
- printf("\033[1;1H\033[2J");
- printf("\033[1;31m");
- printf(" ::::::::::::: ::: ::: ::: :::::::::: \n");
- printf(" :+: :+: :+: :+: :+:+: :+:+: :+: \n");
- printf(" +:+ +:+ +:+ +:+ +:+ +:+:+ +:++:+ \n");
- printf(" :#::+::# +#+ +#++:++#++:+#+ +:+ +#++#++:++# \n");
- printf(" +#+ +#+ +#+ +#++#+ +#++#+ \n");
- printf(" #+# #+# #+# #+##+# #+##+# \n");
- printf("### ########## ###### ############# \n");
- printf("__________________________________________________________\n");
- printf("\n");
- printf(" WELCOME \n");
- sleep(1);
- printf("\033[m");
- printf("\033[1;1H\033[2J");
- }
- int check(char *s)
- {
- struct stat st;
- if (stat(s, &st) == -1)
- {
- printf("\033[0;31mFlame: No such file or directory\033[m\n");
- return (-1);
- }
- return (0);
- }
- void commands(char *s)
- {
- int status;
- char *argv[2];
- pid_t childpid;
- argv[0] = s;
- argv[1] = NULL;
- childpid = fork();
- if (childpid < 0)
- {
- exit(-1);
- }
- else if (childpid == 0)
- {
- if (check(argv[0]) == 0)
- {
- if (execve(argv[0], argv, NULL) == -1)
- {
- exit(-1);
- }
- }
- }
- else
- {
- wait(&status);
- }
- }
- int main(void)
- {
- char *buffer, *token, *last = 0;
- size_t bufsize = 32;
- welcome(); /** msg **/
- last = dir();
- while (1)
- {
- buffer = malloc(bufsize * sizeof(char));
- if (buffer == NULL)
- {
- exit(-1);
- }
- printf("\033[1;32m%s \033[m==> $ ", last);
- if ((getline(&buffer, &bufsize, stdin)) != -1)
- {
- token = strtok(buffer, " \t\r\n\v\f");
- while (token != NULL)
- {
- commands(token);
- token = strtok(NULL, " \t\r\n\v\f");
- }
- free(buffer);
- }
- else
- {
- free(buffer);
- printf("\n");
- exit(-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement