loisduplain

backup

Mar 2nd, 2022
1,223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   microshell.c                                       :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: lduplain <[email protected]>      +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2021/01/09 15:59:05 by gbaud             #+#    #+#             */
  9. /*   Updated: 2022/03/01 14:54:35 by lduplain         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16.  
  17. #define ERR_FATAL "error: fatal\n"
  18. #define ERR_EXEC "error: cannot execute "
  19. #define ERR_CD_ARG "error: cd: bad arguments\n"
  20. #define ERR_CD_DIR "error: cd: cannot change directory to "
  21.  
  22. int ft_strlen(char *str)
  23. {
  24.     int index;
  25.  
  26.     index = 0;
  27.     while (str[index])
  28.         index++;
  29.     return (index);
  30. }
  31.  
  32. int put_err(char *err, char *path)
  33. {
  34.     write(2, err, ft_strlen(err));
  35.     if (path != NULL)
  36.     {
  37.         write(2, path, ft_strlen(path));
  38.         write(2, "\n", 1);
  39.     }
  40.     return (1);
  41. }
  42.  
  43. void    sub(char **cmds, char **av, int from, int to)
  44. {
  45.     int cmd_arg_index;
  46.  
  47.     cmd_arg_index = 0;
  48.     while (from < to)
  49.         cmds[cmd_arg_index++] = av[from++];
  50.     cmds[cmd_arg_index] = NULL;
  51. }
  52.  
  53. int cd(char **av, int len)
  54. {
  55.     if (len != 2)
  56.         return (put_err(ERR_CD_ARG, NULL));
  57.     if (chdir(av[1]))
  58.         return (put_err(ERR_CD_DIR, av[1]));
  59.     return (0);
  60. }
  61.  
  62. int main(int argc, char **argv, char **env)
  63. {
  64.     int     arg_index;
  65.     int     next_semi;
  66.     int     cmd_index;
  67.     int     next_pipe;
  68.     int     pipefd[2];
  69.     pid_t   pid;
  70.     int     fd_in;
  71.  
  72.     arg_index = 1;
  73.     while (arg_index < argc)
  74.     {
  75.         next_semi = arg_index;
  76.         while (next_semi < argc && strncmp(argv[next_semi], ";", 2) != 0)
  77.             next_semi++;
  78.  
  79.         fd_in = 0;
  80.  
  81.         cmd_index = arg_index;
  82.         while (cmd_index < next_semi)
  83.         {
  84.             next_pipe = cmd_index;
  85.             while (next_pipe < next_semi && strncmp(argv[next_pipe], "|", 2) != 0)
  86.                 next_pipe++;
  87.            
  88.             char *cmds[next_pipe - cmd_index + 1];
  89.             sub(cmds, argv, cmd_index, next_pipe);
  90.            
  91.             pipe(pipefd);
  92.             if ((pid = fork()) == -1)
  93.                 return (put_err(ERR_FATAL, NULL));
  94.             else if (pid == 0)
  95.             {
  96.                 dup2(fd_in, 0);
  97.                 if (next_pipe < next_semi)
  98.                     dup2(pipefd[1], 1);
  99.                 close(pipefd[0]);
  100.                 if (strncmp(cmds[0], "cd", 3) == 0)
  101.                     cd(cmds, next_pipe - cmd_index);
  102.                 else if (execve(cmds[0], cmds, env) == -1)
  103.                 {
  104.                     close(pipefd[1]);
  105.                     close(fd_in);
  106.                     return (put_err(ERR_EXEC, cmds[0]));
  107.                 }
  108.                 close(pipefd[1]);
  109.                 close(fd_in);
  110.                 return (0);
  111.             }
  112.             else
  113.             {
  114.                 waitpid(pid, NULL, 0);
  115.                 close(pipefd[1]);
  116.                 if (fd_in)
  117.                     close(fd_in);
  118.                 fd_in = pipefd[0];
  119.             }
  120.             cmd_index = next_pipe + 1;
  121.         }
  122.         close(fd_in);
  123.         arg_index = next_semi + 1;
  124.     }
  125.     return (0);
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment