Advertisement
SamPolyhedron

exam_rank_04

Jul 7th, 2022
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4.  
  5. #define F "error: fatal"
  6. #define BAD "error: cd: bad arguments"
  7. #define DIR "error: cd: cannot change directory to "
  8. #define EXE "error: cannot execute "
  9.  
  10. typedef struct s_list
  11. {
  12.     char    **av;
  13.     int     pipe_fd[2];
  14.     int     prev_pipe;
  15.     int     prev_type;
  16.     int     type; } t_list;
  17.  
  18. int ft_strlen(char *s)
  19. {
  20.     int i = 0;
  21.    
  22.     while (s && s[i])
  23.         i++;
  24.     return (i);
  25. }
  26.  
  27. void    msg(char *str, t_list *list, int k, int f)
  28. {
  29.     write(2, str, ft_strlen(str));
  30.     if (k)
  31.         write(2, list->av[0], ft_strlen(list->av[0]));
  32.     write(2, "\n", 1);
  33.     if (f)
  34.         exit(1);
  35. }
  36.  
  37. int init_list(t_list *list, char **av, int i)
  38. {
  39.     int end = i;
  40.    
  41.     while (av[end] && strcmp(av[end], "|") != 0 && strcmp(av[end], ";") != 0)
  42.         end++;
  43.     if ((end - i) > 0)
  44.     {
  45.         list->av = &av[i];
  46.         list->prev_type = list->type;
  47.         if (!av[end])
  48.             list->type = 0;
  49.         else if
  50.             (strcmp(av[end], "|") == 0) list->type = 1;
  51.         else
  52.             list->type = 2;
  53.         av[end] = NULL;
  54.         list->prev_pipe = list->pipe_fd[0];
  55.     }
  56.     return (end);
  57. }
  58.  
  59. void    fork_process(t_list *list, char **env)
  60. {
  61.     if (list->type == 1 && (dup2(list->pipe_fd[1], 1) < 0))
  62.         msg(F, list, 0, 1);
  63.     if (list->prev_type == 1 && (dup2(list->prev_pipe, 0) < 0))
  64.         msg(F, list, 0, 1);
  65.     if (execve(list->av[0], list->av, env) < 0)
  66.         msg(EXE, list, 1, 1);
  67.     //exit(0); Если не примет, то добавить
  68. }
  69.  
  70. void    exe(t_list *list, char **env)
  71. {
  72.     pid_t   pid;
  73.    
  74.     if ((list->type == 1 || list->prev_type == 1) && pipe(list->pipe_fd))
  75.         msg(F, list, 0, 1);
  76.     if ((pid = fork()) < 0)
  77.         msg(F, list, 0, 1);
  78.     if (!pid)
  79.         fork_process(list, env);
  80.     else
  81.     {
  82.         waitpid(pid, NULL, 0);
  83.         if (list->type == 1 || list->prev_type == 1)
  84.             close(list->pipe_fd[1]);
  85.         if (list->type != 1)
  86.             close(list->pipe_fd[0]);
  87.         if (list->prev_type == 1)
  88.             close(list->prev_pipe);
  89.     }
  90. }
  91.  
  92. int main(int argc, char **argv, char **env)
  93. {
  94.     t_list  list;
  95.     int     start, i = 0;
  96.  
  97.     while (i < argc && argv[++i])
  98.     {
  99.         start = i;
  100.         i = init_list(&list, argv, i);
  101.         if (strcmp(argv[start], "cd") == 0)
  102.         {
  103.             if (i - start != 2)
  104.                 msg(BAD, &list, 0, 0);
  105.             else if (chdir(list.av[1]) < 0)
  106.                 msg(DIR, &list, 1, 0);
  107.         }
  108.         else if (i > start)
  109.             exe(&list, env);
  110.     }
  111.     return (0);
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement