summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorFacundo Batista <facundobatista@gmail.com>2008-06-22 13:36:20 (GMT)
committerFacundo Batista <facundobatista@gmail.com>2008-06-22 13:36:20 (GMT)
commit5596b0cfc275a649f17a97c881d094a949adad6d (patch)
tree399842aa82a490adc495887f2683833de625af99 /Modules
parent0ba92b24b797c00038b9e2c50d132ffdcdd38959 (diff)
downloadcpython-5596b0cfc275a649f17a97c881d094a949adad6d.zip
cpython-5596b0cfc275a649f17a97c881d094a949adad6d.tar.gz
cpython-5596b0cfc275a649f17a97c881d094a949adad6d.tar.bz2
Issue #2722. Now the char buffer to support the path string has
not fixed length, it mallocs memory if needed. As a result, we don't have a maximum for the getcwd() method.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/posixmodule.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index a4bbac5..8f32fd4 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1956,19 +1956,38 @@ Return a string representing the current working directory.");
static PyObject *
posix_getcwd(PyObject *self, PyObject *noargs)
{
- char buf[1026];
- char *res;
+ int bufsize_incr = 1024;
+ int bufsize = 0;
+ char *tmpbuf = NULL;
+ char *res = NULL;
+ PyObject *dynamic_return;
Py_BEGIN_ALLOW_THREADS
+ do {
+ bufsize = bufsize + bufsize_incr;
+ tmpbuf = malloc(bufsize);
+ if (tmpbuf == NULL) {
+ break;
+ }
#if defined(PYOS_OS2) && defined(PYCC_GCC)
- res = _getcwd2(buf, sizeof buf);
+ res = _getcwd2(tmpbuf, bufsize);
#else
- res = getcwd(buf, sizeof buf);
+ res = getcwd(tmpbuf, bufsize);
#endif
+
+ if (res == NULL) {
+ free(tmpbuf);
+ }
+ } while ((res == NULL) && (errno == ERANGE));
Py_END_ALLOW_THREADS
+
if (res == NULL)
return posix_error();
- return PyString_FromString(buf);
+
+ dynamic_return = PyString_FromString(tmpbuf);
+ free(tmpbuf);
+
+ return dynamic_return;
}
#ifdef Py_USING_UNICODE