summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorBenjamin Sugars <bsugars@canoe.ca>2001-03-19 10:07:03 -0500
committerJarkko Hietaniemi <jhi@iki.fi>2001-03-21 01:33:17 +0000
commitb5846a0b04f865340214f384842c67c721c12992 (patch)
tree4205ba85dacf75e79773e939e4819f962ba8c3a7 /ext
parent902bacac8f903013ef6aefa9890e90468ac9406c (diff)
downloadperl-b5846a0b04f865340214f384842c67c721c12992.tar.gz
Re: [PATCH] POSIX::getcwd()
Message-ID: <Pine.LNX.4.21.0103191454500.1820-100000@marmot.rim.canoe.ca> p4raw-id: //depot/perl@9272
Diffstat (limited to 'ext')
-rw-r--r--ext/POSIX/POSIX.pm14
-rw-r--r--ext/POSIX/POSIX.xs46
2 files changed, 46 insertions, 14 deletions
diff --git a/ext/POSIX/POSIX.pm b/ext/POSIX/POSIX.pm
index 2ec44f8846..918b2a0c6b 100644
--- a/ext/POSIX/POSIX.pm
+++ b/ext/POSIX/POSIX.pm
@@ -655,20 +655,6 @@ sub fork {
CORE::fork;
}
-sub getcwd
-{
- usage "getcwd()" if @_ != 0;
- if ($^O eq 'MSWin32') {
- # this perhaps applies to everyone else also?
- require Cwd;
- $cwd = &Cwd::cwd;
- }
- else {
- chop($cwd = `pwd`);
- }
- $cwd;
-}
-
sub getegid {
usage "getegid()" if @_ != 0;
$) + 0;
diff --git a/ext/POSIX/POSIX.xs b/ext/POSIX/POSIX.xs
index b5fd3a995b..6c5c70b77c 100644
--- a/ext/POSIX/POSIX.xs
+++ b/ext/POSIX/POSIX.xs
@@ -38,6 +38,10 @@
#include <stddef.h>
#endif
+#ifdef I_UNISTD
+#include <unistd.h>
+#endif
+
/* XXX This comment is just to make I_TERMIO and I_SGTTY visible to
metaconfig for future extension writers. We don't use them in POSIX.
(This is really sneaky :-) --AD
@@ -4014,3 +4018,45 @@ char *
ttyname(fd)
int fd
+char *
+getcwd()
+ PPCODE:
+#ifdef HAS_GETCWD
+ char * buf;
+ int buflen = 128;
+ int i;
+
+ New(0, buf, buflen, char);
+ /* Many getcwd()s know how to automatically allocate memory
+ * for the directory if the buffer argument is NULL but...
+ * (1) we cannot assume all getcwd()s do that
+ * (2) this may interfere with Perl's malloc
+ * So let's not. --jhi */
+ while ((getcwd(buf, buflen) == NULL) && errno == ERANGE) {
+ buflen += 128;
+ if (buflen > MAXPATHLEN) {
+ Safefree(buf);
+ buf = NULL;
+ break;
+ }
+ Renew(buf, buflen, char);
+ }
+ if (buf) {
+ PUSHs(sv_2mortal(newSVpv(buf, 0)));
+ Safefree(buf);
+ }
+ else
+ PUSHs(&PL_sv_undef);
+#else
+ dSP;
+ require_pv("Cwd.pm");
+
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(sp);
+ PUTBACK;
+ call_pv("Cwd::cwd", GIMME_V);
+ FREETMPS;
+ LEAVE;
+ XSRETURN(1);
+#endif