Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/wait.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <signal.h>
  9.  
  10. char *dir(void)
  11. {
  12.     char *token2, *last = 0, cwd[1024];
  13.  
  14.     getcwd(cwd, sizeof(cwd));
  15.     token2 = strtok(cwd, "/");
  16.     for (; (token2 = strtok(NULL, "/")) != NULL; last = token2)
  17.         ;
  18.     return (last);
  19. }
  20.  
  21. void welcome(void)
  22. {
  23.     printf("\033[1;1H\033[2J");
  24.     printf("\033[1;31m");
  25.     printf("      :::::::::::::        :::      :::   :::  :::::::::: \n");
  26.     printf("     :+:       :+:      :+: :+:   :+:+: :+:+: :+:         \n");
  27.     printf("    +:+       +:+     +:+   +:+ +:+ +:+:+ +:++:+          \n");
  28.     printf("   :#::+::#  +#+    +#++:++#++:+#+  +:+  +#++#++:++#      \n");
  29.     printf("  +#+       +#+    +#+     +#++#+       +#++#+            \n");
  30.     printf(" #+#       #+#    #+#     #+##+#       #+##+#             \n");
  31.     printf("###       ##########     ######       #############       \n");
  32.     printf("__________________________________________________________\n");
  33.     printf("\n");
  34.     printf("                         WELCOME                          \n");
  35.     sleep(1);
  36.     printf("\033[m");
  37.     printf("\033[1;1H\033[2J");
  38. }
  39.  
  40. int check(char *s)
  41. {
  42.     struct stat st;
  43.  
  44.     if (stat(s, &st) == -1)
  45.     {
  46.         printf("\033[0;31mFlame: No such file or directory\033[m\n");
  47.         return (-1);
  48.     }
  49.     return (0);
  50. }
  51.  
  52. void commands(char *s)
  53. {
  54.     int status;
  55.     char *argv[2];
  56.     pid_t childpid;
  57.  
  58.     argv[0] = s;
  59.     argv[1] = NULL;
  60.     childpid = fork();
  61.     if (childpid < 0)
  62.     {
  63.         exit(-1);
  64.     }
  65.     else if (childpid == 0)
  66.     {
  67.         if (check(argv[0]) == 0)
  68.         {
  69.             if (execve(argv[0], argv, NULL) == -1)
  70.             {
  71.                 exit(-1);
  72.             }
  73.         }
  74.     }
  75.     else
  76.     {
  77.         wait(&status);
  78.     }
  79. }
  80.  
  81. int main(void)
  82. {
  83.     char *buffer, *token, *last = 0;
  84.     size_t bufsize = 32;
  85.  
  86.     welcome(); /** msg **/
  87.     last = dir();
  88.     while (1)
  89.     {
  90.         buffer = malloc(bufsize * sizeof(char));
  91.         if (buffer == NULL)
  92.         {
  93.             exit(-1);
  94.         }
  95.         printf("\033[1;32m%s \033[m==> $ ", last);
  96.         if ((getline(&buffer, &bufsize, stdin)) != -1)
  97.         {
  98.             token = strtok(buffer, " \t\r\n\v\f");
  99.             while (token != NULL)
  100.             {
  101.                 commands(token);
  102.                 token = strtok(NULL, " \t\r\n\v\f");
  103.             }
  104.             free(buffer);
  105.         }
  106.         else
  107.         {
  108.             free(buffer);
  109.             printf("\n");
  110.             exit(-1);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement