This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Is this supposed to work this way?


On 4/3/2012 7:35 PM, Andrey Repin wrote:

what is the standard way of detecting OS type? uname -o ?

The wise try not to do that at all.


Instead, take in a little wisdom from the people who created autoconf, a piece of software that's been uncommonly successful[*] by FOSS standards: don't test for platforms, test for features.

The reason is, platform feature sets change. When you write software that makes decisions based on platforms, it tends to break when those changes happen. Or, nearly as bad, your software continues to use some workaround that eventually becomes unnecessary when the platform fixes the problem that lead you to create the workaround.

It's the latter case I'd worry about here.

What's happening is that because you're launching the shell script through some Windows-native mechanism (double-clicking it in Explorer, maybe?) it's getting a Windows style path in $0. Maybe cygwin1.dll will one day detect that situation and translate the path automatically.

The way I'd suggest you attack the problem is do a regex match on $0 to see if it looks like a Windows path, then run it through cygpath if so. "^[A-Z]:/" is probably a good enough test; it's unlikely to ever match a path your script gets on other platforms.

A false positive would require that someone be *trying* to create an MS-DOS style path system on a *ix box, something far to ooky to believe has ever happened. But then, people are strange.

Resist the temptation to back up the path style test with a platform test like the [ -f /bin/cygwin1.dll ] suggested by Barry. If the path looks like a Windows path, have the backbone to just send the path blindly through cygpath. If ever you run into a situation where cygpath doesn't exist but the path matches your regex, fix the regex or add a secondary feature test, don't resort to platform detection.


[*] I think it's fair to say Autoconf's only real competition is ad hockery.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]