1
/* iobuf.c - file handling
2
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
3
* 2004 Free Software Foundation, Inc.
5
* This file is part of GnuPG.
7
* GnuPG is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GnuPG is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
29
#include <sys/types.h>
33
#ifdef HAVE_DOSISH_SYSTEM
39
#endif /* __riscos__ */
45
#undef FILE_FILTER_USES_STDIO
47
#ifdef HAVE_DOSISH_SYSTEM
51
#ifdef FILE_FILTER_USES_STDIO
52
#define my_fileno(a) fileno ((a))
53
#define my_fopen_ro(a,b) fopen ((a),(b))
54
#define my_fopen(a,b) fopen ((a),(b))
55
typedef FILE *FILEP_OR_FD;
56
#define INVALID_FP NULL
57
#define FILEP_OR_FD_FOR_STDIN (stdin)
58
#define FILEP_OR_FD_FOR_STDOUT (stdout)
60
FILE *fp; /* open file handle */
63
int print_only_name; /* flags indicating that fname is not a real file*/
64
char fname[1]; /* name of the file */
67
#define my_fileno(a) (a)
68
#define my_fopen_ro(a,b) fd_cache_open ((a),(b))
69
#define my_fopen(a,b) direct_open ((a),(b))
70
#ifdef HAVE_DOSISH_SYSTEM
71
typedef HANDLE FILEP_OR_FD;
72
#define INVALID_FP ((HANDLE)-1)
73
#define FILEP_OR_FD_FOR_STDIN (GetStdHandle (STD_INPUT_HANDLE))
74
#define FILEP_OR_FD_FOR_STDOUT (GetStdHandle (STD_OUTPUT_HANDLE))
77
typedef int FILEP_OR_FD;
78
#define INVALID_FP (-1)
79
#define FILEP_OR_FD_FOR_STDIN (0)
80
#define FILEP_OR_FD_FOR_STDOUT (1)
83
FILEP_OR_FD fp; /* open file handle */
87
int print_only_name; /* flags indicating that fname is not a real file*/
88
char fname[1]; /* name of the file */
91
struct close_cache_s {
92
struct close_cache_s *next;
96
typedef struct close_cache_s *CLOSE_CACHE;
97
static CLOSE_CACHE close_cache;
106
int print_only_name; /* flags indicating that fname is not a real file*/
107
char fname[1]; /* name of the file */
108
} sock_filter_ctx_t ;
111
/* The first partial length header block must be of size 512
112
* to make it easier (and efficienter) we use a min. block size of 512
113
* for all chunks (but the last one) */
114
#define OP_MIN_PARTIAL_CHUNK 512
115
#define OP_MIN_PARTIAL_CHUNK_2POW 9
121
int partial; /* 1 = partial header, 2 in last partial packet */
122
char *buffer; /* used for partial header */
123
size_t buflen; /* used size of buffer */
124
int first_c; /* of partial header (which is > 0)*/
126
} block_filter_ctx_t;
128
static int special_names_enabled;
130
static int underflow(IOBUF a);
131
static int translate_file_handle ( int fd, int for_write );
135
#ifndef FILE_FILTER_USES_STDIO
137
/* This is a replacement for strcmp. Under W32 it does not
138
distinguish between backslash and slash. */
140
fd_cache_strcmp (const char *a, const char *b)
142
#ifdef HAVE_DOSISH_SYSTEM
143
for (; *a && *b; a++, b++)
145
if (*a != *b && !((*a == '/' && *b == '\\')
146
|| (*a == '\\' && *b == '/')) )
149
return *(const unsigned char *)a - *(const unsigned char *)b;
151
return strcmp (a, b);
156
* Invalidate (i.e. close) a cached iobuf
159
fd_cache_invalidate (const char *fname)
165
log_debug ("fd_cache_invalidate (%s)\n", fname);
167
for (cc=close_cache; cc; cc = cc->next ) {
168
if ( cc->fp != INVALID_FP && !fd_cache_strcmp (cc->fname, fname) ) {
170
log_debug (" did (%s)\n", cc->fname);
171
#ifdef HAVE_DOSISH_SYSTEM
172
CloseHandle (cc->fp);
184
direct_open (const char *fname, const char *mode)
186
#ifdef HAVE_DOSISH_SYSTEM
187
unsigned long da, cd, sm;
190
/* Note, that we do not handle all mode combinations */
192
/* According to the ReactOS source it seems that open() of the
193
* standard MSW32 crt does open the file in share mode which is
194
* something new for MS applications ;-)
196
if ( strchr (mode, '+') ) {
197
fd_cache_invalidate (fname);
198
da = GENERIC_READ|GENERIC_WRITE;
200
sm = FILE_SHARE_READ | FILE_SHARE_WRITE;
202
else if ( strchr (mode, 'w') ) {
203
fd_cache_invalidate (fname);
206
sm = FILE_SHARE_WRITE;
211
sm = FILE_SHARE_READ;
214
hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL);
218
int cflag = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
220
/* Note, that we do not handle all mode combinations */
221
if ( strchr (mode, '+') ) {
222
fd_cache_invalidate (fname);
225
else if ( strchr (mode, 'w') ) {
226
fd_cache_invalidate (fname);
227
oflag = O_WRONLY | O_CREAT | O_TRUNC;
233
if (strchr (mode, 'b'))
237
return open (fname, oflag, cflag );
241
int rc = stat( fname, &buf );
243
/* Don't allow iobufs on directories */
244
if( !rc && S_ISDIR(buf.st_mode) && !S_ISREG(buf.st_mode) )
245
return __set_errno( EISDIR );
247
return open( fname, oflag, cflag );
255
* Instead of closing an FD we keep it open and cache it for later reuse
256
* Note that this caching strategy only works if the process does not chdir.
259
fd_cache_close (const char *fname, FILEP_OR_FD fp)
264
if ( !fname || !*fname ) {
265
#ifdef HAVE_DOSISH_SYSTEM
271
log_debug ("fd_cache_close (%p) real\n", (void*)fp);
274
/* try to reuse a slot */
275
for (cc=close_cache; cc; cc = cc->next ) {
276
if ( cc->fp == INVALID_FP && !fd_cache_strcmp (cc->fname, fname) ) {
279
log_debug ("fd_cache_close (%s) used existing slot\n", fname);
285
log_debug ("fd_cache_close (%s) new slot created\n", fname);
286
cc = m_alloc_clear (sizeof *cc + strlen (fname));
287
strcpy (cc->fname, fname);
289
cc->next = close_cache;
294
* Do an direct_open on FNAME but first try to reuse one from the fd_cache
297
fd_cache_open (const char *fname, const char *mode)
302
for (cc=close_cache; cc; cc = cc->next ) {
303
if ( cc->fp != INVALID_FP && !fd_cache_strcmp (cc->fname, fname) ) {
304
FILEP_OR_FD fp = cc->fp;
307
log_debug ("fd_cache_open (%s) using cached fp\n", fname);
308
#ifdef HAVE_DOSISH_SYSTEM
309
if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff ) {
310
log_error ("rewind file failed on handle %p: %s\n",
311
fp, w32_strerror (errno));
315
if ( lseek (fp, 0, SEEK_SET) == (off_t)-1 ) {
316
log_error("can't rewind fd %d: %s\n", fp, strerror(errno) );
324
log_debug ("fd_cache_open (%s) not cached\n", fname);
325
return direct_open (fname, mode);
329
#endif /*FILE_FILTER_USES_STDIO*/
333
* Read data from a file into buf which has an allocated length of *LEN.
334
* return the number of read bytes in *LEN. OPAQUE is the FILE * of
335
* the stream. A is not used.
337
* IOBUFCTRL_INIT: called just before the function is linked into the
338
* list of function. This can be used to prepare internal
339
* data structures of the function.
340
* IOBUFCTRL_FREE: called just before the function is removed from the
341
* list of functions and can be used to release internal
342
* data structures or close a file etc.
343
* IOBUFCTRL_UNDERFLOW: called by iobuf_underflow to fill the buffer
344
* with new stuff. *RET_LEN is the available size of the
345
* buffer, and should be set to the number of bytes
346
* which were put into the buffer. The function
347
* returns 0 to indicate success, -1 on EOF and
348
* G10ERR_xxxxx for other errors.
350
* IOBUFCTRL_FLUSH: called by iobuf_flush() to write out the collected stuff.
351
* *RET_LAN is the number of bytes in BUF.
353
* IOBUFCTRL_CANCEL: send to all filters on behalf of iobuf_cancel. The
354
* filter may take appropriate action on this message.
357
file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
359
file_filter_ctx_t *a = opaque;
360
FILEP_OR_FD f = a->fp;
361
size_t size = *ret_len;
365
#ifdef FILE_FILTER_USES_STDIO
366
if( control == IOBUFCTRL_UNDERFLOW ) {
367
assert( size ); /* need a buffer */
368
if ( feof(f)) { /* On terminals you could easiely read as many EOFs as you call */
369
rc = -1; /* fread() or fgetc() repeatly. Every call will block until you press */
370
*ret_len = 0; /* CTRL-D. So we catch this case before we call fread() again. */
374
nbytes = fread( buf, 1, size, f );
375
if( feof(f) && !nbytes ) {
376
rc = -1; /* okay: we can return EOF now. */
378
else if( ferror(f) && errno != EPIPE ) {
379
log_error("%s: read error: %s\n",
380
a->fname, strerror(errno));
381
rc = G10ERR_READ_FILE;
386
else if( control == IOBUFCTRL_FLUSH ) {
389
nbytes = fwrite( buf, 1, size, f );
391
log_error("%s: write error: %s\n", a->fname, strerror(errno));
392
rc = G10ERR_WRITE_FILE;
397
else if( control == IOBUFCTRL_INIT ) {
398
a->keep_open = a->no_cache = 0;
400
else if( control == IOBUFCTRL_DESC ) {
401
*(char**)buf = "file_filter";
403
else if( control == IOBUFCTRL_FREE ) {
404
if( f != stdin && f != stdout ) {
406
log_debug("%s: close fd %d\n", a->fname, fileno(f) );
411
m_free(a); /* we can free our context now */
413
#else /* !stdio implementation */
415
if( control == IOBUFCTRL_UNDERFLOW ) {
416
assert( size ); /* need a buffer */
422
#ifdef HAVE_DOSISH_SYSTEM
426
if ( !ReadFile ( f, buf, size, &nread, NULL ) ) {
427
if ((int)GetLastError () != ERROR_BROKEN_PIPE) {
428
log_error ("%s: read error: %s\n", a->fname,
430
rc = G10ERR_READ_FILE;
447
n = read ( f, buf, size );
448
} while (n == -1 && errno == EINTR );
449
if ( n == -1 ) { /* error */
450
if (errno != EPIPE) {
451
log_error("%s: read error: %s\n",
452
a->fname, strerror(errno));
453
rc = G10ERR_READ_FILE;
456
else if ( !n ) { /* eof */
467
else if( control == IOBUFCTRL_FLUSH ) {
469
#ifdef HAVE_DOSISH_SYSTEM
475
if (size && !WriteFile (f, p, nbytes, &n, NULL)) {
476
log_error ("%s: write error: %s\n", a->fname,
478
rc = G10ERR_WRITE_FILE;
492
n = write ( f, p, nbytes );
493
} while ( n == -1 && errno == EINTR );
498
} while ( n != -1 && nbytes );
500
log_error("%s: write error: %s\n", a->fname, strerror(errno));
501
rc = G10ERR_WRITE_FILE;
508
else if ( control == IOBUFCTRL_INIT ) {
513
else if ( control == IOBUFCTRL_DESC ) {
514
*(char**)buf = "file_filter(fd)";
516
else if ( control == IOBUFCTRL_FREE ) {
517
#ifdef HAVE_DOSISH_SYSTEM
518
if ( f != FILEP_OR_FD_FOR_STDIN && f != FILEP_OR_FD_FOR_STDOUT ) {
520
log_debug("%s: close handle %p\n", a->fname, f );
522
fd_cache_close (a->no_cache?NULL:a->fname, f);
525
if ( (int)f != 0 && (int)f != 1 ) {
527
log_debug("%s: close fd %d\n", a->fname, f );
529
fd_cache_close (a->no_cache?NULL:a->fname, f);
533
m_free (a); /* we can free our context now */
535
#endif /* !stdio implementation */
540
/* Becuase sockets are an special object under Lose32 we have to
541
* use a special filter */
543
sock_filter (void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
545
sock_filter_ctx_t *a = opaque;
546
size_t size = *ret_len;
550
if( control == IOBUFCTRL_UNDERFLOW ) {
551
assert( size ); /* need a buffer */
559
nread = recv ( a->sock, buf, size, 0 );
560
if ( nread == SOCKET_ERROR ) {
561
int ec = (int)WSAGetLastError ();
562
log_error("socket read error: ec=%d\n", ec);
563
rc = G10ERR_READ_FILE;
575
else if( control == IOBUFCTRL_FLUSH ) {
582
n = send (a->sock, p, nbytes, 0);
583
if ( n == SOCKET_ERROR ) {
584
int ec = (int)WSAGetLastError ();
585
log_error("socket write error: ec=%d\n", ec);
586
rc = G10ERR_WRITE_FILE;
596
else if ( control == IOBUFCTRL_INIT ) {
601
else if ( control == IOBUFCTRL_DESC ) {
602
*(char**)buf = "sock_filter";
604
else if ( control == IOBUFCTRL_FREE ) {
606
closesocket (a->sock);
607
m_free (a); /* we can free our context now */
614
* This is used to implement the block write mode.
615
* Block reading is done on a byte by byte basis in readbyte(),
619
block_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
621
block_filter_ctx_t *a = opaque;
622
size_t size = *ret_len;
623
int c, needed, rc = 0;
626
if( control == IOBUFCTRL_UNDERFLOW ) {
630
assert( size ); /* need a buffer */
631
if( a->eof ) /* don't read any further */
633
while( !rc && size ) {
634
if( !a->size ) { /* get the length bytes */
635
if( a->partial == 2 ) {
641
else if( a->partial ) {
642
/* These OpenPGP introduced huffman like encoded length
643
* bytes are really a mess :-( */
648
else if( (c = iobuf_get(chain)) == -1 ) {
649
log_error("block_filter: 1st length byte missing\n");
650
rc = G10ERR_READ_FILE;
664
a->size = (c - 192) * 256;
665
if( (c = iobuf_get(chain)) == -1 ) {
666
log_error("block_filter: 2nd length byte missing\n");
667
rc = G10ERR_READ_FILE;
679
else if( c == 255 ) {
680
a->size = iobuf_get(chain) << 24;
681
a->size |= iobuf_get(chain) << 16;
682
a->size |= iobuf_get(chain) << 8;
683
if( (c = iobuf_get(chain)) == -1 ) {
684
log_error("block_filter: invalid 4 byte length\n");
685
rc = G10ERR_READ_FILE;
697
else { /* next partial body length */
698
a->size = 1 << (c & 0x1f);
700
/* log_debug("partial: ctx=%p c=%02x size=%u\n", a, c, a->size);*/
706
while( !rc && size && a->size ) {
707
needed = size < a->size ? size : a->size;
708
c = iobuf_read( chain, p, needed );
711
log_error("block_filter %p: read error (size=%lu,a->size=%lu)\n",
712
a, (ulong)size+c, (ulong)a->size+c);
713
rc = G10ERR_READ_FILE;
725
else if( control == IOBUFCTRL_FLUSH ) {
726
if( a->partial ) { /* the complicated openpgp scheme */
727
size_t blen, n, nbytes = size + a->buflen;
729
assert( a->buflen <= OP_MIN_PARTIAL_CHUNK );
730
if( nbytes < OP_MIN_PARTIAL_CHUNK ) {
731
/* not enough to write a partial block out; so we store it*/
733
a->buffer = m_alloc( OP_MIN_PARTIAL_CHUNK );
734
memcpy( a->buffer + a->buflen, buf, size );
737
else { /* okay, we can write out something */
738
/* do this in a loop to use the most efficient block lengths */
741
/* find the best matching block length - this is limited
742
* by the size of the internal buffering */
743
for( blen=OP_MIN_PARTIAL_CHUNK*2,
744
c=OP_MIN_PARTIAL_CHUNK_2POW+1; blen <= nbytes;
748
/* write the partial length header */
749
assert( c <= 0x1f ); /*;-)*/
751
iobuf_put( chain, c );
752
if( (n=a->buflen) ) { /* write stuff from the buffer */
753
assert( n == OP_MIN_PARTIAL_CHUNK);
754
if( iobuf_write(chain, a->buffer, n ) )
755
rc = G10ERR_WRITE_FILE;
759
if( (n = nbytes) > blen )
761
if( n && iobuf_write(chain, p, n ) )
762
rc = G10ERR_WRITE_FILE;
765
} while( !rc && nbytes >= OP_MIN_PARTIAL_CHUNK );
766
/* store the rest in the buffer */
767
if( !rc && nbytes ) {
768
assert( !a->buflen );
769
assert( nbytes < OP_MIN_PARTIAL_CHUNK );
771
a->buffer = m_alloc( OP_MIN_PARTIAL_CHUNK );
772
memcpy( a->buffer, p, nbytes );
780
else if( control == IOBUFCTRL_INIT ) {
782
log_debug("init block_filter %p\n", a );
785
else if( a->use == 1 )
786
a->count = a->size = 0;
788
a->count = a->size; /* force first length bytes */
793
else if( control == IOBUFCTRL_DESC ) {
794
*(char**)buf = "block_filter";
796
else if( control == IOBUFCTRL_FREE ) {
797
if( a->use == 2 ) { /* write the end markers */
800
/* write out the remaining bytes without a partial header
801
* the length of this header may be 0 - but if it is
802
* the first block we are not allowed to use a partial header
803
* and frankly we can't do so, because this length must be
804
* a power of 2. This is _really_ complicated because we
805
* have to check the possible length of a packet prior
806
* to it's creation: a chain of filters becomes complicated
807
* and we need a lot of code to handle compressed packets etc.
810
/* construct header */
812
/*log_debug("partial: remaining length=%u\n", len );*/
814
rc = iobuf_put(chain, len );
815
else if( len < 8384 ) {
816
if( !(rc=iobuf_put( chain, ((len-192) / 256) + 192)) )
817
rc = iobuf_put( chain, ((len-192) % 256));
819
else { /* use a 4 byte header */
820
if( !(rc=iobuf_put( chain, 0xff )) )
821
if( !(rc=iobuf_put( chain, (len >> 24)&0xff )) )
822
if( !(rc=iobuf_put( chain, (len >> 16)&0xff )) )
823
if( !(rc=iobuf_put( chain, (len >> 8)&0xff )))
824
rc=iobuf_put( chain, len & 0xff );
827
rc = iobuf_write(chain, a->buffer, len );
829
log_error("block_filter: write error: %s\n",strerror(errno));
830
rc = G10ERR_WRITE_FILE;
832
m_free( a->buffer ); a->buffer = NULL; a->buflen = 0;
838
log_error("block_filter: pending bytes!\n");
841
log_debug("free block_filter %p\n", a );
842
m_free(a); /* we can free our context now */
850
print_chain( IOBUF a )
854
for(; a; a = a->chain ) {
855
size_t dummy_len = 0;
856
const char *desc = "[none]";
859
a->filter( a->filter_ov, IOBUFCTRL_DESC, NULL,
860
(byte*)&desc, &dummy_len );
862
log_debug("iobuf chain: %d.%d `%s' filter_eof=%d start=%d len=%d\n",
863
a->no, a->subno, desc, a->filter_eof,
864
(int)a->d.start, (int)a->d.len );
869
iobuf_print_chain( IOBUF a )
876
* Allocate a new io buffer, with no function assigned.
877
* Use is the desired usage: 1 for input, 2 for output, 3 for temp buffer
878
* BUFSIZE is a suggested buffer size.
881
iobuf_alloc(int use, size_t bufsize)
886
a = m_alloc_clear(sizeof *a);
888
a->d.buf = m_alloc( bufsize );
893
a->real_fname = NULL;
898
iobuf_close ( IOBUF a )
904
if( a && a->directfp ) {
905
fclose( a->directfp );
906
m_free( a->real_fname );
908
log_debug("iobuf_close -> %p\n", a->directfp );
912
for( ; a && !rc ; a = a2 ) {
914
if( a->use == 2 && (rc=iobuf_flush(a)) )
915
log_error("iobuf_flush failed on close: %s\n", g10_errstr(rc));
918
log_debug("iobuf-%d.%d: close `%s'\n", a->no, a->subno, a->desc );
919
if( a->filter && (rc = a->filter(a->filter_ov, IOBUFCTRL_FREE,
920
a->chain, NULL, &dummy_len)) )
921
log_error("IOBUFCTRL_FREE failed on close: %s\n", g10_errstr(rc) );
922
m_free(a->real_fname);
924
memset (a->d.buf, 0, a->d.size); /* erase the buffer */
933
iobuf_cancel( IOBUF a )
938
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
939
char *remove_name = NULL;
942
if( a && a->use == 2 ) {
943
s = iobuf_get_real_fname(a);
945
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
946
remove_name = m_strdup ( s );
953
/* send a cancel message to all filters */
954
for( a2 = a; a2 ; a2 = a2->chain ) {
957
a2->filter( a2->filter_ov, IOBUFCTRL_CANCEL, a2->chain,
962
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
964
/* Argg, MSDOS does not allow to remove open files. So
965
* we have to do it here */
966
remove ( remove_name );
967
m_free ( remove_name );
975
* create a temporary iobuf, which can be used to collect stuff
976
* in an iobuf and later be written by iobuf_write_temp() to another
984
a = iobuf_alloc(3, 8192 );
990
iobuf_temp_with_content( const char *buffer, size_t length )
994
a = iobuf_alloc(3, length );
995
memcpy( a->d.buf, buffer, length );
1002
iobuf_enable_special_filenames ( int yes )
1004
special_names_enabled = yes;
1008
* see whether the filename has the for "-&nnnn", where n is a
1010
* Returns this number or -1 if it is not the case.
1013
check_special_filename ( const char *fname )
1015
if ( special_names_enabled
1016
&& fname && *fname == '-' && fname[1] == '&' ) {
1020
for (i=0; digitp (fname+i); i++ )
1023
return atoi (fname);
1028
/* This fucntion returns true if FNAME indicates a PIPE (stdout or
1029
stderr) or a special file name if those are enabled. */
1031
iobuf_is_pipe_filename (const char *fname)
1033
if (!fname || (*fname=='-' && !fname[1]) )
1035
return check_special_filename (fname) != -1;
1039
* Create a head iobuf for reading from a file
1040
* returns: NULL if an error occures and sets errno
1043
iobuf_open( const char *fname )
1047
file_filter_ctx_t *fcx;
1052
if( !fname || (*fname=='-' && !fname[1]) ) {
1053
fp = FILEP_OR_FD_FOR_STDIN;
1055
setmode ( my_fileno(fp) , O_BINARY );
1060
else if ( (fd = check_special_filename ( fname )) != -1 )
1061
return iobuf_fdopen ( translate_file_handle (fd,0), "rb" );
1062
else if( (fp = my_fopen_ro(fname, "rb")) == INVALID_FP )
1064
a = iobuf_alloc(1, 8192 );
1065
fcx = m_alloc( sizeof *fcx + strlen(fname) );
1067
fcx->print_only_name = print_only;
1068
strcpy(fcx->fname, fname );
1070
a->real_fname = m_strdup( fname );
1071
a->filter = file_filter;
1073
file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1074
file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len );
1076
log_debug("iobuf-%d.%d: open `%s' fd=%d\n",
1077
a->no, a->subno, fname, (int)my_fileno(fcx->fp) );
1083
* Create a head iobuf for reading from a file
1084
* returns: NULL if an error occures and sets errno
1087
iobuf_fdopen( int fd, const char *mode )
1091
file_filter_ctx_t *fcx;
1094
#ifdef FILE_FILTER_USES_STDIO
1095
if( !(fp = fdopen(fd, mode)) )
1098
fp = (FILEP_OR_FD)fd;
1100
a = iobuf_alloc( strchr( mode, 'w')? 2:1, 8192 );
1101
fcx = m_alloc( sizeof *fcx + 20 );
1103
fcx->print_only_name = 1;
1104
sprintf(fcx->fname, "[fd %d]", fd );
1105
a->filter = file_filter;
1107
file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1108
file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len );
1110
log_debug("iobuf-%d.%d: fdopen `%s'\n", a->no, a->subno, fcx->fname );
1111
iobuf_ioctl (a,3,1,NULL); /* disable fd caching */
1117
iobuf_sockopen ( int fd, const char *mode )
1121
sock_filter_ctx_t *scx;
1124
a = iobuf_alloc( strchr( mode, 'w')? 2:1, 8192 );
1125
scx = m_alloc( sizeof *scx + 25 );
1127
scx->print_only_name = 1;
1128
sprintf(scx->fname, "[sock %d]", fd );
1129
a->filter = sock_filter;
1131
sock_filter( scx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1132
sock_filter( scx, IOBUFCTRL_INIT, NULL, NULL, &len );
1134
log_debug("iobuf-%d.%d: sockopen `%s'\n", a->no, a->subno, scx->fname);
1135
iobuf_ioctl (a,3,1,NULL); /* disable fd caching */
1137
a = iobuf_fdopen (fd, mode);
1143
* create an iobuf for writing to a file; the file will be created.
1146
iobuf_create( const char *fname )
1150
file_filter_ctx_t *fcx;
1155
if( !fname || (*fname=='-' && !fname[1]) ) {
1156
fp = FILEP_OR_FD_FOR_STDOUT;
1158
setmode ( my_fileno(fp) , O_BINARY );
1163
else if ( (fd = check_special_filename ( fname )) != -1 )
1164
return iobuf_fdopen ( translate_file_handle (fd, 1), "wb" );
1165
else if( (fp = my_fopen(fname, "wb")) == INVALID_FP )
1167
a = iobuf_alloc(2, 8192 );
1168
fcx = m_alloc( sizeof *fcx + strlen(fname) );
1170
fcx->print_only_name = print_only;
1171
strcpy(fcx->fname, fname );
1173
a->real_fname = m_strdup( fname );
1174
a->filter = file_filter;
1176
file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1177
file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len );
1179
log_debug("iobuf-%d.%d: create `%s'\n", a->no, a->subno, a->desc );
1185
* append to an iobuf; if the file does not exist, create it.
1186
* cannot be used for stdout.
1187
* Note: This is not used.
1189
#if 0 /* not used */
1191
iobuf_append( const char *fname )
1195
file_filter_ctx_t *fcx;
1200
else if( !(fp = my_fopen(fname, "ab")) )
1202
a = iobuf_alloc(2, 8192 );
1203
fcx = m_alloc( sizeof *fcx + strlen(fname) );
1205
strcpy(fcx->fname, fname );
1206
a->real_fname = m_strdup( fname );
1207
a->filter = file_filter;
1209
file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1210
file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len );
1212
log_debug("iobuf-%d.%d: append `%s'\n", a->no, a->subno, a->desc );
1219
iobuf_openrw( const char *fname )
1223
file_filter_ctx_t *fcx;
1228
else if( (fp = my_fopen(fname, "r+b")) == INVALID_FP )
1230
a = iobuf_alloc(2, 8192 );
1231
fcx = m_alloc( sizeof *fcx + strlen(fname) );
1233
strcpy(fcx->fname, fname );
1234
a->real_fname = m_strdup( fname );
1235
a->filter = file_filter;
1237
file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
1238
file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len );
1240
log_debug("iobuf-%d.%d: openrw `%s'\n", a->no, a->subno, a->desc );
1247
iobuf_ioctl ( IOBUF a, int cmd, int intval, void *ptrval )
1249
if ( cmd == 1 ) { /* keep system filepointer/descriptor open */
1251
log_debug("iobuf-%d.%d: ioctl `%s' keep=%d\n",
1252
a? a->no:-1, a?a->subno:-1, a?a->desc:"?", intval );
1253
for( ; a; a = a->chain )
1254
if( !a->chain && a->filter == file_filter ) {
1255
file_filter_ctx_t *b = a->filter_ov;
1256
b->keep_open = intval;
1260
else if( !a->chain && a->filter == sock_filter ) {
1261
sock_filter_ctx_t *b = a->filter_ov;
1262
b->keep_open = intval;
1267
else if ( cmd == 2 ) { /* invalidate cache */
1269
log_debug("iobuf-*.*: ioctl `%s' invalidate\n",
1270
ptrval? (char*)ptrval:"?");
1271
if ( !a && !intval && ptrval ) {
1272
#ifndef FILE_FILTER_USES_STDIO
1273
fd_cache_invalidate (ptrval);
1278
else if ( cmd == 3 ) { /* disallow/allow caching */
1280
log_debug("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
1281
a? a->no:-1, a?a->subno:-1, a?a->desc:"?", intval );
1282
for( ; a; a = a->chain )
1283
if( !a->chain && a->filter == file_filter ) {
1284
file_filter_ctx_t *b = a->filter_ov;
1285
b->no_cache = intval;
1289
else if( !a->chain && a->filter == sock_filter ) {
1290
sock_filter_ctx_t *b = a->filter_ov;
1291
b->no_cache = intval;
1302
* Register an i/o filter.
1305
iobuf_push_filter( IOBUF a,
1306
int (*f)(void *opaque, int control,
1307
IOBUF chain, byte *buf, size_t *len), void *ov )
1309
return iobuf_push_filter2( a, f, ov, 0 );
1313
iobuf_push_filter2( IOBUF a,
1314
int (*f)(void *opaque, int control,
1315
IOBUF chain, byte *buf, size_t *len),
1316
void *ov, int rel_ov )
1325
if( a->use == 2 && (rc=iobuf_flush(a)) )
1327
/* make a copy of the current stream, so that
1328
* A is the new stream and B the original one.
1329
* The contents of the buffers are transferred to the
1332
b = m_alloc(sizeof *b);
1333
memcpy(b, a, sizeof *b );
1334
/* fixme: it is stupid to keep a copy of the name at every level
1335
* but we need the name somewhere because the name known by file_filter
1336
* may have been released when we need the name of the file */
1337
b->real_fname = a->real_fname? m_strdup(a->real_fname):NULL;
1338
/* remove the filter stuff from the new stream */
1340
a->filter_ov = NULL;
1341
a->filter_ov_owner = 0;
1344
a->use = 2; /* make a write stream from a temp stream */
1346
if( a->use == 2 ) { /* allocate a fresh buffer for the original stream */
1347
b->d.buf = m_alloc( a->d.size );
1351
else { /* allocate a fresh buffer for the new stream */
1352
a->d.buf = m_alloc( a->d.size );
1356
/* disable nlimit for the new stream */
1357
a->ntotal = b->ntotal + b->nbytes;
1358
a->nlimit = a->nbytes = 0;
1360
/* make a link from the new stream to the original stream */
1362
a->opaque = b->opaque;
1364
/* setup the function on the new stream */
1367
a->filter_ov_owner = rel_ov;
1369
a->subno = b->subno + 1;
1370
f( ov, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &dummy_len );
1373
log_debug("iobuf-%d.%d: push `%s'\n", a->no, a->subno, a->desc );
1377
/* now we can initialize the new function if we have one */
1378
if( a->filter && (rc = a->filter(a->filter_ov, IOBUFCTRL_INIT, a->chain,
1379
NULL, &dummy_len)) )
1380
log_error("IOBUFCTRL_INIT failed: %s\n", g10_errstr(rc) );
1385
* Remove an i/o filter.
1388
pop_filter( IOBUF a, int (*f)(void *opaque, int control,
1389
IOBUF chain, byte *buf, size_t *len), void *ov )
1399
log_debug("iobuf-%d.%d: pop `%s'\n", a->no, a->subno, a->desc );
1400
if( !a->filter ) { /* this is simple */
1404
m_free(a->real_fname);
1405
memcpy(a,b, sizeof *a);
1409
for(b=a ; b; b = b->chain )
1410
if( b->filter == f && (!ov || b->filter_ov == ov) )
1413
log_bug("pop_filter(): filter function not found\n");
1415
/* flush this stream if it is an output stream */
1416
if( a->use == 2 && (rc=iobuf_flush(b)) ) {
1417
log_error("iobuf_flush failed in pop_filter: %s\n", g10_errstr(rc));
1420
/* and tell the filter to free it self */
1421
if( b->filter && (rc = b->filter(b->filter_ov, IOBUFCTRL_FREE, b->chain,
1422
NULL, &dummy_len)) ) {
1423
log_error("IOBUFCTRL_FREE failed: %s\n", g10_errstr(rc) );
1426
if( b->filter_ov && b->filter_ov_owner ) {
1427
m_free( b->filter_ov );
1428
b->filter_ov = NULL;
1432
/* and see how to remove it */
1433
if( a == b && !b->chain )
1434
log_bug("can't remove the last filter from the chain\n");
1435
else if( a == b ) { /* remove the first iobuf from the chain */
1436
/* everything from b is copied to a. This is save because
1437
* a flush has been done on the to be removed entry
1441
m_free(a->real_fname);
1442
memcpy(a,b, sizeof *a);
1445
log_debug("iobuf-%d.%d: popped filter\n", a->no, a->subno );
1447
else if( !b->chain ) { /* remove the last iobuf from the chain */
1448
log_bug("Ohh jeee, trying to remove a head filter\n");
1450
else { /* remove an intermediate iobuf from the chain */
1451
log_bug("Ohh jeee, trying to remove an intermediate filter\n");
1459
* read underflow: read more bytes into the buffer and return
1460
* the first byte or -1 on EOF.
1468
assert( a->d.start == a->d.len );
1470
return -1; /* EOF because a temp buffer can't do an underflow */
1472
if( a->filter_eof ) {
1476
log_debug("iobuf-%d.%d: pop `%s' in underflow\n",
1477
a->no, a->subno, a->desc );
1479
m_free(a->real_fname);
1480
memcpy(a, b, sizeof *a);
1485
a->filter_eof = 0; /* for the top level filter */
1487
log_debug("iobuf-%d.%d: underflow: eof (due to filter eof)\n",
1489
return -1; /* return one(!) EOF */
1493
log_debug("iobuf-%d.%d: error\n", a->no, a->subno );
1498
FILE *fp = a->directfp;
1500
len = fread( a->d.buf, 1, a->d.size, fp);
1501
if( len < a->d.size ) {
1507
return len? a->d.buf[a->d.start++] : -1;
1514
log_debug("iobuf-%d.%d: underflow: req=%lu\n",
1515
a->no, a->subno, (ulong)len );
1516
rc = a->filter( a->filter_ov, IOBUFCTRL_UNDERFLOW, a->chain,
1519
log_debug("iobuf-%d.%d: underflow: got=%lu rc=%d\n",
1520
a->no, a->subno, (ulong)len, rc );
1521
/* if( a->no == 1 ) */
1522
/* log_hexdump (" data:", a->d.buf, len); */
1524
if( a->use == 1 && rc == -1 ) { /* EOF: we can remove the filter */
1527
/* and tell the filter to free itself */
1528
if( (rc = a->filter(a->filter_ov, IOBUFCTRL_FREE, a->chain,
1529
NULL, &dummy_len)) )
1530
log_error("IOBUFCTRL_FREE failed: %s\n", g10_errstr(rc) );
1531
if( a->filter_ov && a->filter_ov_owner ) {
1532
m_free( a->filter_ov );
1533
a->filter_ov = NULL;
1537
a->filter_ov = NULL;
1539
if( !len && a->chain ) {
1542
log_debug("iobuf-%d.%d: pop `%s' in underflow (!len)\n",
1543
a->no, a->subno, a->desc );
1545
m_free(a->real_fname);
1546
memcpy(a,b, sizeof *a);
1556
log_debug("iobuf-%d.%d: underflow: eof\n", a->no, a->subno );
1561
return a->d.buf[a->d.start++];
1565
log_debug("iobuf-%d.%d: underflow: eof (no filter)\n",
1567
return -1; /* no filter; return EOF */
1573
iobuf_flush(IOBUF a)
1581
if( a->use == 3 ) { /* increase the temp buffer */
1583
size_t newsize = a->d.size + 8192;
1586
log_debug("increasing temp iobuf from %lu to %lu\n",
1587
(ulong)a->d.size, (ulong)newsize );
1588
newbuf = m_alloc( newsize );
1589
memcpy( newbuf, a->d.buf, a->d.len );
1592
a->d.size = newsize;
1595
else if( a->use != 2 )
1596
log_bug("flush on non-output iobuf\n");
1597
else if( !a->filter )
1598
log_bug("iobuf_flush: no filter\n");
1600
rc = a->filter( a->filter_ov, IOBUFCTRL_FLUSH, a->chain, a->d.buf, &len );
1601
if( !rc && len != a->d.len ) {
1602
log_info("iobuf_flush did not write all!\n");
1603
rc = G10ERR_WRITE_FILE;
1614
* Read a byte from the iobuf; returns -1 on EOF
1617
iobuf_readbyte(IOBUF a)
1621
/* nlimit does not work together with unget */
1622
/* nbytes is also not valid! */
1623
if( a->unget.buf ) {
1624
if( a->unget.start < a->unget.len )
1625
return a->unget.buf[a->unget.start++];
1626
m_free(a->unget.buf);
1627
a->unget.buf = NULL;
1631
if( a->nlimit && a->nbytes >= a->nlimit )
1632
return -1; /* forced EOF */
1634
if( a->d.start < a->d.len ) {
1635
c = a->d.buf[a->d.start++];
1637
else if( (c=underflow(a)) == -1 )
1638
return -1; /* EOF */
1646
iobuf_read(IOBUF a, byte *buf, unsigned buflen )
1650
if( a->unget.buf || a->nlimit ) {
1651
/* handle special cases */
1652
for(n=0 ; n < buflen; n++ ) {
1653
if( (c = iobuf_readbyte(a)) == -1 ) {
1655
return -1; /* eof */
1667
if( n < buflen && a->d.start < a->d.len ) {
1668
unsigned size = a->d.len - a->d.start;
1669
if( size > buflen - n )
1672
memcpy( buf, a->d.buf + a->d.start, size );
1679
if( (c=underflow(a)) == -1 ) {
1681
return n? n : -1/*EOF*/;
1687
} while( n < buflen );
1694
* Have a look at the iobuf.
1695
* NOTE: This only works in special cases.
1698
iobuf_peek(IOBUF a, byte *buf, unsigned buflen )
1705
if( !(a->d.start < a->d.len) ) {
1706
if( underflow(a) == -1 )
1708
/* and unget this character */
1709
assert(a->d.start == 1);
1713
for(n=0 ; n < buflen && (a->d.start+n) < a->d.len ; n++, buf++ )
1722
iobuf_writebyte(IOBUF a, unsigned c)
1728
if( a->d.len == a->d.size )
1729
if( iobuf_flush(a) )
1732
assert( a->d.len < a->d.size );
1733
a->d.buf[a->d.len++] = c;
1739
iobuf_write(IOBUF a, byte *buf, unsigned buflen )
1746
if( buflen && a->d.len < a->d.size ) {
1747
unsigned size = a->d.size - a->d.len;
1748
if( size > buflen ) size = buflen;
1749
memcpy( a->d.buf + a->d.len, buf, size );
1755
if( iobuf_flush(a) )
1764
iobuf_writestr(IOBUF a, const char *buf )
1766
for( ; *buf; buf++ )
1767
if( iobuf_writebyte(a, *buf) )
1775
* copy the contents of TEMP to A.
1778
iobuf_write_temp( IOBUF a, IOBUF temp )
1780
while( temp->chain )
1781
pop_filter( temp, temp->filter, NULL );
1782
return iobuf_write(a, temp->d.buf, temp->d.len );
1786
* copy the contents of the temp io stream to BUFFER.
1789
iobuf_temp_to_buffer( IOBUF a, byte *buffer, size_t buflen )
1791
size_t n = a->d.len;
1795
memcpy( buffer, a->d.buf, n );
1801
* Call this function to terminate processing of the temp stream
1802
* without closing it. This removes all filters from the stream
1803
* makes sure that iobuf_get_temp_{buffer,length}() returns correct
1807
iobuf_flush_temp( IOBUF temp )
1809
while( temp->chain )
1810
pop_filter( temp, temp->filter, NULL );
1815
* Set a limit on how many bytes may be read from the input stream A.
1816
* Setting the limit to 0 disables this feature.
1819
iobuf_set_limit( IOBUF a, off_t nlimit )
1826
a->ntotal += a->nbytes;
1833
* Return the length of an open file
1836
iobuf_get_filelength( IOBUF a )
1841
FILE *fp = a->directfp;
1843
if( !fstat(fileno(fp), &st) )
1845
log_error("fstat() failed: %s\n", strerror(errno) );
1849
/* Hmmm: file_filter may have already been removed */
1850
for( ; a; a = a->chain )
1851
if( !a->chain && a->filter == file_filter ) {
1852
file_filter_ctx_t *b = a->filter_ov;
1853
FILEP_OR_FD fp = b->fp;
1855
#if defined(HAVE_DOSISH_SYSTEM) && !defined(FILE_FILTER_USES_STDIO)
1858
if ((size=GetFileSize (fp, NULL)) != 0xffffffff)
1860
log_error ("GetFileSize for handle %p failed: %s\n",
1861
fp, w32_strerror (0));
1863
if( !fstat(my_fileno(fp), &st) )
1865
log_error("fstat() failed: %s\n", strerror(errno) );
1874
/* Return the file descriptor of the underlying file or -1 if it is
1877
iobuf_get_fd (IOBUF a)
1880
return fileno ( (FILE*)a->directfp );
1882
for ( ; a; a = a->chain )
1883
if (!a->chain && a->filter == file_filter)
1885
file_filter_ctx_t *b = a->filter_ov;
1886
FILEP_OR_FD fp = b->fp;
1888
return my_fileno (fp);
1896
* Tell the file position, where the next read will take place
1899
iobuf_tell( IOBUF a )
1901
return a->ntotal + a->nbytes;
1905
#if !defined(HAVE_FSEEKO) && !defined(fseeko)
1907
#ifdef HAVE_LIMITS_H
1908
# include <limits.h>
1911
# define LONG_MAX ((long) ((unsigned long) -1 >> 1))
1914
# define LONG_MIN (-1 - LONG_MAX)
1918
* A substitute for fseeko, for hosts that don't have it.
1921
fseeko( FILE *stream, off_t newpos, int whence )
1923
while( newpos != (long) newpos ) {
1924
long pos = newpos < 0 ? LONG_MIN : LONG_MAX;
1925
if( fseek( stream, pos, whence ) != 0 )
1930
return fseek( stream, (long)newpos, whence );
1935
* This is a very limited implementation. It simply discards all internal
1936
* buffering and removes all filters but the first one.
1939
iobuf_seek( IOBUF a, off_t newpos )
1941
file_filter_ctx_t *b = NULL;
1944
FILE *fp = a->directfp;
1945
if( fseeko( fp, newpos, SEEK_SET ) ) {
1946
log_error("can't seek: %s\n", strerror(errno) );
1952
for( ; a; a = a->chain ) {
1953
if( !a->chain && a->filter == file_filter ) {
1960
#ifdef FILE_FILTER_USES_STDIO
1961
if( fseeko( b->fp, newpos, SEEK_SET ) ) {
1962
log_error("can't fseek: %s\n", strerror(errno) );
1966
#ifdef HAVE_DOSISH_SYSTEM
1967
if (SetFilePointer (b->fp, newpos, NULL, FILE_BEGIN) == 0xffffffff ) {
1968
log_error ("SetFilePointer failed on handle %p: %s\n",
1969
b->fp, w32_strerror (0));
1973
if ( lseek (b->fp, newpos, SEEK_SET) == (off_t)-1 ) {
1974
log_error("can't lseek: %s\n", strerror(errno) );
1980
a->d.len = 0; /* discard buffer */
1987
/* remove filters, but the last */
1989
log_debug("pop_filter called in iobuf_seek - please report\n");
1991
pop_filter( a, a->filter, NULL );
2002
* Retrieve the real filename
2005
iobuf_get_real_fname( IOBUF a )
2008
return a->real_fname;
2010
/* the old solution */
2011
for( ; a; a = a->chain )
2012
if( !a->chain && a->filter == file_filter ) {
2013
file_filter_ctx_t *b = a->filter_ov;
2014
return b->print_only_name? NULL : b->fname;
2022
* Retrieve the filename
2025
iobuf_get_fname( IOBUF a )
2027
for( ; a; a = a->chain )
2028
if( !a->chain && a->filter == file_filter ) {
2029
file_filter_ctx_t *b = a->filter_ov;
2038
* enable partial block mode as described in the OpenPGP draft.
2039
* LEN is the first length byte on read, but ignored on writes.
2042
iobuf_set_partial_block_mode( IOBUF a, size_t len )
2044
block_filter_ctx_t *ctx = m_alloc_clear( sizeof *ctx );
2046
assert( a->use == 1 || a->use == 2 );
2050
log_debug("pop_filter called in set_partial_block_mode"
2051
" - please report\n");
2052
pop_filter(a, block_filter, NULL );
2058
iobuf_push_filter(a, block_filter, ctx );
2064
* Same as fgets() but if the buffer is too short a larger one will
2065
* be allocated up to some limit *max_length.
2066
* A line is considered a byte stream ending in a LF.
2067
* Returns the length of the line. EOF is indicated by a line of
2068
* length zero. The last LF may be missing due to an EOF.
2069
* is max_length is zero on return, the line has been truncated.
2071
* Note: The buffer is allocated with enough space to append a CR,LF,EOL
2074
iobuf_read_line( IOBUF a, byte **addr_of_buffer,
2075
unsigned *length_of_buffer, unsigned *max_length )
2078
char *buffer = *addr_of_buffer;
2079
unsigned length = *length_of_buffer;
2080
unsigned nbytes = 0;
2081
unsigned maxlen = *max_length;
2084
if( !buffer ) { /* must allocate a new buffer */
2086
buffer = m_alloc( length );
2087
*addr_of_buffer = buffer;
2088
*length_of_buffer = length;
2091
length -= 3; /* reserve 3 bytes (cr,lf,eol) */
2093
while( (c=iobuf_get(a)) != -1 ) {
2094
if( nbytes == length ) { /* increase the buffer */
2095
if( length > maxlen ) { /* this is out limit */
2096
/* skip the rest of the line */
2097
while( c != '\n' && (c=iobuf_get(a)) != -1 )
2099
*p++ = '\n'; /* always append a LF (we have reserved space) */
2101
*max_length = 0; /* indicate truncation */
2104
length += 3; /* correct for the reserved byte */
2105
length += length < 1024? 256 : 1024;
2106
buffer = m_realloc( buffer, length );
2107
*addr_of_buffer = buffer;
2108
*length_of_buffer = length;
2109
length -= 3; /* and reserve again */
2110
p = buffer + nbytes;
2117
*p = 0; /* make sure the line is a string */
2122
/* This is the non iobuf specific function */
2124
iobuf_translate_file_handle ( int fd, int for_write )
2131
return fd; /* do not do this for error, stdin, stdout, stderr */
2133
x = _open_osfhandle ( fd, for_write? 1:0 );
2135
log_error ("failed to translate osfhandle %p\n", (void*)fd );
2137
/*log_info ("_open_osfhandle %p yields %d%s\n",
2138
(void*)fd, x, for_write? " for writing":"" );*/
2147
translate_file_handle ( int fd, int for_write )
2150
#ifdef FILE_FILTER_USES_STDIO
2151
fd = iobuf_translate_file_handle (fd, for_write);
2157
x = (int)GetStdHandle (STD_INPUT_HANDLE);
2159
x = (int)GetStdHandle (STD_OUTPUT_HANDLE);
2161
x = (int)GetStdHandle (STD_ERROR_HANDLE);
2166
log_debug ("GetStdHandle(%d) failed: %s\n",
2167
fd, w32_strerror (0));
2178
iobuf_skip_rest(IOBUF a, unsigned long n, int partial)
2182
if (a->nofast || a->d.start >= a->d.len) {
2183
if (iobuf_readbyte (a) == -1) {
2187
unsigned long count = a->d.len - a->d.start;
2189
a->d.start = a->d.len;
2193
unsigned long remaining = n;
2194
while (remaining > 0) {
2195
if (a->nofast || a->d.start >= a->d.len) {
2196
if (iobuf_readbyte (a) == -1) {
2201
unsigned long count = a->d.len - a->d.start;
2202
if (count > remaining) {
2206
a->d.start += count;