Index: stream/stream_file.c
===================================================================
--- stream/stream_file.c	(revision 36298)
+++ stream/stream_file.c	(working copy)
@@ -36,6 +36,7 @@
 #include "help_mp.h"
 #include "m_option.h"
 #include "m_struct.h"
+#include "osdep/osdep.h"

 static struct stream_priv_s {
   char* filename;
Index: sub/vobsub.c
===================================================================
--- sub/vobsub.c	(revision 36298)
+++ sub/vobsub.c	(working copy)
@@ -42,6 +42,7 @@
 #include "unrar_exec.h"
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "osdep/osdep.h"

 // Record the original -vobsubid set by commandline, since vobsub_id will be
 // overridden if slang match any of vobsub streams.
Index: sub/subreader.c
===================================================================
--- sub/subreader.c	(revision 36298)
+++ sub/subreader.c	(working copy)
@@ -42,6 +42,7 @@
 #include "stream/stream.h"
 #include "libavutil/common.h"
 #include "libavutil/avstring.h"
+#include "osdep/osdep.h"

 #ifdef CONFIG_ENCA
 #include <enca.h>
Index: configure
===================================================================
--- configure	(revision 36298)
+++ configure	(working copy)
@@ -855,6 +855,7 @@
 _priority=no
 def_dos_paths="#define HAVE_DOS_PATHS 0"
 def_stream_cache="#define CONFIG_STREAM_CACHE 1"
+def_path_max_check="#define CONFIG_PATH_MAX_CHECK 0"
 def_priority="#undef CONFIG_PRIORITY"
 def_pthread_cache="#undef PTHREAD_CACHE"
 shmem=no
@@ -1780,6 +1781,7 @@
   _priority=yes
   def_dos_paths="#define HAVE_DOS_PATHS 1"
   def_priority="#define CONFIG_PRIORITY 1"
+  def_path_max_check="#define CONFIG_PATH_MAX_CHECK 1"
 fi

 if wine ; then
@@ -8724,6 +8726,7 @@
 $def_macosx_finder
 $def_maemo
 $def_memalign_hack
+$def_path_max_check
 $def_priority
 $def_quicktime
 $def_restrict_keyword
Index: osdep/osdep.h
===================================================================
--- osdep/osdep.h	(revision 36298)
+++ osdep/osdep.h	(working copy)
@@ -23,6 +23,28 @@
 #ifndef MPLAYER_OSDEP_H
 #define MPLAYER_OSDEP_H

+#include "config.h"
+
+#if CONFIG_PATH_MAX_CHECK
+#include <stdio.h>      /* fopen()   */
+#include <dirent.h>     /* opendir() */
+#include <io.h>         /* open()    */
+#include <fcntl.h>      /* open()    */
+#include <string.h>     /* strlen()  */
+#include <limits.h>     /* PATH_MAX  */
+#include <errno.h>      /* errno     */
+
+#define fopen(n, m) \
+    (strlen(n) >= PATH_MAX ? (errno = ENAMETOOLONG, NULL) : (fopen)(n, m))
+
+#define opendir(n) \
+    (strlen(n) >= PATH_MAX ? (errno = ENOENT, NULL) : (opendir)(n))
+
+#define open(n, ...) \
+    (strlen(n) >= PATH_MAX ? (errno = ENAMETOOLONG, -1) : \
+                             (open)(n, __VA_ARGS__))
+#endif /* CONFIG_PATH_MAX_CHECK */
+
 #ifdef __OS2__
 #define INCL_DOS
 #define INCL_DOSDEVIOCTL

