From: Benjamin Sugars Date: Mon, 19 Mar 2001 15:07:03 +0000 (-0500) Subject: Re: [PATCH] POSIX::getcwd() X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b5846a0b04f865340214f384842c67c721c12992;p=p5sagit%2Fp5-mst-13.2.git Re: [PATCH] POSIX::getcwd() Message-ID: p4raw-id: //depot/perl@9272 --- diff --git a/ext/POSIX/POSIX.pm b/ext/POSIX/POSIX.pm index 2ec44f8..918b2a0 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 b5fd3a9..6c5c70b 100644 --- a/ext/POSIX/POSIX.xs +++ b/ext/POSIX/POSIX.xs @@ -38,6 +38,10 @@ #include #endif +#ifdef I_UNISTD +#include +#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