Browse Source

[fix] could not execute absolute given file when PATH is unset:
- rework execute() function:
- seperate absolute path execution from relative one.
- remove useless variable.
- add comment on exec_if_perm_ok() and rename it.
- unit test:
- add test without PATH var env.
- reorganize execution ones.

Moul 4 years ago
parent
commit
26923b2405
3 changed files with 20 additions and 13 deletions
  1. 14 10
      src/exec.c
  2. 1 1
      src/sh.h
  3. 5 2
      tests.sh

+ 14 - 10
src/exec.c

@@ -52,8 +52,8 @@ void	manage_cmd_2(char **cmd)
 void	execute(char **cmd)
 {
 	int		i;
-	char	*path;
 
+	i = -1;
 	if (cmd[0][0] == '.' && cmd[0][1] == '/' && cmd[0][2] != '\0')
 	{
 		cmd[0] = ft_strdup(&cmd[0][2]);
@@ -64,20 +64,24 @@ void	execute(char **cmd)
 		else
 			execve(cmd[0], &cmd[0], g_tenv);
 	}
-	else if (g_path != NULL)
+	else
 	{
-		i = -1;
-		while (g_path[++i] != NULL)
-		{
-			path = ft_strjoin(g_path[i], ft_strjoin("/", cmd[0]));
-			check_file_exist_exec_rights(path, cmd);
-			check_file_exist_exec_rights(cmd[0], cmd);
-		}
+		exec_if_perm_ok(cmd[0], cmd);
+		if (g_path != NULL)
+			while (g_path[++i] != NULL)
+				exec_if_perm_ok(ft_strjoin(g_path[i],\
+				ft_strjoin("/", cmd[0])), cmd);
 	}
 	error_message(cmd[0], "no such command", "exit");
 }
 
-void	check_file_exist_exec_rights(char *path, char **cmd)
+/*
+** Check permissions and executions rights on the file.
+** If it's ok, execute it.
+** Else: error message
+*/
+
+void	exec_if_perm_ok(char *path, char **cmd)
 {
 	if (access(path, F_OK) != -1)
 	{

+ 1 - 1
src/sh.h

@@ -84,7 +84,7 @@ char	**change_tilde_to_home(char **cmd);
 int		manage_cmd_1(char **cmdt);
 void	manage_cmd_2(char **cmdt);
 void	execute(char **cmdt);
-void	check_file_exist_exec_rights(char *path, char **cmd);
+void	exec_if_perm_ok(char *path, char **cmd);
 /*
 ** env.c
 */

+ 5 - 2
tests.sh

@@ -7,12 +7,15 @@ tests=(
 "cat auteur" # execution simple
 "ls -l" # avec option
 "ls ~" # avec tilde
+"unsetenv PATH; ls;/bin/ls" # execution ls et /bin/ls sans PATH
 "/bin/ls" # execution avec le chemin entier
-"/sbin/yubikey_shell" # execution sans les droits d’execution
-"yubikey_shell" # 
 "naritse" # cmd, élément qui n’existe pas
 "./21sh -c ls" # execution avec './'
 "./anrsiute" # execution élément qui n’existe pas
+
+### permissions
+"/sbin/yubikey_shell" # execution sans les droits d’execution
+"yubikey_shell" #
 "touch $tmpf ; chmod 000 $tmpf ; ./$tmpf" # execution toto sans les droits avec './'
 "$tmpf" # execution toto sans les droits avec le path entier
 "rm -f $tmpf"