Reduce version skew: trivia (I hope).
[openssl.git] / crypto / rand / randfile.c
index c345d085417c7ba75c6b2f5a02b55aba1ef52b85..7f1428072d27d612b2345353a7c408a68b9d1df7 100644 (file)
@@ -57,7 +57,9 @@
  */
 
 /* We need to define this to get macros like S_IFBLK and S_IFCHR */
+#if !defined(OPENSSL_SYS_VXWORKS)
 #define _XOPEN_SOURCE 500
+#endif
 
 #include <errno.h>
 #include <stdio.h>
 # include <sys/stat.h>
 #endif
 
+#ifdef _WIN32
+#define stat   _stat
+#define chmod  _chmod
+#define open   _open
+#define fdopen _fdopen
+#endif
+
 #undef BUFSIZE
 #define BUFSIZE        1024
 #define RAND_DATA 1024
@@ -105,13 +114,20 @@ int RAND_load_file(const char *file, long bytes)
 #ifndef OPENSSL_NO_POSIX_IO
        struct stat sb;
 #endif
-       int i,ret=0;
-       size_t n;
+       int i,ret=0,n;
        FILE *in;
 
        if (file == NULL) return(0);
 
 #ifndef OPENSSL_NO_POSIX_IO
+#ifdef PURIFY
+       /* struct stat can have padding and unused fields that may not be
+        * initialized in the call to stat(). We need to clear the entire
+        * structure before calling RAND_add() to avoid complaints from
+        * applications such as Valgrind.
+        */
+       memset(&sb, 0, sizeof(sb));
+#endif
        if (stat(file,&sb) < 0) return(0);
        RAND_add(&sb,sizeof(sb),0.0);
 #endif
@@ -123,14 +139,16 @@ int RAND_load_file(const char *file, long bytes)
        in=fopen(file,"rb");
 #endif
        if (in == NULL) goto err;
-#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPNESSL_NO_POSIX_IO)
+#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPENSSL_NO_POSIX_IO)
        if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
          /* this file is a device. we don't want read an infinite number
           * of bytes from a random device, nor do we want to use buffered
           * I/O because we will waste system entropy. 
           */
          bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */
+#ifndef OPENSSL_NO_SETVBUF_IONBF
          setvbuf(in, NULL, _IONBF, 0); /* don't do buffered reads */
+#endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
        }
 #endif
        for (;;)
@@ -163,8 +181,7 @@ err:
 int RAND_write_file(const char *file)
        {
        unsigned char buf[BUFSIZE];
-       int ret=0,rand_err=0;
-       size_t i;
+       int i,ret=0,rand_err=0;
        FILE *out = NULL;
        int n;
 #ifndef OPENSSL_NO_POSIX_IO
@@ -172,8 +189,8 @@ int RAND_write_file(const char *file)
        
        i=stat(file,&sb);
        if (i != -1) { 
-#if defined(S_IFBLK) && defined(S_IFCHR)
-         if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
+#if defined(S_ISBLK) && defined(S_ISCHR)
+         if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
            /* this file is a device. we don't write back to it. 
             * we "succeed" on the assumption this is some sort 
             * of random device. Otherwise attempting to write to 
@@ -228,7 +245,7 @@ int RAND_write_file(const char *file)
        if (out == NULL) goto err;
 
 #ifndef NO_CHMOD
-       chmod(file,(mode_t)0600);
+       chmod(file,0600);
 #endif
        n=RAND_DATA;
        for (;;)
@@ -238,7 +255,7 @@ int RAND_write_file(const char *file)
                if (RAND_bytes(buf,i) <= 0)
                        rand_err=1;
                i=fwrite(buf,1,i,out);
-               if (i == 0)
+               if (i <= 0)
                        {
                        ret=0;
                        break;
@@ -256,7 +273,6 @@ err:
 const char *RAND_file_name(char *buf, size_t size)
        {
        char *s=NULL;
-       int ok = 0;
 #ifdef __OpenBSD__
        struct stat sb;
 #endif
@@ -285,7 +301,6 @@ const char *RAND_file_name(char *buf, size_t size)
                        BUF_strlcat(buf,"/",size);
 #endif
                        BUF_strlcat(buf,RFILE,size);
-                       ok = 1;
                        }
                else
                        buf[0] = '\0'; /* no file name */
@@ -299,7 +314,7 @@ const char *RAND_file_name(char *buf, size_t size)
         * to something hopefully decent if that isn't available. 
         */
 
-       if (!ok)
+       if (!buf[0])
                if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
                        return(NULL);
                }