develooper Front page | perl.perl5.porters | Postings from July 2005

fchmod, fchown, fchdir

Thread Next
From:
Gisle Aas
Date:
July 14, 2005 15:27
Subject:
fchmod, fchown, fchdir
Message ID:
lrbr55f2h3.fsf@caliper.activestate.com
I would like to be able to pass file handles to chmod(), chown() and
chdir() and have perl call the corresponding fxxx() function.  This is
similar to how perl treats the argument of stat() or truncate().

Below is an implementation that make perl call fchmod().  If there are
no objections to this approach, I'll complete a patch that includes
the other 2 functions and comes with tests and documentation.
Configure already probes for the availability of these functions.


--- doio.c.orig	2005-07-14 23:52:24.000000000 +0200
+++ doio.c	2005-07-15 00:06:48.000000000 +0200
@@ -1677,10 +1677,33 @@
 	    APPLY_TAINT_PROPER();
 	    tot = sp - mark;
 	    while (++mark <= sp) {
-		const char *name = SvPV_nolen_const(*mark);
-		APPLY_TAINT_PROPER();
-		if (PerlLIO_chmod(name, val))
-		    tot--;
+                GV* gv;
+                if (SvTYPE(*mark) == SVt_PVGV) {
+                    gv = (GV*)*mark;
+		do_fchmod:
+#ifdef HAS_FCHMOD
+		    if (GvIO(gv) && IoIFP(GvIOp(gv))) {
+			APPLY_TAINT_PROPER();
+			if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
+			    tot--;
+#else
+			DIE(aTHX_ PL_no_func, "fchmod");
+#endif
+		    }
+		    else {
+			tot--;
+		    }
+		}
+		else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
+		    gv = (GV*)SvRV(*mark);
+		    goto do_fchmod;
+		}
+		else {
+		    const char *name = SvPV_nolen_const(*mark);
+		    APPLY_TAINT_PROPER();
+		    if (PerlLIO_chmod(name, val))
+			tot--;
+		}
 	    }
 	}
 	break;

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