Prepare for next version.
[oweals/openssl.git] / crypto / bio / bss_file.c
index dd17802f8fb18b7a7cd204ed81518ad64fd03b17..9ad46fa081dbedb40c7eba675eb9d53797e958e1 100644 (file)
 #ifndef HEADER_BSS_FILE_C
 #define HEADER_BSS_FILE_C
 
+#if defined(__linux) || defined(__sun) || defined(__hpux)
+/* Following definition aliases fopen to fopen64 on above mentioned
+ * platforms. This makes it possible to open and sequentially access
+ * files larger than 2GB from 32-bit application. It does not allow to
+ * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
+ * 32-bit platform permits that, not with fseek/ftell. Not to mention
+ * that breaking 2GB limit for seeking would require surgery to *our*
+ * API. But sequential access suffices for practical cases when you
+ * can run into large files, such as fingerprinting, so we can let API
+ * alone. For reference, the list of 32-bit platforms which allow for
+ * sequential access of large files without extra "magic" comprise *BSD,
+ * Darwin, IRIX...
+ */
+#ifndef _FILE_OFFSET_BITS
+#define _FILE_OFFSET_BITS 64
+#endif
+#endif
+
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
 #include "bio_lcl.h"
 #include <openssl/err.h>
 
+#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
+#include <nwfileio.h>
+#endif
+
 #if !defined(OPENSSL_NO_STDIO)
 
 static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
@@ -110,7 +132,10 @@ BIO *BIO_new_file(const char *filename, const char *mode)
                return(NULL);
                }
        if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
+               {
+               fclose(file);
                return(NULL);
+               }
 
        BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
        BIO_set_fp(ret,file,BIO_CLOSE);
@@ -254,7 +279,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
 #endif
                {
 #if defined(OPENSSL_SYS_WINDOWS)
-               int fd = fileno((FILE*)ptr);
+               int fd = _fileno((FILE*)ptr);
                if (num & BIO_FP_TEXT)
                        _setmode(fd,_O_TEXT);
                else
@@ -264,9 +289,9 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
          /* Under CLib there are differences in file modes
          */
                if (num & BIO_FP_TEXT)
-                       _setmode(fd,O_TEXT);
+                       setmode(fd,O_TEXT);
                else
-                       _setmode(fd,O_BINARY);
+                       setmode(fd,O_BINARY);
 #elif defined(OPENSSL_SYS_MSDOS)
                int fd = fileno((FILE*)ptr);
                /* Set correct text/binary mode */
@@ -378,7 +403,10 @@ static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
        int ret=0;
 
        buf[0]='\0';
-       fgets(buf,size,(FILE *)bp->ptr);
+       if (bp->flags&BIO_FLAGS_UPLINK)
+               UP_fgets(buf,size,bp->ptr);
+       else
+               fgets(buf,size,(FILE *)bp->ptr);
        if (buf[0] != '\0')
                ret=strlen(buf);
        return(ret);