4
* All rights reserved. Copyright (C) 1996 by NARITA Tomio.
5
* $Id: stream.c,v 1.5 2003/11/13 03:08:19 nrt Exp $
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <sys/types.h>
44
private byte *gz_filter = "zcat";
45
private byte *bz2_filter = "bzcat";
47
private stream_t *StreamAlloc()
51
st = (stream_t *)Malloc( sizeof( stream_t ) );
60
public stream_t *StreamOpen( byte *file )
63
byte *exts, *filter = NULL;
65
if( access( file, 0 ) ){
72
if( NULL != (exts = Exts( file )) ){
73
if( !strcmp( "gz", exts ) || !strcmp( "GZ", exts )
74
|| !strcmp( "z", exts ) || !strcmp( "Z", exts ) )
76
else if( !strcmp( "bz2", exts ) || !strcmp( "BZ2", exts ) )
83
if( NULL == (st->fp = (FILE *)tmpfile()) )
84
perror( "temporary file" ), exit( -1 );
91
dup( fileno( st->fp ) );
92
if( 0 > spawnlp( 0, filter, filter, file, NULL ) )
105
if( 0 > pipe( fds ) )
106
perror( "pipe" ), exit( -1 );
108
switch( pid = fork() ){
110
perror( "fork" ), exit( -1 );
118
if( 0 > execlp( filter, filter, file, NULL ) )
119
perror( filter ), exit( -1 );
129
if( NULL == (st->sp = fdopen( fds[ 0 ], "r" )) )
130
perror( "fdopen" ), exit( -1 );
138
if( NULL == (st->fp = fopen( file, "rb" )) ){
146
private void StdinDuplicationFailed()
148
fprintf( stderr, "lv: stdin duplication failed\n" );
152
public stream_t *StreamReconnectStdin()
162
if( NULL == (st->fp = fdopen( dup( 0 ), "rb" )) )
163
StdinDuplicationFailed();
169
if( S_IFREG == ( sbuf.st_mode & S_IFMT ) ){
171
if( NULL == (st->fp = fdopen( dup( 0 ), "r" )) )
172
StdinDuplicationFailed();
175
if( NULL == (st->fp = (FILE *)tmpfile()) )
176
perror( "temporary file" ), exit( -1 );
177
if( NULL == (st->sp = fdopen( dup( 0 ), "r" )) )
178
StdinDuplicationFailed();
181
if( IsAtty( 1 ) && 0 != open( "/dev/tty", O_RDONLY ) )
182
perror( "/dev/tty" ), exit( -1 );
188
public boolean_t StreamClose( stream_t *st )