xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bss_file.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bss_file.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * 03-Dec-1997	rdenny@dc3.com  Fix bug preventing use of stdin/stdout
610Sstevel@tonic-gate  *		with binary data (e.g. asn1parse -inform DER < xxx) under
620Sstevel@tonic-gate  *		Windows
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifndef HEADER_BSS_FILE_C
660Sstevel@tonic-gate #define HEADER_BSS_FILE_C
670Sstevel@tonic-gate 
68*2139Sjp161948 #if defined(__linux) || defined(__sun) || defined(__hpux)
69*2139Sjp161948 /* Following definition aliases fopen to fopen64 on above mentioned
70*2139Sjp161948  * platforms. This makes it possible to open and sequentially access
71*2139Sjp161948  * files larger than 2GB from 32-bit application. It does not allow to
72*2139Sjp161948  * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
73*2139Sjp161948  * 32-bit platform permits that, not with fseek/ftell. Not to mention
74*2139Sjp161948  * that breaking 2GB limit for seeking would require surgery to *our*
75*2139Sjp161948  * API. But sequential access suffices for practical cases when you
76*2139Sjp161948  * can run into large files, such as fingerprinting, so we can let API
77*2139Sjp161948  * alone. For reference, the list of 32-bit platforms which allow for
78*2139Sjp161948  * sequential access of large files without extra "magic" comprise *BSD,
79*2139Sjp161948  * Darwin, IRIX...
80*2139Sjp161948  */
81*2139Sjp161948 #ifndef _FILE_OFFSET_BITS
82*2139Sjp161948 #define _FILE_OFFSET_BITS 64
83*2139Sjp161948 #endif
84*2139Sjp161948 #endif
85*2139Sjp161948 
860Sstevel@tonic-gate #include <stdio.h>
870Sstevel@tonic-gate #include <errno.h>
880Sstevel@tonic-gate #include "cryptlib.h"
89*2139Sjp161948 #include "bio_lcl.h"
900Sstevel@tonic-gate #include <openssl/err.h>
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #if !defined(OPENSSL_NO_STDIO)
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
950Sstevel@tonic-gate static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
960Sstevel@tonic-gate static int MS_CALLBACK file_puts(BIO *h, const char *str);
970Sstevel@tonic-gate static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
980Sstevel@tonic-gate static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
990Sstevel@tonic-gate static int MS_CALLBACK file_new(BIO *h);
1000Sstevel@tonic-gate static int MS_CALLBACK file_free(BIO *data);
1010Sstevel@tonic-gate static BIO_METHOD methods_filep=
1020Sstevel@tonic-gate 	{
1030Sstevel@tonic-gate 	BIO_TYPE_FILE,
1040Sstevel@tonic-gate 	"FILE pointer",
1050Sstevel@tonic-gate 	file_write,
1060Sstevel@tonic-gate 	file_read,
1070Sstevel@tonic-gate 	file_puts,
1080Sstevel@tonic-gate 	file_gets,
1090Sstevel@tonic-gate 	file_ctrl,
1100Sstevel@tonic-gate 	file_new,
1110Sstevel@tonic-gate 	file_free,
1120Sstevel@tonic-gate 	NULL,
1130Sstevel@tonic-gate 	};
1140Sstevel@tonic-gate 
BIO_new_file(const char * filename,const char * mode)1150Sstevel@tonic-gate BIO *BIO_new_file(const char *filename, const char *mode)
1160Sstevel@tonic-gate 	{
1170Sstevel@tonic-gate 	BIO *ret;
1180Sstevel@tonic-gate 	FILE *file;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if ((file=fopen(filename,mode)) == NULL)
1210Sstevel@tonic-gate 		{
1220Sstevel@tonic-gate 		SYSerr(SYS_F_FOPEN,get_last_sys_error());
1230Sstevel@tonic-gate 		ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
1240Sstevel@tonic-gate 		if (errno == ENOENT)
1250Sstevel@tonic-gate 			BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE);
1260Sstevel@tonic-gate 		else
1270Sstevel@tonic-gate 			BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
1280Sstevel@tonic-gate 		return(NULL);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
1310Sstevel@tonic-gate 		return(NULL);
1320Sstevel@tonic-gate 
133*2139Sjp161948 	BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
1340Sstevel@tonic-gate 	BIO_set_fp(ret,file,BIO_CLOSE);
1350Sstevel@tonic-gate 	return(ret);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
BIO_new_fp(FILE * stream,int close_flag)1380Sstevel@tonic-gate BIO *BIO_new_fp(FILE *stream, int close_flag)
1390Sstevel@tonic-gate 	{
1400Sstevel@tonic-gate 	BIO *ret;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if ((ret=BIO_new(BIO_s_file())) == NULL)
1430Sstevel@tonic-gate 		return(NULL);
1440Sstevel@tonic-gate 
145*2139Sjp161948 	BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */
1460Sstevel@tonic-gate 	BIO_set_fp(ret,stream,close_flag);
1470Sstevel@tonic-gate 	return(ret);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
BIO_s_file(void)1500Sstevel@tonic-gate BIO_METHOD *BIO_s_file(void)
1510Sstevel@tonic-gate 	{
1520Sstevel@tonic-gate 	return(&methods_filep);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
file_new(BIO * bi)1550Sstevel@tonic-gate static int MS_CALLBACK file_new(BIO *bi)
1560Sstevel@tonic-gate 	{
1570Sstevel@tonic-gate 	bi->init=0;
1580Sstevel@tonic-gate 	bi->num=0;
1590Sstevel@tonic-gate 	bi->ptr=NULL;
160*2139Sjp161948 	bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */
1610Sstevel@tonic-gate 	return(1);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
file_free(BIO * a)1640Sstevel@tonic-gate static int MS_CALLBACK file_free(BIO *a)
1650Sstevel@tonic-gate 	{
1660Sstevel@tonic-gate 	if (a == NULL) return(0);
1670Sstevel@tonic-gate 	if (a->shutdown)
1680Sstevel@tonic-gate 		{
1690Sstevel@tonic-gate 		if ((a->init) && (a->ptr != NULL))
1700Sstevel@tonic-gate 			{
171*2139Sjp161948 			if (a->flags&BIO_FLAGS_UPLINK)
172*2139Sjp161948 				UP_fclose (a->ptr);
173*2139Sjp161948 			else
174*2139Sjp161948 				fclose (a->ptr);
1750Sstevel@tonic-gate 			a->ptr=NULL;
176*2139Sjp161948 			a->flags=BIO_FLAGS_UPLINK;
1770Sstevel@tonic-gate 			}
1780Sstevel@tonic-gate 		a->init=0;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	return(1);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
file_read(BIO * b,char * out,int outl)1830Sstevel@tonic-gate static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
1840Sstevel@tonic-gate 	{
1850Sstevel@tonic-gate 	int ret=0;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (b->init && (out != NULL))
1880Sstevel@tonic-gate 		{
189*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
190*2139Sjp161948 			ret=UP_fread(out,1,(int)outl,b->ptr);
191*2139Sjp161948 		else
192*2139Sjp161948 			ret=fread(out,1,(int)outl,(FILE *)b->ptr);
193*2139Sjp161948 		if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr))
1940Sstevel@tonic-gate 			{
1950Sstevel@tonic-gate 			SYSerr(SYS_F_FREAD,get_last_sys_error());
1960Sstevel@tonic-gate 			BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB);
1970Sstevel@tonic-gate 			ret=-1;
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	return(ret);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
file_write(BIO * b,const char * in,int inl)2030Sstevel@tonic-gate static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
2040Sstevel@tonic-gate 	{
2050Sstevel@tonic-gate 	int ret=0;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (b->init && (in != NULL))
2080Sstevel@tonic-gate 		{
209*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
210*2139Sjp161948 			ret=UP_fwrite(in,(int)inl,1,b->ptr);
211*2139Sjp161948 		else
212*2139Sjp161948 			ret=fwrite(in,(int)inl,1,(FILE *)b->ptr);
213*2139Sjp161948 		if (ret)
2140Sstevel@tonic-gate 			ret=inl;
2150Sstevel@tonic-gate 		/* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
2160Sstevel@tonic-gate 		/* according to Tim Hudson <tjh@cryptsoft.com>, the commented
2170Sstevel@tonic-gate 		 * out version above can cause 'inl' write calls under
2180Sstevel@tonic-gate 		 * some stupid stdio implementations (VMS) */
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	return(ret);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
file_ctrl(BIO * b,int cmd,long num,void * ptr)2230Sstevel@tonic-gate static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
2240Sstevel@tonic-gate 	{
2250Sstevel@tonic-gate 	long ret=1;
2260Sstevel@tonic-gate 	FILE *fp=(FILE *)b->ptr;
2270Sstevel@tonic-gate 	FILE **fpp;
2280Sstevel@tonic-gate 	char p[4];
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	switch (cmd)
2310Sstevel@tonic-gate 		{
2320Sstevel@tonic-gate 	case BIO_C_FILE_SEEK:
2330Sstevel@tonic-gate 	case BIO_CTRL_RESET:
234*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
235*2139Sjp161948 			ret=(long)UP_fseek(b->ptr,num,0);
236*2139Sjp161948 		else
237*2139Sjp161948 			ret=(long)fseek(fp,num,0);
2380Sstevel@tonic-gate 		break;
2390Sstevel@tonic-gate 	case BIO_CTRL_EOF:
240*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
241*2139Sjp161948 			ret=(long)UP_feof(fp);
242*2139Sjp161948 		else
243*2139Sjp161948 			ret=(long)feof(fp);
2440Sstevel@tonic-gate 		break;
2450Sstevel@tonic-gate 	case BIO_C_FILE_TELL:
2460Sstevel@tonic-gate 	case BIO_CTRL_INFO:
247*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
248*2139Sjp161948 			ret=UP_ftell(b->ptr);
249*2139Sjp161948 		else
250*2139Sjp161948 			ret=ftell(fp);
2510Sstevel@tonic-gate 		break;
2520Sstevel@tonic-gate 	case BIO_C_SET_FILE_PTR:
2530Sstevel@tonic-gate 		file_free(b);
2540Sstevel@tonic-gate 		b->shutdown=(int)num&BIO_CLOSE;
255*2139Sjp161948 		b->ptr=ptr;
2560Sstevel@tonic-gate 		b->init=1;
257*2139Sjp161948 #if BIO_FLAGS_UPLINK!=0
258*2139Sjp161948 #if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
259*2139Sjp161948 #define _IOB_ENTRIES 20
260*2139Sjp161948 #endif
261*2139Sjp161948 #if defined(_IOB_ENTRIES)
262*2139Sjp161948 		/* Safety net to catch purely internal BIO_set_fp calls */
263*2139Sjp161948 		if ((size_t)ptr >= (size_t)stdin &&
264*2139Sjp161948 		    (size_t)ptr <  (size_t)(stdin+_IOB_ENTRIES))
265*2139Sjp161948 			BIO_clear_flags(b,BIO_FLAGS_UPLINK);
266*2139Sjp161948 #endif
267*2139Sjp161948 #endif
268*2139Sjp161948 #ifdef UP_fsetmode
269*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
270*2139Sjp161948 			UP_fsetmode(b->ptr,num&BIO_FP_TEXT?'t':'b');
271*2139Sjp161948 		else
272*2139Sjp161948 #endif
273*2139Sjp161948 		{
2740Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS)
275*2139Sjp161948 		int fd = fileno((FILE*)ptr);
2760Sstevel@tonic-gate 		if (num & BIO_FP_TEXT)
277*2139Sjp161948 			_setmode(fd,_O_TEXT);
2780Sstevel@tonic-gate 		else
279*2139Sjp161948 			_setmode(fd,_O_BINARY);
280*2139Sjp161948 #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
281*2139Sjp161948 		int fd = fileno((FILE*)ptr);
282*2139Sjp161948          /* Under CLib there are differences in file modes
283*2139Sjp161948          */
284*2139Sjp161948 		if (num & BIO_FP_TEXT)
285*2139Sjp161948 			_setmode(fd,O_TEXT);
286*2139Sjp161948 		else
287*2139Sjp161948 			_setmode(fd,O_BINARY);
2880Sstevel@tonic-gate #elif defined(OPENSSL_SYS_MSDOS)
2890Sstevel@tonic-gate 		int fd = fileno((FILE*)ptr);
2900Sstevel@tonic-gate 		/* Set correct text/binary mode */
2910Sstevel@tonic-gate 		if (num & BIO_FP_TEXT)
2920Sstevel@tonic-gate 			_setmode(fd,_O_TEXT);
2930Sstevel@tonic-gate 		/* Dangerous to set stdin/stdout to raw (unless redirected) */
2940Sstevel@tonic-gate 		else
2950Sstevel@tonic-gate 			{
2960Sstevel@tonic-gate 			if (fd == STDIN_FILENO || fd == STDOUT_FILENO)
2970Sstevel@tonic-gate 				{
2980Sstevel@tonic-gate 				if (isatty(fd) <= 0)
2990Sstevel@tonic-gate 					_setmode(fd,_O_BINARY);
3000Sstevel@tonic-gate 				}
3010Sstevel@tonic-gate 			else
3020Sstevel@tonic-gate 				_setmode(fd,_O_BINARY);
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate #elif defined(OPENSSL_SYS_OS2)
305*2139Sjp161948 		int fd = fileno((FILE*)ptr);
3060Sstevel@tonic-gate 		if (num & BIO_FP_TEXT)
307*2139Sjp161948 			setmode(fd, O_TEXT);
3080Sstevel@tonic-gate 		else
309*2139Sjp161948 			setmode(fd, O_BINARY);
3100Sstevel@tonic-gate #endif
311*2139Sjp161948 		}
3120Sstevel@tonic-gate 		break;
3130Sstevel@tonic-gate 	case BIO_C_SET_FILENAME:
3140Sstevel@tonic-gate 		file_free(b);
3150Sstevel@tonic-gate 		b->shutdown=(int)num&BIO_CLOSE;
3160Sstevel@tonic-gate 		if (num & BIO_FP_APPEND)
3170Sstevel@tonic-gate 			{
3180Sstevel@tonic-gate 			if (num & BIO_FP_READ)
3190Sstevel@tonic-gate 				BUF_strlcpy(p,"a+",sizeof p);
3200Sstevel@tonic-gate 			else	BUF_strlcpy(p,"a",sizeof p);
3210Sstevel@tonic-gate 			}
3220Sstevel@tonic-gate 		else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
3230Sstevel@tonic-gate 			BUF_strlcpy(p,"r+",sizeof p);
3240Sstevel@tonic-gate 		else if (num & BIO_FP_WRITE)
3250Sstevel@tonic-gate 			BUF_strlcpy(p,"w",sizeof p);
3260Sstevel@tonic-gate 		else if (num & BIO_FP_READ)
3270Sstevel@tonic-gate 			BUF_strlcpy(p,"r",sizeof p);
3280Sstevel@tonic-gate 		else
3290Sstevel@tonic-gate 			{
3300Sstevel@tonic-gate 			BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
3310Sstevel@tonic-gate 			ret=0;
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 			}
334*2139Sjp161948 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
335*2139Sjp161948 		if (!(num & BIO_FP_TEXT))
336*2139Sjp161948 			strcat(p,"b");
337*2139Sjp161948 		else
338*2139Sjp161948 			strcat(p,"t");
339*2139Sjp161948 #endif
340*2139Sjp161948 #if defined(OPENSSL_SYS_NETWARE)
3410Sstevel@tonic-gate 		if (!(num & BIO_FP_TEXT))
3420Sstevel@tonic-gate 			strcat(p,"b");
3430Sstevel@tonic-gate 		else
3440Sstevel@tonic-gate 			strcat(p,"t");
3450Sstevel@tonic-gate #endif
3460Sstevel@tonic-gate 		fp=fopen(ptr,p);
3470Sstevel@tonic-gate 		if (fp == NULL)
3480Sstevel@tonic-gate 			{
3490Sstevel@tonic-gate 			SYSerr(SYS_F_FOPEN,get_last_sys_error());
3500Sstevel@tonic-gate 			ERR_add_error_data(5,"fopen('",ptr,"','",p,"')");
3510Sstevel@tonic-gate 			BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
3520Sstevel@tonic-gate 			ret=0;
3530Sstevel@tonic-gate 			break;
3540Sstevel@tonic-gate 			}
355*2139Sjp161948 		b->ptr=fp;
3560Sstevel@tonic-gate 		b->init=1;
357*2139Sjp161948 		BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
3580Sstevel@tonic-gate 		break;
3590Sstevel@tonic-gate 	case BIO_C_GET_FILE_PTR:
3600Sstevel@tonic-gate 		/* the ptr parameter is actually a FILE ** in this case. */
3610Sstevel@tonic-gate 		if (ptr != NULL)
3620Sstevel@tonic-gate 			{
3630Sstevel@tonic-gate 			fpp=(FILE **)ptr;
3640Sstevel@tonic-gate 			*fpp=(FILE *)b->ptr;
3650Sstevel@tonic-gate 			}
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	case BIO_CTRL_GET_CLOSE:
3680Sstevel@tonic-gate 		ret=(long)b->shutdown;
3690Sstevel@tonic-gate 		break;
3700Sstevel@tonic-gate 	case BIO_CTRL_SET_CLOSE:
3710Sstevel@tonic-gate 		b->shutdown=(int)num;
3720Sstevel@tonic-gate 		break;
3730Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
374*2139Sjp161948 		if (b->flags&BIO_FLAGS_UPLINK)
375*2139Sjp161948 			UP_fflush(b->ptr);
376*2139Sjp161948 		else
377*2139Sjp161948 			fflush((FILE *)b->ptr);
3780Sstevel@tonic-gate 		break;
3790Sstevel@tonic-gate 	case BIO_CTRL_DUP:
3800Sstevel@tonic-gate 		ret=1;
3810Sstevel@tonic-gate 		break;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
3840Sstevel@tonic-gate 	case BIO_CTRL_PENDING:
3850Sstevel@tonic-gate 	case BIO_CTRL_PUSH:
3860Sstevel@tonic-gate 	case BIO_CTRL_POP:
3870Sstevel@tonic-gate 	default:
3880Sstevel@tonic-gate 		ret=0;
3890Sstevel@tonic-gate 		break;
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 	return(ret);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
file_gets(BIO * bp,char * buf,int size)3940Sstevel@tonic-gate static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
3950Sstevel@tonic-gate 	{
3960Sstevel@tonic-gate 	int ret=0;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	buf[0]='\0';
399*2139Sjp161948 	if (bp->flags&BIO_FLAGS_UPLINK)
400*2139Sjp161948 		UP_fgets(buf,size,bp->ptr);
401*2139Sjp161948 	else
402*2139Sjp161948 		fgets(buf,size,(FILE *)bp->ptr);
4030Sstevel@tonic-gate 	if (buf[0] != '\0')
4040Sstevel@tonic-gate 		ret=strlen(buf);
4050Sstevel@tonic-gate 	return(ret);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
file_puts(BIO * bp,const char * str)4080Sstevel@tonic-gate static int MS_CALLBACK file_puts(BIO *bp, const char *str)
4090Sstevel@tonic-gate 	{
4100Sstevel@tonic-gate 	int n,ret;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	n=strlen(str);
4130Sstevel@tonic-gate 	ret=file_write(bp,str,n);
4140Sstevel@tonic-gate 	return(ret);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate #endif /* OPENSSL_NO_STDIO */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate #endif /* HEADER_BSS_FILE_C */
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 
422