Re: octal file permissions
Zentara == Zentara [EMAIL PROTECTED] writes: Zentara After putting on the dunce cap and typing 100 times: Zentara the oct function takes a decimal value and returns octal, Zentara and (stat file)[2] returns a decimal value I finally see it. Zentara I think I do anyways. :-) No, it doesn't. The oct function interprets a number rendered as octal string, and returns a *number*. stat returns a *number*. There's nothing inherently decimal about either, and I think that's where the confusion results. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
Michael == Michael Fowler [EMAIL PROTECTED] writes: Michael Heh, you still have that backwards. oct() interprets its argument as an Michael octal value, and returns its decimal equivalent. Interesting that you use decimal here... Michael Also, it's best not to think of functions returning numbers in a specific Michael base. Functions simply return numbers, its when you output them that they Michael have to be interpreted in a certain base. ... when you correct it here. Thank goodness. :) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
Tanton == Tanton Gibbs [EMAIL PROTECTED] writes: Tanton To change from decimal to octal, you can use sprintf with a %o format string Tanton my $val = sprintf( %o, 8 ); Tanton print $val; Tanton 10 No, this changes from a *number*, which you have typically typed in decimal, to an octal string representation. Compare yours with this: my $val = sprintf %o, 010; There. It's converting octal to octal, using your incorrect language! 013 is a *number*, exactly the same number as 11 or 0xB or 0b1011, using the Perl numeric literals. None of them are decimal. The act of rendering a number to text defaults to decimal strings, hence your (common) confusion. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
On Wed, 13 Feb 2002 15:14:39 -0900, [EMAIL PROTECTED] (Michael Fowler) wrote: You have it backwards. Octal 100500 is 33088 in decimal. You just did the conversion with the oct operator. You're confused about what oct does; it returns the decimal representation of an octal value. Also, Perl is going to trip you up here; the decimal After putting on the dunce cap and typing 100 times: the oct function takes a decimal value and returns octal, and (stat file)[2] returns a decimal value I finally see it. I think I do anyways. :-) #!/usr/bin/perl use strict; use warnings; #usage testmode file, file defaults to self my $open = shift || $0; my $mode = (stat $open)[2]; print '(stat(file))[2] is ',$mode,' which is decimal representation of the octal value with file types',\n; printf Permissions are %04o,$mode 0; print ' with file type masked off',\n; my $val = sprintf(%o, $mode); print $mode in oct is $val\n; my $perms = (stat($open))[2] 0; my $oct_perms = sprintf %lo, $perms; print 'decimal perms are ',$perms,' with file type masked off',\n; print 'octal_perms are ',$oct_perms,' with file type masked off',\n; print 'oct(',$perms,') ','is ',sprintf(%o,$perms),\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
On Thu, Feb 14, 2002 at 10:43:41AM -0500, zentara wrote: On Wed, 13 Feb 2002 15:14:39 -0900, [EMAIL PROTECTED] (Michael Fowler) wrote: You have it backwards. Octal 100500 is 33088 in decimal. You just did the conversion with the oct operator. You're confused about what oct does; it returns the decimal representation of an octal value. Also, Perl is going to trip you up here; the decimal After putting on the dunce cap and typing 100 times: the oct function takes a decimal value and returns octal, and (stat file)[2] returns a decimal value I finally see it. Heh, you still have that backwards. oct() interprets its argument as an octal value, and returns its decimal equivalent. Also, it's best not to think of functions returning numbers in a specific base. Functions simply return numbers, its when you output them that they have to be interpreted in a certain base. #!/usr/bin/perl use strict; use warnings; #usage testmode file, file defaults to self my $open = shift || $0; my $mode = (stat $open)[2]; print '(stat(file))[2] is ',$mode,' which is decimal representation of the octal value with file types',\n; Correct. printf Permissions are %04o,$mode 0; print ' with file type masked off',\n; Correct. my $val = sprintf(%o, $mode); print $mode in oct is $val\n; Correct. my $perms = (stat($open))[2] 0; my $oct_perms = sprintf %lo, $perms; print 'decimal perms are ',$perms,' with file type masked off',\n; Correct. print 'octal_perms are ',$oct_perms,' with file type masked off',\n; Correct. print 'oct(',$perms,') ','is ',sprintf(%o,$perms),\n; Given all that I'm not sure how you came to this incorrect conclusion. oct($perms) is not sprintf(%o, $perms). Given a decimal $perms of 320, sprintf(%o, $perms) returns 500, and oct($perms) returns 208. I'm tempted to say oct and sprintf's %o are symmetric, meaning they reverse each other. You can think of it that way if it helps, but it might confuse you in the case of oct(0500) and sprintf(%o, 0500). Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
On Thu, 14 Feb 2002 07:57:07 -0900, [EMAIL PROTECTED] (Michael Fowler) wrote: I'm tempted to say oct and sprintf's %o are symmetric, meaning they reverse each other. You can think of it that way if it helps, but it might confuse you in the case of oct(0500) and sprintf(%o, 0500). Ok, I have to give it one last try. #!/usr/bin/perl use strict; use warnings; #usage testmode file, file defaults to self my $open = shift || $0; my $mode = (stat $open)[2]; print '(stat(file))[2] is ',$mode,' which is decimal representation of the octal perms with file types',\n; my $val = sprintf(%o, $mode); print $mode in oct is $val\n; printf Octal perms are %04o,$mode 0; print ' with file type masked off',\n; my $perms = (stat($open))[2] 0; my $oct_perms = sprintf %lo, $perms; print 'decimal perms are ',$perms,' with file type masked off',\n; #print 'octal_perms are ',$oct_perms,' with file type masked off',\n; print If octal perms are $oct_perms, then to convert to decimal perms,,' oct(',$oct_perms,') is ',oct($oct_perms),\n; print 'Then if dec perms are ',$perms,' you convert to octal with sprintf(%o,',$perms,') which is ', sprintf(%o,$perms),\n; print 'sprintf(%o,oct($oct_perms)) is ', sprintf(%o,oct($oct_perms)),\n; print 'and conversely oct(sprintf(%o,oct($oct_perms)) is ',oct(sprintf(%o,oct($oct_perms))),\n; print 'I\'m a tortured nit-pik but I think I figured out octals :-)',\n -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
To change from decimal to octal, you can use sprintf with a %o format string my $val = sprintf( %o, 8 ); print $val; 10 - Original Message - From: zentara [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, February 13, 2002 5:20 PM Subject: octal file permissions I'm trying to save file permissions with stat, so I can restore permissions back to the file after editing. my $mode = (stat $open)[2]; #and then later chmod $mode,$open; The program is working, but it has caused me to wonder what is going on with the octal permissions. For instance, if I have a file with permissions 100500, (stat file)[2] will report mode to be 33088. Ok, I can understand that because 33088 = oct(100500) but given the octal number 33088 what function do I use to convert it to 100500? In other words what is the procedure to do octal to decimal conversion? Also, I thought that the 0500 style of given file permissions IS octal. So the docs tell me to do a chmod like this with octal values: chmod 0500, file but in my program I'm using 33088 as the octal value? futher confusing the issue for me is that oct(0500) = 208 8 is not supposed to be in the octal digit set of 0 to 7? WTF?? Is there some easy to grasp perspective on this? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: octal file permissions
On Wed, Feb 13, 2002 at 05:20:38PM -0500, zentara wrote: Ok, I can understand that because 33088 = oct(100500) but given the octal number 33088 what function do I use to convert it to 100500? In other words what is the procedure to do octal to decimal conversion? You have it backwards. Octal 100500 is 33088 in decimal. You just did the conversion with the oct operator. Also, I thought that the 0500 style of given file permissions IS octal. It is. So the docs tell me to do a chmod like this with octal values: chmod 0500, file Which works. but in my program I'm using 33088 as the octal value? No, you're using 33088 as the decimal value. chmod takes two or more arguments; the numerical mode, and a list of files. The number can be in any representation you like. In the case of chmod(0500, $file) it's in octal, in the case of chmod(320, $file) it's in decimal. The two numbers, 0500 and 320, are equivalent representations of the same numbers, they're just in different bases. futher confusing the issue for me is that oct(0500) = 208 8 is not supposed to be in the octal digit set of 0 to 7? You're confused about what oct does; it returns the decimal representation of an octal value. Also, Perl is going to trip you up here; the decimal equivalent of 0500 is 320, not 208; Perl is converting 0500 to decimal, passing that to oct, and then oct is converting that to decimal again. So, basically, oct(320) == 208. oct is usually used for converting string values to decimal values; it's for interpreting numbers in a different base. For example, say your program takes an octal mode as an argument; if it were to use this mode directly, chmod($ARGV[0], $file), $ARGV[0] would be interpreted in a decimal base, giving a file the permissions -rwxrw-r-- (decimal 500 is octal 0764) when the user intended -r-x-- (octal 0500). Please see perldoc -f oct. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]