~ubuntu-branches/ubuntu/trusty/lv/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/bts.660358.diff/src/stream.c

  • Committer: Package Import Robot
  • Author(s): HIGUCHI Daisuke (VDR dai)
  • Date: 2014-03-15 03:42:28 UTC
  • Revision ID: package-import@ubuntu.com-20140315034228-6lcwgjiug8rym3wc
Tags: 4.51-2.2
* Non-maintainer upload.
* debian/control, debian/rules, debian/compat: use dh9.
* debian/control
  - add Vcs-* tags.
  - add Homepage: tag.
  - add ${misc:Depends} to Depends:.
  - add xz-utils to Recommends:.
* debian/source/format: set 3.0 (quilt).
* debian/patches/*: rename from debian/patch* and add DEP-3 headers.
* debian/copyright: convert to DEP-5.
* debian/patches/fix-hyphen-used-as-minus-sign.diff: new file.
* debian/lv.doc-base: new file.
* bump up Standards-Version 3.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * stream.c
 
3
 *
 
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 $
 
6
 */
 
7
/*
 
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.
 
12
 *
 
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.
 
17
 *
 
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
 
21
 */
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
 
 
29
#ifdef UNIX
 
30
#include <unistd.h>
 
31
#include <fcntl.h>
 
32
#include <sys/wait.h>
 
33
#endif /* UNIX */
 
34
 
 
35
#ifdef MSDOS
 
36
#include <dos.h>
 
37
#endif /* MSDOS */
 
38
 
 
39
#include <import.h>
 
40
#include <uty.h>
 
41
#include <begin.h>
 
42
#include "stream.h"
 
43
 
 
44
private byte *gz_filter = "zcat";
 
45
private byte *bz2_filter = "bzcat";
 
46
 
 
47
private stream_t *StreamAlloc()
 
48
{
 
49
  stream_t *st;
 
50
 
 
51
  st = (stream_t *)Malloc( sizeof( stream_t ) );
 
52
 
 
53
  st->fp  = NULL;
 
54
  st->sp  = NULL;
 
55
  st->pid = -1;
 
56
 
 
57
  return st;
 
58
}
 
59
 
 
60
public stream_t *StreamOpen( byte *file )
 
61
{
 
62
  stream_t *st;
 
63
  byte *exts, *filter = NULL;
 
64
 
 
65
  if( access( file, 0 ) ){
 
66
    perror( file );
 
67
    return NULL;
 
68
  }
 
69
 
 
70
  st = StreamAlloc();
 
71
 
 
72
  if( NULL != (exts = Exts( file )) ){
 
73
    if( !strcmp( "gz", exts ) || !strcmp( "GZ", exts )
 
74
        || !strcmp( "z", exts ) || !strcmp( "Z", exts ) )
 
75
      filter = gz_filter;
 
76
    else if( !strcmp( "bz2", exts ) || !strcmp( "BZ2", exts ) )
 
77
      filter = bz2_filter;
 
78
  }
 
79
  if( NULL != filter ){
 
80
    /*
 
81
     * zcat or bzcat
 
82
     */
 
83
    if( NULL == (st->fp = (FILE *)tmpfile()) )
 
84
      perror( "temporary file" ), exit( -1 );
 
85
 
 
86
#ifdef MSDOS
 
87
    { int sout;
 
88
 
 
89
      sout = dup( 1 );
 
90
      close( 1 );
 
91
      dup( fileno( st->fp ) );
 
92
      if( 0 > spawnlp( 0, filter, filter, file, NULL ) )
 
93
        FatalErrorOccurred();
 
94
      close( 1 );
 
95
      dup( sout );
 
96
      rewind( st->fp );
 
97
 
 
98
      return st;
 
99
    }
 
100
#endif /* MSDOS */
 
101
 
 
102
#ifdef UNIX
 
103
    { int fds[ 2 ], pid;
 
104
 
 
105
      if( 0 > pipe( fds ) )
 
106
        perror( "pipe" ), exit( -1 );
 
107
 
 
108
      switch( pid = fork() ){
 
109
      case -1:
 
110
        perror( "fork" ), exit( -1 );
 
111
      case 0:
 
112
        /*
 
113
         * child process
 
114
         */
 
115
        close( fds[ 0 ] );
 
116
        close( 1 );
 
117
        dup( fds[ 1 ] );
 
118
        if( 0 > execlp( filter, filter, file, NULL ) )
 
119
          perror( filter ), exit( -1 );
 
120
        /*
 
121
         * never reach here
 
122
         */
 
123
      default:
 
124
        /*
 
125
         * parent process
 
126
         */
 
127
        st->pid = pid;
 
128
        close( fds[ 1 ] );
 
129
        if( NULL == (st->sp = fdopen( fds[ 0 ], "r" )) )
 
130
          perror( "fdopen" ), exit( -1 );
 
131
 
 
132
        return st;
 
133
      }
 
134
    }
 
135
#endif /* UNIX */
 
136
  }
 
137
 
 
138
  if( NULL == (st->fp = fopen( file, "rb" )) ){
 
139
    perror( file );
 
140
    return NULL;
 
141
  }
 
142
 
 
143
  return st;
 
144
}
 
145
 
 
146
private void StdinDuplicationFailed()
 
147
{
 
148
  fprintf( stderr, "lv: stdin duplication failed\n" );
 
149
  exit( -1 );
 
150
}
 
151
 
 
152
public stream_t *StreamReconnectStdin()
 
153
{
 
154
  stream_t *st;
 
155
#ifdef UNIX
 
156
  struct stat sbuf;
 
157
#endif
 
158
 
 
159
  st = StreamAlloc();
 
160
 
 
161
#ifdef MSDOS
 
162
  if( NULL == (st->fp = fdopen( dup( 0 ), "rb" )) )
 
163
    StdinDuplicationFailed();
 
164
  close( 0 );
 
165
  dup( 1 );
 
166
#endif /* MSDOS */
 
167
#ifdef UNIX
 
168
  fstat( 0, &sbuf );
 
169
  if( S_IFREG == ( sbuf.st_mode & S_IFMT ) ){
 
170
    /* regular */
 
171
    if( NULL == (st->fp = fdopen( dup( 0 ), "r" )) )
 
172
      StdinDuplicationFailed();
 
173
  } else {
 
174
    /* socket */
 
175
    if( NULL == (st->fp = (FILE *)tmpfile()) )
 
176
      perror( "temporary file" ), exit( -1 );
 
177
    if( NULL == (st->sp = fdopen( dup( 0 ), "r" )) )
 
178
      StdinDuplicationFailed();
 
179
  }
 
180
  close( 0 );
 
181
  if( IsAtty( 1 ) && 0 != open( "/dev/tty", O_RDONLY ) )
 
182
    perror( "/dev/tty" ), exit( -1 );
 
183
#endif /* UNIX */
 
184
 
 
185
  return st;
 
186
}
 
187
 
 
188
public boolean_t StreamClose( stream_t *st )
 
189
{
 
190
  fclose( st->fp );
 
191
  if( st->sp )
 
192
    fclose( st->fp );
 
193
 
 
194
  free( st );
 
195
 
 
196
  return TRUE;
 
197
}