patch 8.2.0988: getting directory contents is always case sorted

Problem:    Getting directory contents is always case sorted.
Solution:   Add sort options and v:collate. (Christian Brabandt, closes #6229)
diff --git a/src/fileio.c b/src/fileio.c
index 7b60529..395e54b 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -35,6 +35,10 @@
 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags);
 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
 
+#ifdef FEAT_EVAL
+static int readdirex_sort;
+#endif
+
     void
 filemess(
     buf_T	*buf,
@@ -4645,7 +4649,23 @@
 
     name1 = dict_get_string(*(dict_T**)p1, (char_u*)"name", FALSE);
     name2 = dict_get_string(*(dict_T**)p2, (char_u*)"name", FALSE);
-    return STRCMP(name1, name2);
+    if (readdirex_sort == READDIR_SORT_BYTE)
+	return STRCMP(name1, name2);
+    else if (readdirex_sort == READDIR_SORT_IC)
+	return STRICMP(name1, name2);
+    else
+	return STRCOLL(name1, name2);
+}
+
+    static int
+compare_readdir_item(const void *s1, const void *s2)
+{
+    if (readdirex_sort == READDIR_SORT_BYTE)
+	return STRCMP(*(char **)s1, *(char **)s2);
+    else if (readdirex_sort == READDIR_SORT_IC)
+	return STRICMP(*(char **)s1, *(char **)s2);
+    else
+	return STRCOLL(*(char **)s1, *(char **)s2);
 }
 #endif
 
@@ -4663,7 +4683,8 @@
     char_u	*path,
     int		withattr UNUSED,
     void	*context,
-    int		(*checkitem)(void *context, void *item))
+    int		(*checkitem)(void *context, void *item),
+    int         sort)
 {
     int			failed = FALSE;
     char_u		*p;
@@ -4687,6 +4708,8 @@
 	else \
 	    vim_free(item); \
     } while (0)
+
+    readdirex_sort = READDIR_SORT_BYTE;
 # else
 #  define FREE_ITEM(item)   vim_free(item)
 # endif
@@ -4844,15 +4867,19 @@
 
 # undef FREE_ITEM
 
-    if (!failed && gap->ga_len > 0)
+    if (!failed && gap->ga_len > 0 && sort > READDIR_SORT_NONE)
     {
 # ifdef FEAT_EVAL
+	readdirex_sort = sort;
 	if (withattr)
 	    qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(dict_T*),
 		    compare_readdirex_item);
 	else
-# endif
+	    qsort((void*)gap->ga_data, (size_t)gap->ga_len, sizeof(char_u *),
+		    compare_readdir_item);
+# else
 	    sort_strings((char_u **)gap->ga_data, gap->ga_len);
+# endif
     }
 
     return failed ? FAIL : OK;
@@ -4883,7 +4910,7 @@
 	exp = vim_strsave(name);
 	if (exp == NULL)
 	    return -1;
-	if (readdir_core(&ga, exp, FALSE, NULL, NULL) == OK)
+	if (readdir_core(&ga, exp, FALSE, NULL, NULL, READDIR_SORT_NONE) == OK)
 	{
 	    for (i = 0; i < ga.ga_len; ++i)
 	    {