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/open.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/fopen.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,8 @@
 #include "stream/stream.h"
 #include "libavutil/common.h"
 #include "libavutil/avstring.h"
+#include "osdep/fopen.h"
+#include "osdep/opendir.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/open.h
===================================================================
--- osdep/open.h	(revision 0)
+++ osdep/open.h	(revision 0)
@@ -0,0 +1,68 @@
+/*
+ * open() replacement for systems that have buggy one in libc
+ *
+ * Copyright (c) 2013 by KO Myung-Hun (komh@chollian.net)
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_OPEN_H
+#define MPLAYER_OPEN_H
+
+#include "config.h"
+
+#if CONFIG_PATH_MAX_CHECK
+#include <io.h>
+#include <fcntl.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+#include <stdarg.h>
+
+static inline int mplayer_open(const char *name, int oflag, ...)
+{
+    va_list       arg_ptr;
+    int           ret;
+
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENAMETOOLONG;
+        return -1;
+    }
+
+    va_start(arg_ptr, oflag);
+
+    if ((oflag & (O_CREAT | O_SIZE)) == (O_CREAT | O_SIZE)) {
+        int           pmode = va_arg(arg_ptr, int);
+        unsigned long isize = va_arg(arg_ptr, unsigned long);
+        ret = open(name, oflag, pmode, isize);
+    } else if((oflag & (O_CREAT | O_SIZE)) == O_CREAT) {
+        ret = open(name, oflag, va_arg(arg_ptr, int));
+    } else if((oflag & (O_CREAT | O_SIZE)) == O_SIZE) {
+        ret = open(name, oflag, va_arg(arg_ptr, unsigned long));
+    } else {
+        ret = open(name, oflag);
+    }
+
+    va_end(arg_ptr);
+
+    return ret;
+}
+
+#define open mplayer_open
+#endif /* CONFIG_PATH_MAX_CHECK */
+
+#endif /* MPLAYER_OPEN_H */
Index: osdep/opendir.h
===================================================================
--- osdep/opendir.h	(revision 0)
+++ osdep/opendir.h	(revision 0)
@@ -0,0 +1,47 @@
+/*
+ * opendir() replacement for systems that have buggy one in libc
+ *
+ * Copyright (c) 2013 by KO Myung-Hun (komh@chollian.net)
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_OPENDIR_H
+#define MPLAYER_OPENDIR_H
+
+#include "config.h"
+
+#if CONFIG_PATH_MAX_CHECK
+#include <dirent.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+
+static inline DIR *mplayer_opendir(const char *name)
+{
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENOENT;
+        return NULL;
+    }
+
+    return opendir(name);
+}
+
+#define opendir mplayer_opendir
+#endif /* CONFIG_PATH_MAX_CHECK */
+
+#endif /* MPLAYER_OPENDIR_H */
Index: osdep/fopen.h
===================================================================
--- osdep/fopen.h	(revision 0)
+++ osdep/fopen.h	(revision 0)
@@ -0,0 +1,47 @@
+/*
+ * fopen() replacement for systems that have buggy one in libc
+ *
+ * Copyright (c) 2013 by KO Myung-Hun (komh@chollian.net)
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_FOPEN_H
+#define MPLAYER_FOPEN_H
+
+#include "config.h"
+
+#if CONFIG_PATH_MAX_CHECK
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+
+static inline FILE *mplayer_fopen(const char *name, const char *mode)
+{
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENAMETOOLONG;
+        return NULL;
+    }
+
+    return fopen(name, mode);
+}
+
+#define fopen mplayer_fopen
+#endif /* CONFIG_PATH_MAX_CHECK */
+
+#endif /* MPLAYER_FOPEN_H */

