Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Old mail has arrived.


devel / comp.unix.programmer / popen() and r+

SubjectAuthor
* popen() and r+Muttley
+* popen() and r+Kaz Kylheku
|+- popen() and r+Muttley
|`* popen() and r+Nicolas George
| +* popen() and r+Muttley
| |`* popen() and r+Nicolas George
| | +- popen() and r+Muttley
| | `* popen() and r+Kenny McCormack
| |  `* popen() and r+Scott Lurndal
| |   +- popen() and r+Kenny McCormack
| |   `* popen() and r+Muttley
| |    `* popen() and r+Scott Lurndal
| |     `- popen() and r+Muttley
| `* popen() and r+Kaz Kylheku
|  `- popen() and r+Muttley
`* popen() and r+Scott Lurndal
 +* popen() and r+Muttley
 |+* popen() and r+Kenny McCormack
 ||`- popen() and r+Muttley
 |`* popen() and r+Kaz Kylheku
 | `* popen() and r+Geoff Clare
 |  +- popen() and r+Nicolas George
 |  `- popen() and r+Kaz Kylheku
 `* popen() and r+Nicolas George
  `* popen() and r+Muttley
   `* popen() and r+Geoff Clare
    `- popen() and r+Muttley

Pages:12
popen() and r+

<tpgnk9$5sq$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2983&group=comp.unix.programmer#2983

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: popen() and r+
Date: Mon, 9 Jan 2023 09:43:37 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpgnk9$5sq$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="6042"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Mon, 9 Jan 2023 09:43 UTC

Has anyone ever used popen() with the r+ option on MacOS? Writing to the file
descriptor works fine (the output goes to the printer queue) but any attempt to
read the response from CUPS from the file descriptor simply hangs. eg:

if ((pfile = popen("lp","r+")))
{ ... write data to printer ...
fgets(str,sizeof(str),pfile); <- hangs here
}

It also hangs if I try while(!feof(pfile)) or read(fileno(pfile),....)

lp is producing output and I can redirect to a file , eg popen("lp > myfile",..)
I assume this is some kind of buffer flush issue but I can't find anything on
google about it.

Thanks for any help.

Re: popen() and r+

<20230109030226.641@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2984&group=comp.unix.programmer#2984

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!rocksolid2!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 11:20:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <20230109030226.641@kylheku.com>
References: <tpgnk9$5sq$1@gioia.aioe.org>
Injection-Date: Mon, 9 Jan 2023 11:20:09 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="26acfbd36e92d88d0251595dc6168ef5";
logging-data="190196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+cO5lZHmu3/p4Lnt1CDBxkhI967M4OH4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:F8HyCzG9Exj+XmUjIYBQCrKd0cE=
 by: Kaz Kylheku - Mon, 9 Jan 2023 11:20 UTC

On 2023-01-09, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> Has anyone ever used popen() with the r+ option on MacOS? Writing to the file

Coprocesses are tricky; it's easy to get into a deadlock if you just
read and write with one thread, without multiplexing the I/O using
poll or select.

> descriptor works fine (the output goes to the printer queue) but any attempt to
> read the response from CUPS from the file descriptor simply hangs. eg:
>
> if ((pfile = popen("lp","r+")))
> {
> ... write data to printer ...

Did you fflush(pfile) here? Maybe all the data didn't go.
I think popen returns streams that are in fully buffered mode,
so output is only flushed when a buffer fills, the stream is
closed or fflush is called.

You would think that a simple half-duplex situation like this would
avoid deadlock, since lp should read your entire request until the end,
and only then produce a response.

However, if some trailing part of your request is sitting in a buffer,
such that lp is waiting for that last bit before replying, then you have
a deadlock.

(Note that ISO C itself requires a flush operation when switching from
output to input on a bidirectional stream, as a matter of well-defined
behavior. When switching from input to output, a file positioning
operation is required (regardless of whether the stream supports
positioning; seeking zero bytes from the current position with
fseek should do the trick). I think the main reason for this is that
some library implementations share one buffer between input
and output.)

> fgets(str,sizeof(str),pfile); <- hangs here
> }

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: popen() and r+

<tpgtse$1bqq$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2985&group=comp.unix.programmer#2985

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 11:30:22 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpgtse$1bqq$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com>
Injection-Info: gioia.aioe.org; logging-data="44890"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Mon, 9 Jan 2023 11:30 UTC

On Mon, 9 Jan 2023 11:20:09 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-01-09, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> Has anyone ever used popen() with the r+ option on MacOS? Writing to the file
>
>
>Coprocesses are tricky; it's easy to get into a deadlock if you just
>read and write with one thread, without multiplexing the I/O using
>poll or select.
>
>> descriptor works fine (the output goes to the printer queue) but any attempt
>to
>> read the response from CUPS from the file descriptor simply hangs. eg:
>>
>> if ((pfile = popen("lp","r+")))
>> {
>> ... write data to printer ...
>
>Did you fflush(pfile) here? Maybe all the data didn't go.

No I didn't. Just tried it - didn't make any difference sadly.

>You would think that a simple half-duplex situation like this would
>avoid deadlock, since lp should read your entire request until the end,
>and only then produce a response.
>
>However, if some trailing part of your request is sitting in a buffer,
>such that lp is waiting for that last bit before replying, then you have
>a deadlock.

Oddly if popened in write only mode the output from lp is written to stdout
without any delay and if stdout is redirected to a file, eg:

popen("lp > myfile","w")

that happily writes to the file.

>(Note that ISO C itself requires a flush operation when switching from
>output to input on a bidirectional stream, as a matter of well-defined
>behavior. When switching from input to output, a file positioning
>operation is required (regardless of whether the stream supports
>positioning; seeking zero bytes from the current position with
>fseek should do the trick). I think the main reason for this is that

Unfortunately not:

if (fseek(pfile,0,SEEK_SET) == -1) perror("fseek()");

results in

fseek(): Illegal seek

Perhaps the popen() implementation on MacOS/BSD is just broken. I'll have to
try it on Linux when I get a chance later.

Re: popen() and r+

<63bc021f$0$22249$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2986&group=comp.unix.programmer#2986

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!cleanfeed1-b.proxad.net!nnrp4-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$george@salle-s.org (Nicolas George)
Subject: Re: popen() and r+
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 09 Jan 2023 12:01:35 GMT
Lines: 8
Message-ID: <63bc021f$0$22249$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 09 Jan 2023 13:01:35 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1673265695 news-2.free.fr 22249 129.199.129.80:59830
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Mon, 9 Jan 2023 12:01 UTC

Kaz Kylheku , dans le message <20230109030226.641@kylheku.com>, a
écrit :
> However, if some trailing part of your request is sitting in a buffer,
> such that lp is waiting for that last bit before replying, then you have
> a deadlock.

I would guess it is not enough: lp is probably waiting for EOF, and there is
no way to close only half of a popen stream.

Re: popen() and r+

<tphdfa$1016$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2987&group=comp.unix.programmer#2987

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 15:56:26 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tphdfa$1016$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com> <63bc021f$0$22249$426a74cc@news.free.fr>
Injection-Info: gioia.aioe.org; logging-data="32806"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Mon, 9 Jan 2023 15:56 UTC

On 09 Jan 2023 12:01:35 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Kaz Kylheku , dans le message <20230109030226.641@kylheku.com>, a
> �crit�:
>> However, if some trailing part of your request is sitting in a buffer,
>> such that lp is waiting for that last bit before replying, then you have
>> a deadlock.
>
>I would guess it is not enough: lp is probably waiting for EOF, and there is
>no way to close only half of a popen stream.

Surely if you're going to implement r+ functionality you'd make sure you
flushed the write buffer before the close so this issue doesn't occur?

Re: popen() and r+

<63bc3c2e$0$3184$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2988&group=comp.unix.programmer#2988

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!cleanfeed2-b.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$george@salle-s.org (Nicolas George)
Subject: Re: popen() and r+
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 09 Jan 2023 16:09:18 GMT
Lines: 16
Message-ID: <63bc3c2e$0$3184$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 09 Jan 2023 17:09:18 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1673280558 news-1.free.fr 3184 129.199.129.80:37704
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Mon, 9 Jan 2023 16:09 UTC

Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
a écrit :
>>I would guess it is not enough: lp is probably waiting for EOF, and there is
>>no way to close only half of a popen stream.
> Surely if you're going to implement r+ functionality you'd make sure you
> flushed the write buffer before the close so this issue doesn't occur?

The issue is not about an unflushed buffer, it is about a stream that is
still open and therefore could receive more data. Consider this:

f = popen("some_command", "r+");
fprintf(f, "Hello.\n");
fflush(f);
fgets(line, f);

How does some_command know you are done talking after "Hello."?

Re: popen() and r+

<a8XuL.575788$GNG9.424370@fx18.iad>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2989&group=comp.unix.programmer#2989

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: popen() and r+
Newsgroups: comp.unix.programmer
References: <tpgnk9$5sq$1@gioia.aioe.org>
Lines: 29
Message-ID: <a8XuL.575788$GNG9.424370@fx18.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 09 Jan 2023 16:19:18 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 09 Jan 2023 16:19:18 GMT
X-Received-Bytes: 1627
 by: Scott Lurndal - Mon, 9 Jan 2023 16:19 UTC

Muttley@dastardlyhq.com writes:
>Has anyone ever used popen() with the r+ option on MacOS? Writing to the file
>descriptor works fine (the output goes to the printer queue) but any attempt to
>read the response from CUPS from the file descriptor simply hangs. eg:
>
>if ((pfile = popen("lp","r+")))
>{
> ... write data to printer ...
> fgets(str,sizeof(str),pfile); <- hangs here
>}
>
>It also hangs if I try while(!feof(pfile)) or read(fileno(pfile),....)
>
>lp is producing output and I can redirect to a file , eg popen("lp > myfile",..)
>I assume this is some kind of buffer flush issue but I can't find anything on
>google about it.

The 'p' in "popen" stands for "pipe". Pipes in unix are unidirectional.

From https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html

"Since open files are shared, a mode r command can be used as
an input filter and a mode w command as an output filter."

and
"The behavior of popen() is specified for values of mode of r and w."

You can't do what you want to do with popen.

Re: popen() and r+

<tphh1a$q5j$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2990&group=comp.unix.programmer#2990

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 16:57:15 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tphh1a$q5j$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad>
Injection-Info: gioia.aioe.org; logging-data="26803"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Mon, 9 Jan 2023 16:57 UTC

On Mon, 09 Jan 2023 16:19:18 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
>Muttley@dastardlyhq.com writes:
>>Has anyone ever used popen() with the r+ option on MacOS? Writing to the file
>>descriptor works fine (the output goes to the printer queue) but any attempt
>to
>>read the response from CUPS from the file descriptor simply hangs. eg:
>>
>>if ((pfile = popen("lp","r+")))
>>{
>> ... write data to printer ...
>> fgets(str,sizeof(str),pfile); <- hangs here
>>}
>>
>>It also hangs if I try while(!feof(pfile)) or read(fileno(pfile),....)
>>
>>lp is producing output and I can redirect to a file , eg popen("lp >
>myfile",..)
>>I assume this is some kind of buffer flush issue but I can't find anything on
>>google about it.
>
>The 'p' in "popen" stands for "pipe". Pipes in unix are unidirectional.
>
>From https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html
>
>
> "Since open files are shared, a mode r command can be used as
> an input filter and a mode w command as an output filter."
>
>and
> "The behavior of popen() is specified for values of mode of r and w."
>
>You can't do what you want to do with popen.

From the MacOS popen() man page:

reading or writing, not both. Because popen() is now implemented using a
bidirectional pipe, the mode argument may request a bidirectional data
flow. The mode argument is a pointer to a null-terminated string which
must be `r' for reading, `w' for writing, or `r+' for reading and
writing.

HTH.

Re: popen() and r+

<tphh5i$sa6$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2991&group=comp.unix.programmer#2991

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 16:59:30 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tphh5i$sa6$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr>
Injection-Info: gioia.aioe.org; logging-data="28998"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Mon, 9 Jan 2023 16:59 UTC

On 09 Jan 2023 16:09:18 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
> a �crit�:
>>>I would guess it is not enough: lp is probably waiting for EOF, and there is
>>>no way to close only half of a popen stream.
>> Surely if you're going to implement r+ functionality you'd make sure you
>> flushed the write buffer before the close so this issue doesn't occur?
>
>The issue is not about an unflushed buffer, it is about a stream that is
>still open and therefore could receive more data. Consider this:
>
> f = popen("some_command", "r+");
> fprintf(f, "Hello.\n");
> fflush(f);
> fgets(line, f);
>
>How does some_command know you are done talking after "Hello."?

Fair point. So clearly a 2 way popen isn't going to work for utilities that
don't respond until they get an EOF on their input. Unless there's some way to
spoof that.

Re: popen() and r+

<tphmhq$2cb2f$1@news.xmission.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2992&group=comp.unix.programmer#2992

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 18:31:22 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tphmhq$2cb2f$1@news.xmission.com>
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr>
Injection-Date: Mon, 9 Jan 2023 18:31:22 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2501711"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 9 Jan 2023 18:31 UTC

In article <63bc3c2e$0$3184$426a74cc@news.free.fr>,
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
> a crit:
>>>I would guess it is not enough: lp is probably waiting for EOF, and there is
>>>no way to close only half of a popen stream.
>> Surely if you're going to implement r+ functionality you'd make sure you
>> flushed the write buffer before the close so this issue doesn't occur?
>
>The issue is not about an unflushed buffer, it is about a stream that is
>still open and therefore could receive more data. Consider this:
>
> f = popen("some_command", "r+");
> fprintf(f, "Hello.\n");
> fflush(f);
> fgets(line, f);
>
>How does some_command know you are done talking after "Hello."?

Right. The classic example of this is "sort". sort cannot possibly
generate any output until it is certain that no more input is coming.

I assume "lp" behaves likewise. It looks to me that the best solution is
as given by OP: redirect output to a file (in the shell command passed to
popen()), then parse that file after doing pclose().

--
If Jeb is Charlie Brown kicking a football-pulled-away, Mitt is a '50s
housewife with a black eye who insists to her friends the roast wasn't
dry.

Re: popen() and r+

<YfZuL.38332$0dpc.15742@fx33.iad>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2993&group=comp.unix.programmer#2993

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx33.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: popen() and r+
Newsgroups: comp.unix.programmer
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr> <tphmhq$2cb2f$1@news.xmission.com>
Lines: 30
Message-ID: <YfZuL.38332$0dpc.15742@fx33.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 09 Jan 2023 18:44:08 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 09 Jan 2023 18:44:08 GMT
X-Received-Bytes: 2069
 by: Scott Lurndal - Mon, 9 Jan 2023 18:44 UTC

gazelle@shell.xmission.com (Kenny McCormack) writes:
>In article <63bc3c2e$0$3184$426a74cc@news.free.fr>,
>Nicolas George <nicolas$george@salle-s.org> wrote:
>>Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
>> a crit:
>>>>I would guess it is not enough: lp is probably waiting for EOF, and there is
>>>>no way to close only half of a popen stream.
>>> Surely if you're going to implement r+ functionality you'd make sure you
>>> flushed the write buffer before the close so this issue doesn't occur?
>>
>>The issue is not about an unflushed buffer, it is about a stream that is
>>still open and therefore could receive more data. Consider this:
>>
>> f = popen("some_command", "r+");
>> fprintf(f, "Hello.\n");
>> fflush(f);
>> fgets(line, f);
>>
>>How does some_command know you are done talking after "Hello."?
>
>Right. The classic example of this is "sort". sort cannot possibly
>generate any output until it is certain that no more input is coming.
>
>I assume "lp" behaves likewise. It looks to me that the best solution is
>as given by OP: redirect output to a file (in the shell command passed to
>popen()), then parse that file after doing pclose().

Or bite the bullet and use fork/exec and set up stdin and stdout
for the child appropriately with two stdio streams, one for read
and one for write.

Re: popen() and r+

<tpi02d$2cfh8$1@news.xmission.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2994&group=comp.unix.programmer#2994

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Mon, 9 Jan 2023 21:13:49 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tpi02d$2cfh8$1@news.xmission.com>
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr> <tphmhq$2cb2f$1@news.xmission.com> <YfZuL.38332$0dpc.15742@fx33.iad>
Injection-Date: Mon, 9 Jan 2023 21:13:49 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2506280"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 9 Jan 2023 21:13 UTC

In article <YfZuL.38332$0dpc.15742@fx33.iad>,
Scott Lurndal <slp53@pacbell.net> wrote:
....
>>I assume "lp" behaves likewise. It looks to me that the best solution is
>>as given by OP: redirect output to a file (in the shell command passed to
>>popen()), then parse that file after doing pclose().
>
>Or bite the bullet and use fork/exec and set up stdin and stdout
>for the child appropriately with two stdio streams, one for read
>and one for write.

Of course you can always do that. I took it as a parameter of the problem
that the goal was to stay inside the context of popen().

--
The difference between communism and capitalism?
In capitalism, man exploits man. In communism, it's the other way around.

- Daniel Bell, The End of Ideology (1960) -

Re: popen() and r+

<63bc8d5b$0$3075$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2995&group=comp.unix.programmer#2995

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp2-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$george@salle-s.org (Nicolas George)
Subject: Re: popen() and r+
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=utf-8
Date: 09 Jan 2023 21:55:39 GMT
Lines: 27
Message-ID: <63bc8d5b$0$3075$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 09 Jan 2023 22:55:39 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1673301339 news-1.free.fr 3075 129.199.129.80:54678
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Mon, 9 Jan 2023 21:55 UTC

Scott Lurndal, dans le message <a8XuL.575788$GNG9.424370@fx18.iad>, a
écrit :
>>Has anyone ever used popen() with the r+ option on MacOS?
^^^^^^
> The 'p' in "popen" stands for "pipe". Pipes in unix are unidirectional.
>
> From https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html
>
>
> "Since open files are shared, a mode r command can be used as
> an input filter and a mode w command as an output filter."
>
> and
> "The behavior of popen() is specified for values of mode of r and w."
>
> You can't do what you want to do with popen.

Unfortunately, they can:
https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/popen.3.html

“or `r+' for reading and writ-ing.”

Blame on Apple for adding this extension without documenting how to
half-close the stream. They could have returned two FILE*, or documented the
use of shutdown(fileno(p)).

Lucky guess: try write(fileno(p), "", 0).

Re: popen() and r+

<tpii0v$2cpjs$1@news.xmission.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2996&group=comp.unix.programmer#2996

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 02:20:15 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tpii0v$2cpjs$1@news.xmission.com>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad> <tphh1a$q5j$1@gioia.aioe.org>
Injection-Date: Tue, 10 Jan 2023 02:20:15 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2516604"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 10 Jan 2023 02:20 UTC

In article <tphh1a$q5j$1@gioia.aioe.org>, <Muttley@dastardlyhq.com> wrote:
....
>> "The behavior of popen() is specified for values of mode of r and w."
>>
>>You can't do what you want to do with popen.
>
>From the MacOS popen() man page:
>
> reading or writing, not both. Because popen() is now implemented using a
> bidirectional pipe, the mode argument may request a bidirectional data
> flow. The mode argument is a pointer to a null-terminated string which
> must be `r' for reading, `w' for writing, or `r+' for reading and
> writing.
>
>HTH.

When answering questions like this, it is important to be clear as to
whether you are answering from a "POSIX standards" POV vs. from the OP's
actual use case situation. The POSIX standards answer is, as stated above,
you can't do that (and now go home and bother us no more). However, OP
stated that he was using a Mac and that Mac documentation explicitly allows
this (with, of course, the caveat that it is an "extension").

Note, BTW, that I don't currently have access to a Mac to check this, but I
am assuming that the reports posted so far in this thread are accurate.

Interestingly, the Linux documentation that just checked makes it clear
that only "r" and "w" are valid. Even though, Linux certainly has
bidirectional pipes available (and, in fact, I think it is common knowledge
that pipe is implemented with socketpair), so you'd think Linux would have
a bidirectional version of popen() available, but apparently such is not
the case.

--
Watching ConservaLoons playing with statistics and facts is like watching a
newborn play with a computer. Endlessly amusing, but totally unproductive.

Re: popen() and r+

<tpjcgi$t10$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2997&group=comp.unix.programmer#2997

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 09:52:18 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpjcgi$t10$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad> <63bc8d5b$0$3075$426a74cc@news.free.fr>
Injection-Info: gioia.aioe.org; logging-data="29728"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Tue, 10 Jan 2023 09:52 UTC

On 09 Jan 2023 21:55:39 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Scott Lurndal, dans le message <a8XuL.575788$GNG9.424370@fx18.iad>, a
> écrit :
>>>Has anyone ever used popen() with the r+ option on MacOS?
> ^^^^^^
>> The 'p' in "popen" stands for "pipe". Pipes in unix are unidirectional.
>>
>> From https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html
>>
>>
>> "Since open files are shared, a mode r command can be used as
>> an input filter and a mode w command as an output filter."
>>
>> and
>> "The behavior of popen() is specified for values of mode of r and w."
>>
>> You can't do what you want to do with popen.
>
>Unfortunately, they can:
>https://developer.apple.com/library/archive/documentation/System/Conceptual/Man
>Pages_iPhoneOS/man3/popen.3.html
>
>“or `r+' for reading and writ-ing.”
>
>Blame on Apple for adding this extension without documenting how to
>half-close the stream. They could have returned two FILE*, or documented the
>use of shutdown(fileno(p)).
>
>Lucky guess: try write(fileno(p), "", 0).

Good idea, didn't work though. It does look like Apples implementation is
broken, or more politely - incomplete. There seems to be no way to close one
half of the pipe to force the child process to complete its stdout.

Re: popen() and r+

<tpjckc$up8$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2998&group=comp.unix.programmer#2998

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 09:54:20 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpjckc$up8$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr> <tphmhq$2cb2f$1@news.xmission.com> <YfZuL.38332$0dpc.15742@fx33.iad>
Injection-Info: gioia.aioe.org; logging-data="31528"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Tue, 10 Jan 2023 09:54 UTC

On Mon, 09 Jan 2023 18:44:08 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
>gazelle@shell.xmission.com (Kenny McCormack) writes:
>>In article <63bc3c2e$0$3184$426a74cc@news.free.fr>,
>>Nicolas George <nicolas$george@salle-s.org> wrote:
>>>Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
>>> a crit:
>>>>>I would guess it is not enough: lp is probably waiting for EOF, and there
>is
>>>>>no way to close only half of a popen stream.
>>>> Surely if you're going to implement r+ functionality you'd make sure you
>>>> flushed the write buffer before the close so this issue doesn't occur?
>>>
>>>The issue is not about an unflushed buffer, it is about a stream that is
>>>still open and therefore could receive more data. Consider this:
>>>
>>> f = popen("some_command", "r+");
>>> fprintf(f, "Hello.\n");
>>> fflush(f);
>>> fgets(line, f);
>>>
>>>How does some_command know you are done talking after "Hello."?
>>
>>Right. The classic example of this is "sort". sort cannot possibly
>>generate any output until it is certain that no more input is coming.
>>
>>I assume "lp" behaves likewise. It looks to me that the best solution is
>>as given by OP: redirect output to a file (in the shell command passed to
>>popen()), then parse that file after doing pclose().
>
>Or bite the bullet and use fork/exec and set up stdin and stdout
>for the child appropriately with two stdio streams, one for read
>and one for write.

Urgh. Yes I could but thats what one wants to avoid by using popen(). Its such
a faff to setup plus all the error checking whereas popen() is 1 line of code.

Re: popen() and r+

<tpjcpv$10v3$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=2999&group=comp.unix.programmer#2999

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 09:57:19 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpjcpv$10v3$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad> <tphh1a$q5j$1@gioia.aioe.org> <tpii0v$2cpjs$1@news.xmission.com>
Injection-Info: gioia.aioe.org; logging-data="33763"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Tue, 10 Jan 2023 09:57 UTC

On Tue, 10 Jan 2023 02:20:15 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) wrote:
>In article <tphh1a$q5j$1@gioia.aioe.org>, <Muttley@dastardlyhq.com> wrote:
>....
>>> "The behavior of popen() is specified for values of mode of r and w."
>>>
>>>You can't do what you want to do with popen.
>>
>>From the MacOS popen() man page:
>>
>> reading or writing, not both. Because popen() is now implemented using a
>
>> bidirectional pipe, the mode argument may request a bidirectional data
>> flow. The mode argument is a pointer to a null-terminated string which
>> must be `r' for reading, `w' for writing, or `r+' for reading and
>> writing.
>>
>>HTH.
>
>When answering questions like this, it is important to be clear as to
>whether you are answering from a "POSIX standards" POV vs. from the OP's
>actual use case situation. The POSIX standards answer is, as stated above,
>you can't do that (and now go home and bother us no more). However, OP
>stated that he was using a Mac and that Mac documentation explicitly allows
>this (with, of course, the caveat that it is an "extension").
>
>Note, BTW, that I don't currently have access to a Mac to check this, but I
>am assuming that the reports posted so far in this thread are accurate.
>
>Interestingly, the Linux documentation that just checked makes it clear
>that only "r" and "w" are valid. Even though, Linux certainly has

Yes, I didn't realise that when I started writing the code on MacOS.

>bidirectional pipes available (and, in fact, I think it is common knowledge
>that pipe is implemented with socketpair), so you'd think Linux would have
>a bidirectional version of popen() available, but apparently such is not
>the case.

Perhaps they realised it can't work properly in the current API of popen()
as there's no way to close one half of the pipe without some modification
to pclose().

Anyone know if r+ is a MacOS thing or it was brought over from FreeBSD?

Re: popen() and r+

<ve509j-3nf.ln1@ID-313840.user.individual.net>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3000&group=comp.unix.programmer#3000

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 13:23:11 +0000
Lines: 21
Message-ID: <ve509j-3nf.ln1@ID-313840.user.individual.net>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad>
<63bc8d5b$0$3075$426a74cc@news.free.fr> <tpjcgi$t10$1@gioia.aioe.org>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net wKB9SmnhoWAv5mf1XUvTigtXa8qkxx/AgUeZlTwlvxftWWjLGk
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:FfwLwfmdcfU96V78g70z3wZryOA=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Tue, 10 Jan 2023 13:23 UTC

Muttley wrote:

> It does look like Apples implementation is
> broken, or more politely - incomplete. There seems to be no way to close one
> half of the pipe to force the child process to complete its stdout.

If popen(..., "r+") is only usable with commands that don't wait for
EOF, then execute a command that doesn't wait for EOF.

You were trying to execute "lp". You could instead execute something
like:

sed '/^=EnD=oF=InPuT=$/{d;q;}' | lp

and write that sentinel string (plus a newline) to the stream after
the data to be printed. To reduce the risk of the sentinel string
occurring in the data to be printed, you could include some rarely-used
control characters.

--
Geoff Clare <netnews@gclare.org.uk>

Re: popen() and r+

<xKevL.263715$MVg8.125770@fx12.iad>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3001&group=comp.unix.programmer#3001

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: popen() and r+
Newsgroups: comp.unix.programmer
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr> <tphmhq$2cb2f$1@news.xmission.com> <YfZuL.38332$0dpc.15742@fx33.iad> <tpjckc$up8$1@gioia.aioe.org>
Lines: 41
Message-ID: <xKevL.263715$MVg8.125770@fx12.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 10 Jan 2023 14:37:17 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 10 Jan 2023 14:37:17 GMT
X-Received-Bytes: 2587
 by: Scott Lurndal - Tue, 10 Jan 2023 14:37 UTC

Muttley@dastardlyhq.com writes:
>On Mon, 09 Jan 2023 18:44:08 GMT
>scott@slp53.sl.home (Scott Lurndal) wrote:
>>gazelle@shell.xmission.com (Kenny McCormack) writes:
>>>In article <63bc3c2e$0$3184$426a74cc@news.free.fr>,
>>>Nicolas George <nicolas$george@salle-s.org> wrote:
>>>>Muttley@dastardlyhq.com, dans le message <tphdfa$1016$1@gioia.aioe.org>,
>>>> a crit:
>>>>>>I would guess it is not enough: lp is probably waiting for EOF, and there
>>is
>>>>>>no way to close only half of a popen stream.
>>>>> Surely if you're going to implement r+ functionality you'd make sure you
>>>>> flushed the write buffer before the close so this issue doesn't occur?
>>>>
>>>>The issue is not about an unflushed buffer, it is about a stream that is
>>>>still open and therefore could receive more data. Consider this:
>>>>
>>>> f = popen("some_command", "r+");
>>>> fprintf(f, "Hello.\n");
>>>> fflush(f);
>>>> fgets(line, f);
>>>>
>>>>How does some_command know you are done talking after "Hello."?
>>>
>>>Right. The classic example of this is "sort". sort cannot possibly
>>>generate any output until it is certain that no more input is coming.
>>>
>>>I assume "lp" behaves likewise. It looks to me that the best solution is
>>>as given by OP: redirect output to a file (in the shell command passed to
>>>popen()), then parse that file after doing pclose().
>>
>>Or bite the bullet and use fork/exec and set up stdin and stdout
>>for the child appropriately with two stdio streams, one for read
>>and one for write.
>
>Urgh. Yes I could but thats what one wants to avoid by using popen(). Its such
>a faff to setup plus all the error checking whereas popen() is 1 line of code.
>

Does OSX have a printer API that allows one to submit a job (rather than
using system(3) or popen(3))?

Re: popen() and r+

<tpk1rd$1bkv$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3003&group=comp.unix.programmer#3003

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 15:56:29 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpk1rd$1bkv$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad>
<63bc8d5b$0$3075$426a74cc@news.free.fr> <tpjcgi$t10$1@gioia.aioe.org>
<ve509j-3nf.ln1@ID-313840.user.individual.net>
Injection-Info: gioia.aioe.org; logging-data="44703"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Tue, 10 Jan 2023 15:56 UTC

On Tue, 10 Jan 2023 13:23:11 +0000
Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote:
>Muttley wrote:
>
>> It does look like Apples implementation is
>> broken, or more politely - incomplete. There seems to be no way to close one
>> half of the pipe to force the child process to complete its stdout.
>
>If popen(..., "r+") is only usable with commands that don't wait for
>EOF, then execute a command that doesn't wait for EOF.
>
>You were trying to execute "lp". You could instead execute something
>like:
>
>sed '/^=EnD=oF=InPuT=$/{d;q;}' | lp
>
>and write that sentinel string (plus a newline) to the stream after
>the data to be printed. To reduce the risk of the sentinel string
>occurring in the data to be printed, you could include some rarely-used
>control characters.

Sounds like more trouble than its worth and rather flakey.

Re: popen() and r+

<tpk24a$1g6r$1@gioia.aioe.org>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3004&group=comp.unix.programmer#3004

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!aioe.org!QImplQW63EVMF2Hp+OxW0A.user.46.165.242.91.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Tue, 10 Jan 2023 16:01:14 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tpk24a$1g6r$1@gioia.aioe.org>
References: <tpgnk9$5sq$1@gioia.aioe.org> <63bc021f$0$22249$426a74cc@news.free.fr> <tphdfa$1016$1@gioia.aioe.org> <63bc3c2e$0$3184$426a74cc@news.free.fr> <tphmhq$2cb2f$1@news.xmission.com> <YfZuL.38332$0dpc.15742@fx33.iad> <tpjckc$up8$1@gioia.aioe.org> <xKevL.263715$MVg8.125770@fx12.iad>
Injection-Info: gioia.aioe.org; logging-data="49371"; posting-host="QImplQW63EVMF2Hp+OxW0A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Muttley@dastardlyhq.com - Tue, 10 Jan 2023 16:01 UTC

On Tue, 10 Jan 2023 14:37:17 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
>Muttley@dastardlyhq.com writes:
>>>Or bite the bullet and use fork/exec and set up stdin and stdout
>>>for the child appropriately with two stdio streams, one for read
>>>and one for write.
>>
>>Urgh. Yes I could but thats what one wants to avoid by using popen(). Its
>such
>>a faff to setup plus all the error checking whereas popen() is 1 line of code.
>
>>
>
>Does OSX have a printer API that allows one to submit a job (rather than
>using system(3) or popen(3))?

Yes, there's the standard CUPS C API which Linux also shares, but its a bit long
winded. However the main reasons for not using it are if the box still uses
the old style lp system CUPS is no use plus the headers and library might not
be installed either so the program won't even compile.

Re: popen() and r+

<20230111022557.476@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3009&group=comp.unix.programmer#3009

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Wed, 11 Jan 2023 10:40:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <20230111022557.476@kylheku.com>
References: <tpgnk9$5sq$1@gioia.aioe.org> <20230109030226.641@kylheku.com>
<63bc021f$0$22249$426a74cc@news.free.fr>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 11 Jan 2023 10:40:06 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="9732cfaec9e8303273061edbd19cc1c3";
logging-data="920820"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18N7zIEZGxCPgGZwGOAOPZRonVFRvCWNFA="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:lu3acCizF48ADE1P1G+Abpy/OmQ=
 by: Kaz Kylheku - Wed, 11 Jan 2023 10:40 UTC

On 2023-01-09, Nicolas George <nicolas$george@salle-s.org> wrote:
> Kaz Kylheku , dans le message <20230109030226.641@kylheku.com>, a
> écrit :
>> However, if some trailing part of your request is sitting in a buffer,
>> such that lp is waiting for that last bit before replying, then you have
>> a deadlock.
>
> I would guess it is not enough: lp is probably waiting for EOF, and there is
> no way to close only half of a popen stream.

That's a great observation.

Now the "r+" mode is not portable to begin with.

The POSIX model of standard I/O streams is such that a stream has one
underlying descriptor, accessible via fileno.

Suppose an implementation implements a bidirectional popen using only
one descriptor, such that the descriptor returned by fileno is all there
is; that object handles both data transfer directions.

Such an implementation is quite possibly using the socketpair function
to creae the bidirectional descriptor.

And so if socketpair is being used, then it should be possible to use
shutdown on the descriptor to do a half-close.

As in:

// write data to lp ...

// send everything to socket
fflush(pipe_stream);

// close in writing direction
if (shutdown(fileno(pipe_stream), SHUT_WR) != 0)
{
// didn't work: not a socket?
}

It's worth a try.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: popen() and r+

<20230111024122.143@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3010&group=comp.unix.programmer#3010

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Wed, 11 Jan 2023 10:45:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <20230111024122.143@kylheku.com>
References: <tpgnk9$5sq$1@gioia.aioe.org>
<a8XuL.575788$GNG9.424370@fx18.iad> <tphh1a$q5j$1@gioia.aioe.org>
Injection-Date: Wed, 11 Jan 2023 10:45:32 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="9732cfaec9e8303273061edbd19cc1c3";
logging-data="920820"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dsWrJ7/7QDrK66v4g/9ifKFv+o9IgTqc="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:6N9F85i9CLvq69ihkX/SU6qmxKU=
 by: Kaz Kylheku - Wed, 11 Jan 2023 10:45 UTC

On 2023-01-09, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Mon, 09 Jan 2023 16:19:18 GMT
> scott@slp53.sl.home (Scott Lurndal) wrote:
>>You can't do what you want to do with popen.
>
> From the MacOS popen() man page:
>
> reading or writing, not both. Because popen() is now implemented using a
> bidirectional pipe, the mode argument may request a bidirectional data
> flow. The mode argument is a pointer to a null-terminated string which
> must be `r' for reading, `w' for writing, or `r+' for reading and
> writing.

And, so, continuing in the same vein as my previous posting: we have had
bidirectional pipes in Unix for decades. The socketpair function creates
two descriptors and places them into an array, just like pipe. But
they are a pair of connected sockets: writing to one makes data appear
in the other and vice versa.

So there are two possibilities:

1. MacOS invented a new kind of bidirectional pipe that is other than a
socket pair.

2. The bidirectional pipe is actually a socket pair.

If (2) is the case, it would be amazing if, after flushing all the
written data to lp, you couldn't pull out the descriptor using fileno,
and then half-close it using shutdown(fd, SHUT_WR).

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: popen() and r+

<p1r29j-29d.ln1@ID-313840.user.individual.net>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3011&group=comp.unix.programmer#3011

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.programmer
Subject: Re: popen() and r+
Date: Wed, 11 Jan 2023 13:43:53 +0000
Lines: 30
Message-ID: <p1r29j-29d.ln1@ID-313840.user.individual.net>
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad>
<tphh1a$q5j$1@gioia.aioe.org> <20230111024122.143@kylheku.com>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net 8yRIapmDdSSdCsyZQfNuHw4H4KvYQJ3CIv5Fe0Qf/wnl2KOIxx
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:tLIwiBJB62jlb6f6zB9+B3ptFqk=
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
 by: Geoff Clare - Wed, 11 Jan 2023 13:43 UTC

Kaz Kylheku wrote:

> And, so, continuing in the same vein as my previous posting: we have had
> bidirectional pipes in Unix for decades. The socketpair function creates
> two descriptors and places them into an array, just like pipe. But
> they are a pair of connected sockets: writing to one makes data appear
> in the other and vice versa.
>
> So there are two possibilities:
>
> 1. MacOS invented a new kind of bidirectional pipe that is other than a
> socket pair.

I'm fairly sure it's a true bidirectional pipe, not a socket pair.
However, such things have existed since SVR4 (where they were
STREAMS-based). I think in Solaris they are no longer STREAMS-based
but are still bidirectional.

MacOS got them from FreeBSD, which has had them since 2.2.1 according
to their historical man pages:

www.freebsd.org/cgi/man.cgi?query=pipe&sektion=2&manpath=FreeBSD+2.2.1-RELEASE

> 2. The bidirectional pipe is actually a socket pair.

This would not conform to POSIX, as fstat() would show S_ISSOCK(st_mode)
true instead of S_ISFIFO(st_mode).

--
Geoff Clare <netnews@gclare.org.uk>

Re: popen() and r+

<63bece94$0$25798$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3012&group=comp.unix.programmer#3012

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp2-2.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$george@salle-s.org (Nicolas George)
Subject: Re: popen() and r+
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <tpgnk9$5sq$1@gioia.aioe.org> <a8XuL.575788$GNG9.424370@fx18.iad> <tphh1a$q5j$1@gioia.aioe.org> <20230111024122.143@kylheku.com> <p1r29j-29d.ln1@ID-313840.user.individual.net>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 11 Jan 2023 14:58:28 GMT
Lines: 9
Message-ID: <63bece94$0$25798$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 11 Jan 2023 15:58:28 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1673449108 news-1.free.fr 25798 129.199.129.80:57144
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Wed, 11 Jan 2023 14:58 UTC

Geoff Clare , dans le message
<p1r29j-29d.ln1@ID-313840.user.individual.net>, a écrit :
> I'm fairly sure it's a true bidirectional pipe, not a socket pair.

Note that pipes, unidirectional or bidirectional, are as much pairs than
socket pairs. There is little difference between a stream socket pair and a
bidirectional pipe: maybe socket pairs have a few extra features like
half-close through shutdown() or ancillary data, or maybe they are the same
thing internally.


devel / comp.unix.programmer / popen() and r+

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor