pipex.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* pipex.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: phella <phella@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2022/03/01 12:24:44 by phella #+# #+# */
  9. /* Updated: 2022/03/03 19:24:11 by phella ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "pipex.h"
  13. char *ft_path(char *cmd, char **env)
  14. {
  15. int i;
  16. char **all_paths;
  17. char *piece_path;
  18. char *res;
  19. i = 0;
  20. if (env == NULL)
  21. exit (1);
  22. while (env[i] && ft_strnstr(env[i], "PATH=", 5) == 0)
  23. i++;
  24. if (env[i] == NULL)
  25. return (0);
  26. all_paths = ft_split(env[i] + 5, ':');
  27. i = -1;
  28. while (all_paths[++i])
  29. {
  30. piece_path = ft_strjoin(all_paths[i], "/");
  31. res = ft_strjoin(piece_path, cmd);
  32. if (access(res, F_OK) == 0)
  33. return (res);
  34. else
  35. free(res);
  36. }
  37. return (0);
  38. }
  39. char *ft_pwd_path(char *cmd, char **env)
  40. {
  41. int i;
  42. char *pwd_path;
  43. char *res;
  44. int j;
  45. i = 0;
  46. j = -1;
  47. if (env == NULL)
  48. exit (1);
  49. while (env[i] && ft_strnstr(env[i], "PWD=", 4) == 0)
  50. i++;
  51. if (env[i] == NULL)
  52. exit(0);
  53. pwd_path = (char *)malloc((ft_strlen(env[i]) - 4) * sizeof(char));
  54. while (++j < ft_strlen(env[i]) - 4)
  55. pwd_path[j] = env[i][j + 4];
  56. pwd_path = ft_strjoin(pwd_path, "/");
  57. res = ft_strjoin(pwd_path, cmd);
  58. if (access(res, F_OK) == 0)
  59. return (res);
  60. else
  61. free(res);
  62. exit(0);
  63. }
  64. void ft_get_cmd(char *argv, char **env)
  65. {
  66. char **cmd;
  67. char **cmd2;
  68. char *path;
  69. int i;
  70. i = 0;
  71. cmd2 = ft_split(argv, ' ');
  72. cmd = ft_split(argv, ' ');
  73. if (access(cmd[0], X_OK) == 0)
  74. {
  75. cmd = ft_split(argv, '/');
  76. while (cmd[i])
  77. i++;
  78. cmd = ft_split(cmd[i - 1], ' ');
  79. execve(cmd2[0], cmd, env);
  80. }
  81. path = ft_path(cmd[0], env);
  82. if (path == NULL)
  83. path = ft_pwd_path(cmd[0], env);
  84. if (execve(path, cmd, env) == -1)
  85. {
  86. perror("Command error");
  87. exit(1);
  88. }
  89. }
  90. void ft_parent_process(char **argv, char **env, int *fd)
  91. {
  92. int fd_out;
  93. fd_out = open(argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  94. if (fd_out == -1)
  95. {
  96. write(2, "Permission denied error\n", 24);
  97. exit(1);
  98. }
  99. dup2(fd[0], 0);
  100. dup2(fd_out, 1);
  101. close(fd[1]);
  102. ft_get_cmd(argv[3], env);
  103. }
  104. void ft_child_process(char **argv, char **env, int *fd)
  105. {
  106. if (access(argv[1], F_OK) != 0)
  107. {
  108. write(2, "No such file or directory error\n", 32);
  109. exit(1);
  110. }
  111. if (access(argv[1], R_OK) != 0)
  112. {
  113. write(2, "Permission denied error\n", 25);
  114. exit(1);
  115. }
  116. dup2(fd[1], 1);
  117. dup2(open(argv[1], O_RDONLY), 0);
  118. close(fd[0]);
  119. ft_get_cmd(argv[2], env);
  120. }