469,572 Members | 1,307 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

open stdin a second time

Hi,

Im opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);
}
pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?

Fernando
Jun 27 '08 #1
16 2647
fbertasso wrote:
Hi,

Im opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);
}
pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?
You *might* be able to record the starting position of
the stdin stream before reading it the first time, and
return to that position to read it again:

fpos_t where;
if (fgetpos(stdin, &where) != 0) {
perror("Can't determine position");
...
}
... read data from stdin ...
if (want_to_read_again) {
if (fgetpos_failed_above) {
fputs("Sorry, Charlie!\n", stderr);
...
}
else if (fsetpos(stdin, &where) != 0) {
perror("Can't restore position");
...
}
... read stdin a second time ...
}

Whether this works or not depends greatly on what kind of
source the stdin stream reads from. Things like disk files
are often "repositionable," while things like keyboards and
sockets and pipes usually are not.

The only way to be certain of having the data available
for a second look is to save it somewhere while reading it
the first time.

--
Er*********@sun.com
Jun 27 '08 #2
fbertasso wrote:
>
.... snip ...
>
I do not want to save the message to a temp file or save it to
memory for i/o questions. Is there a way to re-read stdin ?
No. You have no idea what generated the input on stdin. It might
have been a monkey on the keyboard. It might have been a lightning
bolt somewhere.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #3
On Jun 9, 6:46 pm, fbertasso <fberta...@gmail.comwrote:
Hi,

Im opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);}

pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?

Fernando
Of course fgetpos and fsetpos or other seeking techniques are not
going to work
if you are taking the input from the keyboard. If you are reading from
a file
then ftell/fseek or fgetpos/fsetpos are at you disposable.

To everyone else:
Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?
Jun 27 '08 #4
rahul wrote:
To everyone else:
Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?
DECtape springs immediately to mind. This new-fangled flash memory
which pretends to be a disk hobbles along soon afterward.

Are there reversible paper tape readers?

(Fondly remembering `revInFromFile` ...)

--
"I am afraid that this theory is quite untenable." /The Caves of Steel/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #5
In article <08**********************************@p39g2000prm. googlegroups.com>,
rahul <ra*********@gmail.comwrote:
>Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?
Paper tape, some card readers, drum memory (provided it's refreshed),
magnetic tape in its many varieties, optical tape, CDs and DVDs, flash
memory, RAM "disks".

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #6
Richard Tobin wrote:
In article <08**********************************@p39g2000prm. googlegroups.com>,
rahul <ra*********@gmail.comwrote:
>>Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?

Paper tape, some card readers, drum memory (provided it's refreshed),
magnetic tape in its many varieties, optical tape,
(fx:linebreak)
CDs and DVDs,
Apart from silly marketing tricks, all the CDs and DVDs I've seen
are disks ...

(fx:linebreak)
flash memory, RAM "disks".

--
"I don't make decisions. I'm a bird." /A Fine and Private Place/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 27 '08 #7
In article <g2**********@news-pa1.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>>>Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?
>CDs and DVDs,

Apart from silly marketing tricks, all the CDs and DVDs I've seen
are disks ...
I was thinking of their use as linear storage (dump 0f /dev/acd0 /home),
which makes them "disks" but not "disk files". Of course, by that
reasoning I could have included magnetic disks of all kinds.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #8
On 10 Jun 2008 at 4:43, rahul wrote:
To everyone else: Apart from disk files, is there any other stream
which can be repositioned ( in the sense of seeking)?
Maybe it would be quicker to list the streams that can't be
repositioned... tty, pipes, sockets - any others?

Jun 27 '08 #9
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>Maybe it would be quicker to list the streams that can't be
repositioned... tty, pipes, sockets - any others?
Many physical devices - TV tuners, modems, random number generators...

-- Richard

--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #10
Chris Dollin said:
Richard Tobin wrote:
<snip>
>
>CDs and DVDs,

Apart from silly marketing tricks, all the CDs and DVDs I've seen
are disks ...
No, they're discs. Floppy disks and fixed (hard) disks are disks, but
compact discs and digital versatile discs are discs.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #11
Richard Heathfield wrote:
Chris Dollin said:
>Richard Tobin wrote:
<snip>
>>
>>CDs and DVDs,

Apart from silly marketing tricks, all the CDs and DVDs I've seen
are disks ...

No, they're discs. Floppy disks and fixed (hard) disks are disks, but
compact discs and digital versatile discs are discs.
In my vocabulary, disc and disk are merely alternate spellings, of
which I prefer `disc` but used `disk` above for consistency with
the PPs.

--
"It's a hat as long as I /say/ it's a hat." /A College of Magics/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #12
In article <g2**********@news-pa1.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>Are there reversible paper tape readers?
http://www.freepatentsonline.com/3014092.html
United States Patent 3014092 "High speed reversible tape reader"
(December 19, 1961)
--
"And believe me, I was very lousy yesterday.
I had nothing to say, and, by God, I said it."
-- Walter Wellesley Smith
Jun 27 '08 #13
On Tue, 10 Jun 2008 10:10:13 UTC, Antoninus Twink
<no****@nospam.invalidwrote:
On 10 Jun 2008 at 4:43, rahul wrote:
To everyone else: Apart from disk files, is there any other stream
which can be repositioned ( in the sense of seeking)?

Maybe it would be quicker to list the streams that can't be
repositioned... tty, pipes, sockets - any others?
punch cards, magnetic cards, paper types

There are even devices who are write only:

printers, card writer, plotters, graphic tablets

Some of the above are limited in the ability to be repositioned, some
not, some totally unable depending on the specific type.

There are devices you can read multiple times like disk but write only
once depending on the media inserted:

CD ROM, DVD ROM, EPROM
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
Jun 27 '08 #14
On 10 Jun 2008 08:54:44 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <08**********************************@p39g2000prm. googlegroups.com>,
rahul <ra*********@gmail.comwrote:
Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?

Paper tape, some card readers, drum memory (provided it's refreshed),
I assume this means classic punched or optical-mark; not newer
(plastic) cards like mag-stripe and RFID, mostly for ID, access and
payment rather than data as such. I knew of some readers that could
'catch' a card in the path after reading e.g. for an alternate stacker
but none that could actually go backward. (Or skip forward either.)
magnetic tape in its many varieties, optical tape,
But usually not to arbitrary bytes, thus fitting the fget,setpos model
rather than the fseek (tell/seek) model. Except as noted DECtape.
CDs and DVDs, flash memory, RAM "disks".
- formerly david.thompson1 || achar(64) || worldnet.att.net
Jun 27 '08 #15
In article <9a********************************@4ax.com>,
David Thompson <da************@verizon.netwrote:
>Apart from disk files, is there any other stream which can be
repositioned ( in the sense of seeking)?
>Paper tape, some card readers, drum memory (provided it's refreshed),
>I assume this means classic punched or optical-mark; not newer
(plastic) cards like mag-stripe and RFID, mostly for ID, access and
payment rather than data as such. I knew of some readers that could
'catch' a card in the path after reading e.g. for an alternate stacker
but none that could actually go backward. (Or skip forward either.)
I was thinking of ones that could re-read the whole deck. You don't
have to go backwards to seek.
>magnetic tape in its many varieties, optical tape,

But usually not to arbitrary bytes, thus fitting the fget,setpos model
That's equally true of magnetic disks! The byte-level seeking can be
simulated in software, while the hardware deals in blocks.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #16
In article <9a********************************@4ax.com>,
David Thompson <da************@verizon.netwrote:
>On 10 Jun 2008 08:54:44 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
>Paper tape, some card readers, drum memory (provided it's refreshed),
>I assume this means classic punched or optical-mark; not newer
(plastic) cards like mag-stripe and RFID, mostly for ID, access and
payment rather than data as such. I knew of some readers that could
'catch' a card in the path after reading e.g. for an alternate stacker
but none that could actually go backward. (Or skip forward either.)
A "tub file" perhaps??
http://en.wikipedia.org/wiki/Tub_file
--
"Whenever there is a hard job to be done I assign it to a lazy
man; he is sure to find an easy way of doing it."
-- Walter Chrysler
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Antal Rutz | last post: by
3 posts views Thread by Harayasu | last post: by
11 posts views Thread by Darklight | last post: by
6 posts views Thread by Kevin Simmons | last post: by
24 posts views Thread by Olaf \El Blanco\ | last post: by
8 posts views Thread by Christoph Haas | last post: by
4 posts views Thread by DyslexicAnaboko | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.