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

[PATCH] mkdir() mode argument is missing initial 0

Thread Next
From:
Abhijit Menon-Sen
Date:
July 5, 2001 04:22
Subject:
[PATCH] mkdir() mode argument is missing initial 0
Message ID:
20010705165015.A586@lustre.dyn.wiw.org
Various people have asked for mkdir() to warn when the mode specified is
a non-octal literal. The appended patch implements this behaviour (many
thanks to MJD for the idea).

(Robin: adding WARN_MKDIR caused ext/B/Deparse.t to fail, and I'm afraid
I didn't try hard enough to figure out why. Any ideas?)

- ams

--- current/warnings.pl~	Thu Jul  5 14:42:35 2001
+++ current/warnings.pl	Thu Jul  5 14:43:03 2001
@@ -48,6 +48,7 @@
        	'glob'		=> DEFAULT_OFF,
        	'y2k'		=> DEFAULT_OFF,
        	'chmod'		=> DEFAULT_OFF,
+       	'mkdir'		=> DEFAULT_OFF,
        	'umask'		=> DEFAULT_OFF,
        	'untie'		=> DEFAULT_OFF,
 	'substr'	=> DEFAULT_OFF,

--- current/opcode.pl~	Tue Jul  3 12:43:27 2001
+++ current/opcode.pl	Thu Jul  5 14:20:25 2001
@@ -773,7 +773,7 @@
 link		link			ck_fun		isT@	S S
 symlink		symlink			ck_fun		isT@	S S
 readlink	readlink		ck_fun		stu%	S?
-mkdir		mkdir			ck_fun		isT@	S S?
+mkdir		mkdir			ck_mkdir	isT@	S S?
 rmdir		rmdir			ck_fun		isTu%	S?
 
 # Directory calls.

--- current/op.h~	Thu Jul  5 14:07:35 2001
+++ current/op.h	Thu Jul  5 14:08:07 2001
@@ -166,6 +166,7 @@
 #define OPpTARGET_MY		16	/* Target is PADMY. */
 
 /* Private for OP_CONST */
+#define OPpCONST_OCTAL		4	/* Octal constant. */
 #define	OPpCONST_STRICT		8	/* bearword subject to strict 'subs' */
 #define OPpCONST_ENTERED	16	/* Has been entered as symbol. */
 #define OPpCONST_ARYBASE	32	/* Was a $[ translated to constant. */

--- current/op.c~	Thu Jul  5 14:21:52 2001
+++ current/op.c	Thu Jul  5 14:47:47 2001
@@ -6105,6 +6105,21 @@
 }
 
 OP *
+Perl_ck_mkdir(pTHX_ OP *o)
+{
+    if (ckWARN(WARN_MKDIR) && o->op_flags & OPf_KIDS) {
+	OP *kid = cBINOPo->op_last;
+	if (kid->op_type == OP_CONST
+	    && !(kid->op_private & OPpCONST_OCTAL))
+	{
+	    Perl_warner(aTHX_ WARN_MKDIR,
+			"mkdir() mode argument is missing initial 0");
+	}
+    }
+    return ck_fun(o);
+}
+
+OP *
 Perl_ck_null(pTHX_ OP *o)
 {
     return o;

--- current/toke.c~	Thu Jul  5 14:10:54 2001
+++ current/toke.c	Thu Jul  5 14:18:47 2001
@@ -6914,7 +6914,8 @@
     register char *e;			/* end of temp buffer */
     NV nv;				/* number read, as a double */
     SV *sv = Nullsv;			/* place to put the converted number */
-    bool floatit;			/* boolean: int or float? */
+    bool floatit,			/* boolean: int or float? */
+	octal = 0;			/* Is this an octal number? */
     char *lastub = 0;			/* position of last underbar */
     static char number_too_long[] = "Number too long";
 
@@ -6968,6 +6969,7 @@
 	    /* so it must be octal */
 	    else {
 		shift = 3;
+		octal = 1;
 		s++;
 	    }
 
@@ -7373,8 +7375,11 @@
 
     /* make the op for the constant and return */
 
-    if (sv)
+    if (sv) {
 	lvalp->opval = newSVOP(OP_CONST, 0, sv);
+	if (octal)
+	    ((SVOP*)lvalp->opval)->op_private |= OPpCONST_OCTAL;
+    }
     else
 	lvalp->opval = Nullop;
 
--- current/dump.c~	Mon Jul  2 19:56:41 2001
+++ current/dump.c	Thu Jul  5 14:26:43 2001
@@ -519,6 +519,8 @@
 	else if (o->op_type == OP_CONST) {
 	    if (o->op_private & OPpCONST_BARE)
 		sv_catpv(tmpsv, ",BARE");
+	    if (o->op_private & OPpCONST_OCTAL)
+		sv_catpv(tmpsv, ",OCTAL");
 	    if (o->op_private & OPpCONST_STRICT)
 		sv_catpv(tmpsv, ",STRICT");
 	    if (o->op_private & OPpCONST_ARYBASE)

--- current/pod/perldiag.pod~	Thu Jul  5 14:44:21 2001
+++ current/pod/perldiag.pod	Thu Jul  5 14:45:23 2001
@@ -1999,6 +1999,16 @@
 you omitted the name of the module.  Consult L<perlrun> for full details
 about C<-M> and C<-m>.
 
+=item mkdir() mode argument is missing initial 0
+
+(W mkdir) A novice will sometimes say
+
+    mkdir $dir, 777
+
+not realizing that 777 will be interpreted as a decimal number,
+equivalent to 01411.  Octal constants are introduced with a leading 0 in
+Perl, as in C.
+
 =item msg%s not implemented
 
 (F) You don't have System V message IPC on your system.

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