diff -Naurp grep/src/grep.c grep_new/src/grep.c
--- grep/src/grep.c	2005-04-22 20:14:31.000000000 +0200
+++ grep_new/src/grep.c	2005-04-23 01:35:03.000000000 +0200
@@ -36,6 +36,10 @@
 # include <wchar.h>
 # include <wctype.h>
 #endif
+#ifdef HAVE_LANGINFO_CODESET
+# include <langinfo.h>
+#endif
+
 #include <stdio.h>
 #include "system.h"
 #include "getopt.h"
@@ -157,6 +161,9 @@ int match_icase;
 int match_words;
 int match_lines;
 unsigned char eolbyte;
+#ifdef MBS_SUPPORT
+int using_utf8;
+#endif
 
 /* For error messages. */
 /* The name the program was run with, stripped of any leading path. */
@@ -1317,6 +1324,29 @@ get_nondigit_option (int argc, char *con
   return opt;
 }
 
+#ifdef MBS_SUPPORT
+static int
+mb_check_utf8 (void)
+{
+  const char *str_utf8[] = { "UTF-8", "utf-8", "UTF8", "utf8", 0 };
+  int i;
+  char* str = 0;
+#ifdef HAVE_LANGINFO_CODESET
+  str = nl_langinfo (CODESET);
+#elif HAVE_SETLOCALE
+  str = setlocale (LC_CTYPE, 0);
+#endif
+  if (!str)
+    return 0;			/* langinfo nor locale, or error */
+
+  for (i = 0; str_utf8[i]; i++)
+    if (strstr (str, str_utf8[i]))
+      return 1;
+
+  return 0;
+}
+#endif /* MBS_SUPPORT */
+
 int
 main (int argc, char **argv)
 {
@@ -1387,6 +1417,9 @@ main (int argc, char **argv)
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 #endif
+#ifdef MBS_SUPPORT
+  using_utf8 = mb_check_utf8();
+#endif
 
   atexit (close_stdout);
 
diff -Naurp grep/src/grep.h grep_new/src/grep.h
--- grep/src/grep.h	2005-04-22 20:14:46.000000000 +0200
+++ grep_new/src/grep.h	2005-04-23 01:35:03.000000000 +0200
@@ -40,3 +40,7 @@ extern int match_icase;		/* -i */
 extern int match_words;		/* -w */
 extern int match_lines;		/* -x */
 extern unsigned char eolbyte;	/* -z */
+
+#ifdef MBS_SUPPORT
+extern int using_utf8;		/* utf8 optimization */
+#endif
diff -Naurp grep/src/search.c grep_new/src/search.c
--- grep/src/search.c	2005-04-22 20:14:40.000000000 +0200
+++ grep_new/src/search.c	2005-04-23 01:35:03.000000000 +0200
@@ -666,10 +666,9 @@ Pcompile (char const *pattern, size_t si
 #else
   int e;
   char const *ep;
-  char *re = xmalloc (4 * size + 7);
-  int flags = PCRE_MULTILINE | (match_icase ? PCRE_CASELESS : 0);
+  char *re, *n;
+  int flags;
   char const *patlim = pattern + size;
-  char *n = re;
   char const *p;
   char const *pnul;
 
@@ -677,6 +676,22 @@ Pcompile (char const *pattern, size_t si
   if (eolbyte != '\n')
     error (2, 0, _("The -P and -z options cannot be combined"));
 
+  flags = PCRE_MULTILINE | 
+    (match_icase ? PCRE_CASELESS : 0);
+
+#ifdef MBS_SUPPORT
+  /* note: PCRE must be configured to work under UTF-8 */
+  if (using_utf8)
+    {
+      /* PCRE_CASELESS does not work with PCRE_UTF8 */
+      if (match_icase)
+	error (0, 0, _("warning: %s: %s\n"), "PCRE", 
+	       _("-i does not work with PCRE under UTF-8"));
+      flags |= PCRE_UTF8;
+    }
+#endif /* MBS_SUPPORT */
+
+  n = re = xmalloc (4 * size + 7);
   *n = '\0';
   if (match_lines)
     strcpy (n, "^(");
