develooper Front page | perl.perl5.porters | Postings from March 2001

[PATCH] POSIX::getcwd()

Thread Next
From:
Benjamin Sugars
Date:
March 19, 2001 10:40
Subject:
[PATCH] POSIX::getcwd()
Message ID:
Pine.LNX.4.21.0103190029160.4686-100000@marmot.rim.canoe.ca

Hi Jarkko,

Here's a patch that lets POSIX::getcwd() actually call getcwd(3) if
available, rather than calling `pwd`.  This change has proved useful to me
when running in an environment where the (unexpected) fork was not
desirable. In the case where getcwd(3) is not found, it calls out to
Cwd::cwd() as before.

Patch is against perl@9214.

Cheers,
-Ben

-- 
signer: can't create ~/.sig: File too large

--- POSIX.pm.orig	Sun Mar 18 22:59:27 2001
+++ POSIX.pm	Sun Mar 18 23:00:08 2001
@@ -655,20 +655,6 @@
     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;
--- POSIX.xs.orig	Sun Mar 18 22:57:01 2001
+++ POSIX.xs	Sun Mar 18 22:58:45 2001
@@ -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
@@ -4016,3 +4020,39 @@
 ttyname(fd)
 	int		fd
 
+char *
+getcwd()
+	PPCODE:
+#ifdef HAS_GETCWD
+	char *		buf;
+	int		buflen = 128;
+	int		i;
+
+	New(0, buf, buflen, char);
+	while (getcwd(buf, buflen) == NULL) {
+	    buflen += 128;
+	    if (buflen > MAXPATHLEN) {
+		Safefree(buf);
+		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



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About