@@ -5147,7 +5147,7 @@ expand_shellcmd(
51475147{
51485148 char_u * pat ;
51495149 int i ;
5150- char_u * path ;
5150+ char_u * path = NULL ;
51515151 int mustfree = FALSE;
51525152 garray_T ga ;
51535153 char_u * buf = alloc (MAXPATHL );
@@ -5156,6 +5156,9 @@ expand_shellcmd(
51565156 int flags = flagsarg ;
51575157 int ret ;
51585158 int did_curdir = FALSE;
5159+ hashtab_T found_ht ;
5160+ hashitem_T * hi ;
5161+ hash_T hash ;
51595162
51605163 if (buf == NULL )
51615164 return FAIL ;
@@ -5169,15 +5172,14 @@ expand_shellcmd(
51695172
51705173 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD ;
51715174
5172- /* For an absolute name we don't use $PATH. */
5173- if (mch_isFullName (pat ))
5174- path = (char_u * )" " ;
5175- else if ((pat [0 ] == '.' && (vim_ispathsep (pat [1 ])
5176- || (pat [1 ] == '.' && vim_ispathsep (pat [2 ])))))
5175+ if (pat [0 ] == '.' && (vim_ispathsep (pat [1 ])
5176+ || (pat [1 ] == '.' && vim_ispathsep (pat [2 ]))))
51775177 path = (char_u * )"." ;
51785178 else
51795179 {
5180- path = vim_getenv ((char_u * )"PATH" , & mustfree );
5180+ /* For an absolute name we don't use $PATH. */
5181+ if (!mch_isFullName (pat ))
5182+ path = vim_getenv ((char_u * )"PATH" , & mustfree );
51815183 if (path == NULL )
51825184 path = (char_u * )"" ;
51835185 }
@@ -5188,6 +5190,7 @@ expand_shellcmd(
51885190 * current directory, to find "subdir/cmd".
51895191 */
51905192 ga_init2 (& ga , (int )sizeof (char * ), 10 );
5193+ hash_init (& found_ht );
51915194 for (s = path ; ; s = e )
51925195 {
51935196 if (* s == NUL )
@@ -5200,9 +5203,6 @@ expand_shellcmd(
52005203 else if (* s == '.' )
52015204 did_curdir = TRUE;
52025205
5203- if (* s == ' ' )
5204- ++ s ; /* Skip space used for absolute path name. */
5205-
52065206#if defined(MSWIN )
52075207 e = vim_strchr (s , ';' );
52085208#else
@@ -5229,15 +5229,23 @@ expand_shellcmd(
52295229 {
52305230 for (i = 0 ; i < * num_file ; ++ i )
52315231 {
5232- s = (* file )[i ];
5233- if (STRLEN (s ) > l )
5232+ char_u * name = (* file )[i ];
5233+
5234+ if (STRLEN (name ) > l )
52345235 {
5235- /* Remove the path again. */
5236- STRMOVE (s , s + l );
5237- ((char_u * * )ga .ga_data )[ga .ga_len ++ ] = s ;
5236+ // Check if this name was already found.
5237+ hash = hash_hash (name + l );
5238+ hi = hash_lookup (& found_ht , name + l , hash );
5239+ if (HASHITEM_EMPTY (hi ))
5240+ {
5241+ // Remove the path that was prepended.
5242+ STRMOVE (name , name + l );
5243+ ((char_u * * )ga .ga_data )[ga .ga_len ++ ] = name ;
5244+ hash_add_item (& found_ht , hi , name , hash );
5245+ name = NULL ;
5246+ }
52385247 }
5239- else
5240- vim_free (s );
5248+ vim_free (name );
52415249 }
52425250 vim_free (* file );
52435251 }
@@ -5252,6 +5260,7 @@ expand_shellcmd(
52525260 vim_free (pat );
52535261 if (mustfree )
52545262 vim_free (path );
5263+ hash_clear (& found_ht );
52555264 return OK ;
52565265}
52575266
0 commit comments