Skip to content

Instantly share code, notes, and snippets.

@kechako
Created July 11, 2014 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kechako/92cd749422fcc2475231 to your computer and use it in GitHub Desktop.
Save kechako/92cd749422fcc2475231 to your computer and use it in GitHub Desktop.
zsh 5.0.5 を OS X の NFD に対応させるパッチ
diff --git a/Src/utils.c b/Src/utils.c
index c6d178c..e979cb2 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -4256,6 +4256,12 @@ mod_export char *
zreaddir(DIR *dir, int ignoredots)
{
struct dirent *de;
+#if defined(HAVE_ICONV) && defined(__APPLE__)
+ static iconv_t conv_ds = (iconv_t)0;
+ static char *conv_name = 0;
+ char *conv_name_ptr, *orig_name_ptr;
+ size_t conv_name_len, orig_name_len;
+#endif
do {
de = readdir(dir);
@@ -4264,6 +4270,30 @@ zreaddir(DIR *dir, int ignoredots)
} while(ignoredots && de->d_name[0] == '.' &&
(!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])));
+#if defined(HAVE_ICONV) && defined(__APPLE__)
+ if (!conv_ds)
+ conv_ds = iconv_open("UTF-8", "UTF-8-MAC");
+ if (conv_ds != (iconv_t)(-1)) {
+ /* Force initial state in case re-using conv_ds */
+ (void) iconv(conv_ds, 0, &orig_name_len, 0, &conv_name_len);
+
+ orig_name_ptr = de->d_name;
+ orig_name_len = strlen(de->d_name);
+ conv_name = zrealloc(conv_name, orig_name_len+1);
+ conv_name_ptr = conv_name;
+ conv_name_len = orig_name_len;
+ if (iconv(conv_ds,
+ &orig_name_ptr, &orig_name_len,
+ &conv_name_ptr, &conv_name_len) != (size_t)(-1) &&
+ orig_name_len == 0) {
+ /* Completely converted, metafy and return */
+ *conv_name_ptr = '\0';
+ return metafy(conv_name, -1, META_STATIC);
+ }
+ /* Error, or conversion incomplete, keep the original name */
+ }
+#endif
+
return metafy(de->d_name, -1, META_STATIC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment