exec.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "sh.h"
  2. /*
  3. ** Renew HOME and PATH environnement variables before.
  4. ** Handle ~
  5. ** Execute builtins.
  6. ** Else, new fork process to execute executables.
  7. */
  8. int manage_cmd_1(t_e *e, char **cmd)
  9. {
  10. e->home = get_tenv(e, "HOME");
  11. cmd = change_tilde_to_home(e, cmd);
  12. ft_tabdel(e->path);
  13. e->path = ft_strsplit(get_tenv(e, "PATH"), ':');
  14. if (ft_strcmp(cmd[0], "cd") == 0)
  15. cd(e, &cmd[1]);
  16. else if (ft_strcmp(cmd[0], "setenv") == 0 || \
  17. ft_strcmp(cmd[0], "export") == 0)
  18. ft_setenv(e, cmd[1]);
  19. else if (ft_strcmp(cmd[0], "unsetenv") == 0)
  20. ft_unsetenv(e, cmd[1]);
  21. else if (ft_strcmp(cmd[0], "exit") == 0)
  22. exit(0);
  23. else
  24. return (0);
  25. return (1);
  26. }
  27. void manage_cmd_2(t_e *e, char **cmd)
  28. {
  29. char *pwd;
  30. if (ft_strcmp(cmd[0], "env") == 0 && cmd[1] == NULL)
  31. ft_puttab(e->env);
  32. else if (ft_strcmp(cmd[0], "pwd") == 0)
  33. {
  34. pwd = get_pwd();
  35. ft_putendl(pwd);
  36. ft_strdel(&pwd);
  37. }
  38. else
  39. execute(e, cmd);
  40. }
  41. /*
  42. ** Remove cotes on every blocks
  43. ** If local executable:
  44. ** Check file exist.
  45. ** Check user have executable rights on it.
  46. ** Execute it and exit fork.
  47. ** Else
  48. ** Execute direct given path like /bin/ls
  49. ** Search for executable in PATH paths.
  50. ** then run it.
  51. ** exit the fork if the no execution because execution stop the fork
  52. */
  53. void execute(t_e *e, char **cmd)
  54. {
  55. int i;
  56. i = -1;
  57. if (cmd[0][0] == '.' && cmd[0][1] == '/' && cmd[0][2] != '\0')
  58. {
  59. cmd[0] = &cmd[0][2];
  60. if (access(cmd[0], F_OK) == -1)
  61. error_message(cmd[0], "no such executable", "exit");
  62. else if (access(cmd[0], X_OK) == -1)
  63. error_message(cmd[0], "no rights to execute it", "exit");
  64. else
  65. execve(cmd[0], &cmd[0], e->env);
  66. }
  67. else
  68. {
  69. exec_if_perm_ok(e, cmd[0], cmd);
  70. if (e->path != NULL)
  71. while (e->path[++i] != NULL)
  72. exec_if_perm_ok(e, ft_strjoin(e->path[i],\
  73. ft_strjoin("/", cmd[0])), cmd);
  74. }
  75. error_message(cmd[0], "no such command", "exit");
  76. }
  77. /*
  78. ** Check permissions and executions rights on the file.
  79. ** If it's ok, execute it.
  80. ** Else: error message
  81. */
  82. void exec_if_perm_ok(t_e *e, char *path, char **cmd)
  83. {
  84. if (access(path, F_OK) != -1)
  85. {
  86. if (access(path, X_OK) != -1)
  87. execve(path, &cmd[0], e->env);
  88. else
  89. error_message(path, "no rights to execute it", "exit");
  90. }
  91. }
  92. /*
  93. ** Change ~ to HOME var
  94. ** Handle variable replacement
  95. */
  96. char **change_tilde_to_home(t_e *e, char **cmd)
  97. {
  98. int i;
  99. char *tmp;
  100. i = -1;
  101. while (++i < ft_tabsize(cmd))
  102. {
  103. if (cmd[i] && ft_strcmp(cmd[i], "~") == 0 && e->home != NULL)
  104. tmp = ft_strdup(e->home);
  105. else if (cmd[i] && cmd[i][0] == '~' && cmd[i][1] == '/' && \
  106. e->home != NULL)
  107. tmp = ft_strjoin(e->home, &cmd[i][1]);
  108. else if (cmd[i][0] == '$' && cmd[i][1] != '\0')
  109. tmp = ft_strdup(get_tenv(e, &cmd[i][1]));
  110. else
  111. tmp = ft_strdup(cmd[i]);
  112. ft_strdel(&cmd[i]);
  113. cmd[i] = tmp;
  114. }
  115. return (cmd);
  116. }