- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
I like this format for the CodeSOD column. A few paragraphs to describe what's actually wrong with the code shown really helps those who aren't familiar with every language on earth.
Admin
That file browser was done on an IRIX machine, for it was available for SGI IRIX (MIPS) machines as a freeware app, so you could browse your IRIX machine exactly like the movie.
Anyhow, I suspect permissions is probably the only time people use octal because it's simpler...
And if I could only figure out what "x" means on a directory or the sticky bit... those are the annoying complex and system-dependent ones.
Admin
Admin
From now on, all CodeSODs must be submitted using pseudo code.
Admin
$permsbin is better.
Admin
Having done similar stupid things, the easy fix for this is to copy some executable to be named, say, foo, then cat chmod into foo, then now rename foo to chmod and copy it back over the original chmod.
Admin
Admin
Personally I find attacks on regular expressions offensive. Like every other tool/technique, they can be abused. (Don't try to parse HTML with them. Please.) They were developed as a Domain Specific Language for handling certain types of parsing problems, and they handle those quite well.
This is not meant to be a slam on Perl (which, like other tools, has its uses), but I don't find stat($path)->mode & 0444 particularly readable either. Other Perl built-in features should be used instead (as others have mentioned) or this logic should be encapsulated.
Admin
Admin
Nonsense. It will succeed very quickly or fail in a small handful of iterations.
Admin
I'm sure everyone's heard the old joke about how there are 10 kinds of people -- those who understand binary and those who don't. It's true, though -- and this is exactly the sort of mistake commonly (and frequently) made by people who don't understand binary arithmetic and the application of boolean logic operators to bitwise representations.
Admin
Admin
I've been programming in perl since 1993. I don't know I've seen something quite that bad (aside from code entered into a contest). Like others have said, what's his beef with using normal file tests?
Now that I think about it, I'm wondering why he bothered with recasting and string comparisons at all when he simply could have shelled out to ls with backticks and then parsed the output of that line by line, looking for his file...
Admin
And the solution, after all the effort, is wrong: a 0400 (*) file may be perfectly readable, if it has the right owner. Actually it is quite complex to see if somebody can read (or write) a file. So that's why you can do
-r filename
to see if the current effective user can read the file.
*) leading zero indicates this is an octal value.
Admin
Even better - it says my $perms :eek:
Admin
HAHA, I didn't catch that.
Admin
Am I the only one who noticed that "bit 256" would imply you are using at least a 256-bit value... Unix must have a lot of permissions to keep track of...
Admin
Isn't it easier to use the file test operators?
if -r $filename then...
Admin
Ed's coworker may have two hundred problems, but a bitch ain't one.
Admin
Actually this Regexp depends on the compiler to do what the programmer wanted. If it is set to take the first possible solution, then it will fail on a string less than 9 characters long, or assign the first, 4th, and 7th characters to $1, $2, and $3. If it is set to do greedy matching, it will assign from the end of the string, the 9th, 6th, and 3rd characters to $1, $2, and $3. But technically, any 3 characters spaced 3 apart, starting anywhere in a string >=9 characters, would be a legal result. So this regexp is pretty unreliable. If (as the expression implies) the programmer wanted matching from the end, the expression should be:
$permsbin =~ s/.*(.)..(.)..(.)..$/$1$2$3/;
But really, '-r' should be used in place of the whole mess.
Admin
Damn, someone beat me to it. I did a find on "bitch" and found nothing, didn't see the other guy use "b1tch". I guess regular expressions would have solved this problem.
Admin
What is PERL? "Protecting Escarpment Rural Lands"? (perlofburlington.org)
If you are talking about the computer language, it's Perl, not PERL, unless you are missing a clue.
Admin
Bill Gates personnally did software development on many, many operating systems, including Xenix. He was the chief architect for the BASIC and Fortran on all OS where they were ported by Microsoft (including on the Altair, Mac OS and god knows what).
Contrary to common beliefs, MS-Dos is a product that came quite late in Microsoft history, before that the company pushed hard on Xenix and on cross-platforms languages, in which Bill Gates played a central role. Same with OS2, which was a IBM product largely developed by Microsoft.
Bill Gates was always involved in business, but he kept a deep technical role until 2-3 years ago when Ray Ozzie came onboard at Microsoft. (Ozzie is the Lotus Notes guy).
A few years back there was a contest during a convention, a competition between Bill Gates and many famous technical journalists (all experienced IT guys), to see who would come up with some specific feature in a software in the shortest time possible. Bill Gates won.
The guy is unplesant but he knows his stuff.
Admin
Yup. Also, you wouldn't check "bits 4, 32 and 256".. You'd check bits 2, 5 and 8 (starting at bit 0). The values used for ANDing would be 4, 32 and 256, though.
Admin
"Interesting" does not mean "fiendishly difficult to solve with the tool at hand," contrary to what many Perl dimwits might suggest.
Back to the illogic of the OP: I presume that, if "1" is TRUE, and "0" is FALSE, then "111" is FILE_PERMISSION_NOT_FOUND?
Admin
The Ed's colleague's solution is ugly indeed... but so is "perms & 0444 == true" -- because... it answers the wrong question!
The proper answer to "whether you can read the file" question would be
(myeffuser == file.owner && (file.perms & 0400)) || (myeffgroup == file.group && (file.perms & 0040)) || (file.perms & 0004))
But, putting my "write what you MEAN, dammit!" ideology to use, if you want to know whether you can read the file, all you need to do is...
---------------- begin Ruby code --------------- begin File.open(target_file_name) { |f| /* do nothing */ } puts ":)" # File.open with a block opens the file, runs the block, closes the file rescue Errno::EACCES # or throws exception if it can't. puts ":(" # We particularily capture the "Access denied" error end ---------------- end Ruby code ---------------
Don't fool around showing off how cool you are knowing UNIX perm flags, binary arithmetics, etc etc! The filesystem knows its own guts better, no need to fsck around emulating it. Besides, this code is obviously future proof and can be used on ANY filesystem, be it NTFS or whatever.
Admin
Admin
Admin
implemented idiotically, just about anything could take millions of years to run ... are you new to this site and code in general?
Admin
...or not... That'll be true even if you don't have read permission but everyone else does. i.e. ---r--r-- & 0444 == 044 == true
Sorry about that chief. We now return you to your regularly scheduled comments.
Admin
If the "mark of the beast" is 666, then I, personally, feel good that the beast only has read/write permissions on me and not "execute."
</revelation>Admin
That made my day...
because that's the first thing I thought of. Does that make me immature?
Admin
WTF aside...
Ahh sure, *nix kicks ass.
And sure, *nix is stuck forever in the land of hopeless geek weenies like most of the posters here who either (a) can't figure out why it's never taken off or (b) are petulant, clannish children wanting to feel cooler because they know the secret. Long, long-standing condensed retardedness like bitflags to change file system modes is one reason. I did something similar twenty years ago in an app and I still feel bad about it.
The fact that more than one challenged geek can't remember the order and/or is too lazy to use man suggests that it's a stupid idea.
Admin
http://perldoc.perl.org/functions/stat.html
Admin
Well, PERL is an acronym for "Practical Extraction and Report Language", so it's not incorrect to write it out in all caps.
Captcha: similis. Is that similar to syphilis?
Admin
http://en.wikipedia.org/wiki/Perl#Name
Larry Wall originally derived the name (and the bless() function) from his religious beliefs.
http://en.wikipedia.org/wiki/Larry_Wall
Admin
Is it just me or is that code... really naughty looking?
Admin
Admin
This one is not very noticable unless you got someone check the code manaually.
Admin
So I stand (or sit) corrected. That doesn't happen very often! (My wife would tell you otherwise.)
Admin
So would mine. :-)
Admin
Admin
If you're the user actually setting the permissions you can use u+r to set read permission to the user, or you can use one of the many GUIs available for just that.
You seem to have some resentment towards unix for some reason...
Admin
This is seriously the first thing online to make me literally laugh out loud in weeks.
I'm not sure what that says about me.
Admin
What? Lazy people who can't be bothered to look up how to use something will always find ways to misuse things. What wouldn't be considered a stupid idea under your criteria?
Admin
meanwhile, windows still uses file extensions to work out executable permission and can't mount a volume no execute, among other sins. At least unix also has ACLs, even though you enerally don't need them.
Admin
'-r $path' ... wow
Admin
I think you just ruined that variable name for me...
Admin
Hey, it's Perl, of course there's more than one way to do it. (They never said anything about the number of non-stupid ways.)