delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/09/29/11:25:32

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: mwoehlke <mwoehlke AT tibco DOT com>
Subject: Re: cygwin detection
Date: Fri, 29 Sep 2006 10:22:36 -0500
Lines: 39
Message-ID: <efjdns$lnq$1@sea.gmane.org>
References: <001601c6e3ce$403f0d80$be32000a AT idirect DOT net>
Mime-Version: 1.0
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20060909 Thunderbird/1.5.0.7 Mnenhy/0.7.4.0
In-Reply-To: <001601c6e3ce$403f0d80$be32000a@idirect.net>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Kenneth Nellis wrote:
> Couldn't find anything relevant in the archives or the documentation...
> 
> I have bash scripts that I want to run identically under Cygwin and 
> Linux, which sometimes require the scripts to detect the environment 
> and branch accordingly. There are numerous ways to do Cygwin detection, 
> but I was wondering what technique should work with the widest audience 
> and be most immune to future Cygwin developments.
> 
> FWIW, below are various techniques that work for *me* *today*, some of 
> which have obvious flaws.
> 
> if [ -f /usr/bin/cygwin1.dll ]; then
> if [ $CYGWIN_ROOT ]; then
> if [ $OSTYPE = cygwin ]; then
> if [ $(uname -s | grep -c CYGWIN) -gt 0 ]; then
> if [ $(grep -c cygwin <<< ${BASH_VERSINFO[5]}) -gt 0 ]; then
> if is_cygwin; then    # where is_cygwin is a locally-built C program
>                       # that tests #ifdef __CYGWIN__

Well, FWIW I've always used a combination of '#ifdef __CYGWIN__' and 
uname (basically, the former when compiling C and the latter in 
scripts)... of course, both of those are only really testing if gcc and 
uname (respectively) are pointing at Cygwin versions. I would say that 
99% of the time though 'uname' will work; basically it will only fail if 
you have an Interix/MKS/MinGW 'uname' in PATH, but you can always check 
for those as well to distinguish "real UNIX" from "UNIX on Windows". If 
anyone builds a 'uname' on a Windows system that tells you 'Linux', they 
deserve what they get. :-) I'd be inclined to say that anyone with a 
system where 'uname' is wrong deserves to have things break.

If you *know* you are running bash, you can also check if it is MinGW by 
testing if $BASH starts with '/' (at least, I assume it would be a DOS 
path on MinGW, and MKS doesn't have bash). But this won't distinguish 
Cygwin from Interix.

-- 
Matthew
My preferred shell is Christian. It's Bourne Again.


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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019