public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Why is stdin always a pipe?
@ 2018-07-20 13:17 João Eiras
  2018-07-20 14:52 ` David Macek
  0 siblings, 1 reply; 6+ messages in thread
From: João Eiras @ 2018-07-20 13:17 UTC (permalink / raw)
  To: cygwin

Hi.

I'm trying to figure out the best way to detect when my program is
being piped data.

So far I've used in linux often "[[ -p /dev/stdin ]]". During an
interactive session, /dev/stdin will be a character device, not a
pipe, and if stdin is closed ( command 0<&- ) then it's nothing.

But in cygwin, /dev/stdin is ALWAYS a pipe. So, I could use "[[ -t 0
]]" to check if stdin is a tty, but that would not cover when running
my script in a background job or service with a closed stdin.

So, a) what is the correct way to detect if stdin is being piped data
and b) is this a bug or design limitation ?

Thank you.

--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Why is stdin always a pipe?
  2018-07-20 13:17 Why is stdin always a pipe? João Eiras
@ 2018-07-20 14:52 ` David Macek
  2018-07-20 15:12   ` João Eiras
  0 siblings, 1 reply; 6+ messages in thread
From: David Macek @ 2018-07-20 14:52 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 272 bytes --]

Hi.  I see this:

> $ [[ -p /dev/stdin ]] && echo pipe || echo nopipe
> nopipe
> $ [[ -t /dev/stdin ]] && echo term || echo noterm
> noterm

Am I missing something?  I tested various scenarios and stdin is never a pipe, neither a tty for me.

-- 
David Macek


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4002 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Why is stdin always a pipe?
  2018-07-20 14:52 ` David Macek
@ 2018-07-20 15:12   ` João Eiras
  2018-07-21  0:59     ` Brian Inglis
  0 siblings, 1 reply; 6+ messages in thread
From: João Eiras @ 2018-07-20 15:12 UTC (permalink / raw)
  To: cygwin

>> $ [[ -p /dev/stdin ]] && echo pipe || echo nopipe
>> nopipe

Interesting, it's always a pipe for me. What about ls ?

$ ls -l /dev/stdin
prw------- 1 user None 0 Jun  4 15:54 /dev/stdin

>> $ [[ -t /dev/stdin ]] && echo term || echo noterm
>> noterm

The '-t' operator is used with file descriptors, so "[[ -t 0 ]]"
http://tldp.org/LDP/abs/html/fto.html

--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Why is stdin always a pipe?
  2018-07-20 15:12   ` João Eiras
@ 2018-07-21  0:59     ` Brian Inglis
  2018-07-21  1:59       ` Heavenly Avenger
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Inglis @ 2018-07-21  0:59 UTC (permalink / raw)
  To: cygwin

On 2018-07-20 07:17, João Eiras wrote:
>>> $ [[ -p /dev/stdin ]] && echo pipe || echo nopipe
>>> nopipe
> 
> Interesting, it's always a pipe for me. What about ls ?
> 
> $ ls -l /dev/stdin
> prw------- 1 user None 0 Jun  4 15:54 /dev/stdin

Are you using a terminal that does not provide a console interface?
That is a pipe; Cygwin terms look like this:

$ ll -go /dev/std*; ll -go /proc/self/fd/[012]; ll -go /dev/pty?
lrwxrwxrwx 1 15 May 14  2013 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 15 May 14  2013 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 15 May 14  2013 /dev/stdout -> /proc/self/fd/1
lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/0 -> /dev/pty0
lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/1 -> /dev/pty0
lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/2 -> /dev/pty0
crw--w---- 1 136, 0 Jul 20 08:18 /dev/pty0
crw--w---- 1 136, 1 Jul 20 08:18 /dev/pty1
$ for fd in 0 1 2 3; do test -t $fd; echo fd $fd term $?; done
fd 0 term 0
fd 1 term 0
fd 2 term 0
fd 3 term 1
$ for f in /dev/{std*,pty?}; do test -p $f; echo file $f pipe $?; done
file /dev/stderr pipe 1
file /dev/stdin pipe 1
file /dev/stdout pipe 1
file /dev/pty0 pipe 1
file /dev/pty1 pipe 1

where ptys are terms and are not pipes.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Why is stdin always a pipe?
  2018-07-21  0:59     ` Brian Inglis
@ 2018-07-21  1:59       ` Heavenly Avenger
  2018-07-21  2:31         ` João Eiras
  0 siblings, 1 reply; 6+ messages in thread
From: Heavenly Avenger @ 2018-07-21  1:59 UTC (permalink / raw)
  To: cygwin

My results match Brian Inglis', not João Eiras'. I run cygwin from a 
putty session (using putty's cygtermd little proxy) if that matters.


On 7/20/2018 11:52 AM, Brian Inglis wrote:
> On 2018-07-20 07:17, João Eiras wrote:
>>>> $ [[ -p /dev/stdin ]] && echo pipe || echo nopipe
>>>> nopipe
>> Interesting, it's always a pipe for me. What about ls ?
>>
>> $ ls -l /dev/stdin
>> prw------- 1 user None 0 Jun  4 15:54 /dev/stdin
> Are you using a terminal that does not provide a console interface?
> That is a pipe; Cygwin terms look like this:
>
> $ ll -go /dev/std*; ll -go /proc/self/fd/[012]; ll -go /dev/pty?
> lrwxrwxrwx 1 15 May 14  2013 /dev/stderr -> /proc/self/fd/2
> lrwxrwxrwx 1 15 May 14  2013 /dev/stdin -> /proc/self/fd/0
> lrwxrwxrwx 1 15 May 14  2013 /dev/stdout -> /proc/self/fd/1
> lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/0 -> /dev/pty0
> lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/1 -> /dev/pty0
> lrwxrwxrwx 1 0 Jul 20 08:18 /proc/self/fd/2 -> /dev/pty0
> crw--w---- 1 136, 0 Jul 20 08:18 /dev/pty0
> crw--w---- 1 136, 1 Jul 20 08:18 /dev/pty1
> $ for fd in 0 1 2 3; do test -t $fd; echo fd $fd term $?; done
> fd 0 term 0
> fd 1 term 0
> fd 2 term 0
> fd 3 term 1
> $ for f in /dev/{std*,pty?}; do test -p $f; echo file $f pipe $?; done
> file /dev/stderr pipe 1
> file /dev/stdin pipe 1
> file /dev/stdout pipe 1
> file /dev/pty0 pipe 1
> file /dev/pty1 pipe 1
>
> where ptys are terms and are not pipes.
>


--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Why is stdin always a pipe?
  2018-07-21  1:59       ` Heavenly Avenger
@ 2018-07-21  2:31         ` João Eiras
  0 siblings, 0 replies; 6+ messages in thread
From: João Eiras @ 2018-07-21  2:31 UTC (permalink / raw)
  To: cygwin

On 7/20/2018 11:52 AM, Brian Inglis wrote:
> Are you using a terminal that does not provide a console interface?
> That is a pipe; Cygwin terms look like this:
>

On 21 July 2018 at 02:59, Heavenly Avenger <avenger@avenger.ws> wrote:
> My results match Brian Inglis', not João Eiras'. I run cygwin from a putty
> session (using putty's cygtermd little proxy) if that matters.
>

Oh right ! Now I checked again, using just a clean windows console
with cygwin running in it, and /dev/stdin are /dev/stdout devices.

I saw the problem running Cygwin with ConEmu. Guess the problem comes
from elsewhere.

Thanks for the debug session then.

Cheers.

--
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-07-21  1:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-20 13:17 Why is stdin always a pipe? João Eiras
2018-07-20 14:52 ` David Macek
2018-07-20 15:12   ` João Eiras
2018-07-21  0:59     ` Brian Inglis
2018-07-21  1:59       ` Heavenly Avenger
2018-07-21  2:31         ` João Eiras

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).