This patch replaces the patch included in noise063a2.zip



2015-12-06  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* noisesys/src/demo/sample.c :  Changes from sample_c.diff.







diff -aprNU5 demo.orig/sample.c demo/sample.c
--- demo.orig/sample.c	1996-06-05 10:05:18 +0200
+++ demo/sample.c	2015-12-06 01:33:40 +0100
@@ -22,11 +22,11 @@
 #include <stdarg.h>
 #include <string.h>
 
 #include "usuals.h"
 
-#ifdef __MSDOS__
+#if defined(__MSDOS__) || defined(__DJGPP__)
 #       define SIZE 8192             /* size of buffer */
 #       define DEFAULT_DEV "/dev/random$"
 #else
 #       define SIZE 1024
 #       define DEFAULT_DEV "/dev/random"
@@ -64,11 +64,15 @@ void main(int argc, char *argv[]) {
    possible to overwrite a file out of carelessness!
 -------------------------------------------------------------------------- */
   else {
     filemode="wb";
     devname=getenv("RNG_DEVICE");
+#ifdef __DJGPP__
+    if (strcmp(devname, NULL)==0) devname=DEFAULT_DEV;
+#else
     if (strcmp(devname,"")==0) devname=DEFAULT_DEV;
+#endif /* __DJGPP__ */
 
     ofname=argv[1];
     num=0; sscanf(argv[2], "%ld", &num);
 
     if (argc>3) for (i=3;i<argc;i++) {
@@ -77,18 +81,18 @@ void main(int argc, char *argv[]) {
 #ifdef DEBUG
         printf("argv[%d][%d] = %c\n", i, j, argv[i][j]);
 #endif
         switch (argv[i][j]) {
           case 'r':
-#ifdef __MSDOS__
+#if defined(__MSDOS__) || defined(__DJGPP__)
             devname="/dev/random$";
 #else
             devname="/dev/random";
 #endif
             break;
           case 'u':
-#ifdef __MSDOS__
+#if defined(__MSDOS__) || defined(__DJGPP__)
             devname="/dev/urandom$";
 #else
             devname="/dev/urandom";
 #endif
             break;


2015-12-06  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* noisesys/src/demo/sample.c :  Various bug fixes.








diff -aprNU5 demo.orig/sample.c demo/sample.c
--- demo.orig/sample.c	2015-12-06 01:33:40 +0100
+++ demo/sample.c	2015-12-06 01:49:06 +0100
@@ -47,14 +47,14 @@ void usage(void) {
                   "The default device is set in the RNG_DEVICE environment\n"
                   "variable (if none is set, " DEFAULT_DEV " is assumed)\n");
   exit(EXIT_FAILURE);
 }
 
-void main(int argc, char *argv[]) {
+int main(int argc, char *argv[]) {
 
   FILE *randstream, *ofile;
-  struct stat *ostatbuf;
+  struct stat ostatbuf;
   word32 num,total=0;
   int i,j, verbose=0;
   unsigned int bytesread,needed=0;
   static char buff[SIZE], *ofname, *filemode, *devname;
 
@@ -64,15 +64,11 @@ void main(int argc, char *argv[]) {
    possible to overwrite a file out of carelessness!
 -------------------------------------------------------------------------- */
   else {
     filemode="wb";
     devname=getenv("RNG_DEVICE");
-#ifdef __DJGPP__
-    if (strcmp(devname, NULL)==0) devname=DEFAULT_DEV;
-#else
-    if (strcmp(devname,"")==0) devname=DEFAULT_DEV;
-#endif /* __DJGPP__ */
+    if (!devname || strcmp(devname, "")==0) devname=DEFAULT_DEV;
 
     ofname=argv[1];
     num=0; sscanf(argv[2], "%ld", &num);
 
     if (argc>3) for (i=3;i<argc;i++) {
@@ -95,12 +91,12 @@ void main(int argc, char *argv[]) {
 #else
             devname="/dev/urandom";
 #endif
             break;
           case 's':
-            if (stat(ofname, ostatbuf)==0) {
-              if (num> ostatbuf->st_size) num-=ostatbuf->st_size;
+            if (stat(ofname, &ostatbuf)==0) {
+              if (num> ostatbuf.st_size) num-=ostatbuf.st_size;
               else num=0;
             }
           case 'a':
             filemode="ab"; /* append mode */
             break;
@@ -122,11 +118,11 @@ void main(int argc, char *argv[]) {
 #ifdef DEBUG
       fprintf(stderr, "open file %s (mode %s) of %ld bytes (requested)\n",
          ofname, filemode, num);
 #endif
       do {
-        needed=(SIZE>num)?(needed=num):(needed=SIZE);
+        needed=(SIZE>num)?num:SIZE;
    /* ----- read bytes from the random stream ----- */
         bytesread=fread(&buff, 1, needed, randstream);
 #ifdef DEBUG
         fprintf(stderr, "%d bytes read from device %s\n", bytesread, devname);
 #endif
@@ -137,7 +133,9 @@ void main(int argc, char *argv[]) {
       fclose(ofile);
       if (verbose) printf("%ld bytes appended to %s\n", total, ofname);
       exit(EXIT_SUCCESS);
     }
   }
+
+  return 0;
 }
 


2015-12-06  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* noisesys/src/demo/sample.c :  White space changes.









diff -aprNU5 demo.orig/sample.c demo/sample.c
--- demo.orig/sample.c	2015-12-06 01:49:06 +0100
+++ demo/sample.c	2015-12-06 02:12:46 +0100
@@ -23,15 +23,15 @@
 #include <string.h>
 
 #include "usuals.h"
 
 #if defined(__MSDOS__) || defined(__DJGPP__)
-#       define SIZE 8192             /* size of buffer */
-#       define DEFAULT_DEV "/dev/random$"
+# define SIZE         8192             /* size of buffer */
+# define DEFAULT_DEV  "/dev/random$"
 #else
-#       define SIZE 1024
-#       define DEFAULT_DEV "/dev/random"
+# define SIZE         1024
+# define DEFAULT_DEV  "/dev/random"
 #endif
 
 void usage(void) {
   fprintf(stderr, "usage: sample file num [-asruv] [dev]\n"
                   "examples: sample foo.bar 256 -ar\n"
@@ -47,39 +47,46 @@ void usage(void) {
                   "The default device is set in the RNG_DEVICE environment\n"
                   "variable (if none is set, " DEFAULT_DEV " is assumed)\n");
   exit(EXIT_FAILURE);
 }
 
-int main(int argc, char *argv[]) {
-
+int main(int argc, char *argv[])
+{
   FILE *randstream, *ofile;
   struct stat ostatbuf;
-  word32 num,total=0;
-  int i,j, verbose=0;
-  unsigned int bytesread,needed=0;
+  word32 num, total = 0;
+  int i, j, verbose = 0;
+  unsigned int bytesread, needed = 0;
   static char buff[SIZE], *ofname, *filemode, *devname;
 
-  if (argc<3) usage();
+  if (argc < 3) usage();
 /* -------------------------------------------------------------------------
    WARNING: This doesn't check if a file exists already, so it's very
    possible to overwrite a file out of carelessness!
--------------------------------------------------------------------------- */
-  else {
-    filemode="wb";
-    devname=getenv("RNG_DEVICE");
-    if (!devname || strcmp(devname, "")==0) devname=DEFAULT_DEV;
-
-    ofname=argv[1];
-    num=0; sscanf(argv[2], "%ld", &num);
-
-    if (argc>3) for (i=3;i<argc;i++) {
-      if (argv[i][0]=='-')
-        for (j=1;j<strlen(argv[i]);j++) {
+   ------------------------------------------------------------------------- */
+  else
+  {
+    filemode = "wb";
+    devname = getenv("RNG_DEVICE");
+    if (!devname || strcmp(devname, "") == 0) devname = DEFAULT_DEV;
+
+    ofname = argv[1];
+    num = 0; sscanf(argv[2], "%ld", &num);
+
+    for (i = 3; i < argc; i++)
+    {
+      if (argv[i][0] == '-')
+      {
+        const size_t length = strlen(argv[i]);
+
+        for (j = 1; j < length; j++)
+        {
 #ifdef DEBUG
-        printf("argv[%d][%d] = %c\n", i, j, argv[i][j]);
+          printf("argv[%d][%d] = %c\n", i, j, argv[i][j]);
 #endif
-        switch (argv[i][j]) {
+          switch (argv[i][j])
+          {
           case 'r':
 #if defined(__MSDOS__) || defined(__DJGPP__)
             devname="/dev/random$";
 #else
             devname="/dev/random";
@@ -91,51 +98,61 @@ int main(int argc, char *argv[]) {
 #else
             devname="/dev/urandom";
 #endif
             break;
           case 's':
-            if (stat(ofname, &ostatbuf)==0) {
-              if (num> ostatbuf.st_size) num-=ostatbuf.st_size;
-              else num=0;
+            if (stat(ofname, &ostatbuf) == 0)
+            {
+              if (num > ostatbuf.st_size)
+                num -= ostatbuf.st_size;
+              else
+                num = 0;
             }
           case 'a':
-            filemode="ab"; /* append mode */
+            filemode = "ab"; /* append mode */
             break;
           case 'v':
             verbose = 1;
             break;
+          }
         }
-      } else devname=argv[i];
+      }
+      else
+        devname = argv[i];
     }
-        /* ----- open random stream in binary mode ----- */
-    if ((randstream=fopen(devname,"rb"))<=0) {
-   /* sometimes if device is not loaded, randstream == 0 */
+
+    /* ----- open random stream in binary mode ----- */
+    if ((randstream = fopen(devname, "rb")) <= 0)
+    {
+      /* sometimes if device is not loaded, randstream == 0 */
       fprintf(stderr, "error opening %s\n",devname);
       exit(EXIT_FAILURE);
-    } else if ((ofile=fopen(ofname,filemode))<0) { 
-        fprintf(stderr, "unable to open file %s\n", ofname);
-        exit(EXIT_FAILURE);
-    } else {
+    }
+    else if ((ofile = fopen(ofname, filemode)) < 0)
+    { 
+      fprintf(stderr, "unable to open file %s\n", ofname);
+      exit(EXIT_FAILURE);
+    }
+    else
+    {
 #ifdef DEBUG
-      fprintf(stderr, "open file %s (mode %s) of %ld bytes (requested)\n",
-         ofname, filemode, num);
+      fprintf(stderr, "open file %s (mode %s) of %ld bytes (requested)\n", ofname, filemode, num);
 #endif
       do {
-        needed=(SIZE>num)?num:SIZE;
-   /* ----- read bytes from the random stream ----- */
+        needed = SIZE > num ? num : SIZE;
+        /* ----- read bytes from the random stream ----- */
         bytesread=fread(&buff, 1, needed, randstream);
 #ifdef DEBUG
         fprintf(stderr, "%d bytes read from device %s\n", bytesread, devname);
 #endif
         if (bytesread)
-           num-=((i=fwrite(&buff, 1, bytesread, ofile))>=0)?i:0;
-        total+=bytesread;
+          num -= (i = fwrite(&buff, 1, bytesread, ofile)) >= 0 ? i : 0;
+        total += bytesread;
       } while ((num!=0) && (bytesread==needed) && (i==bytesread));
       fclose(ofile);
       if (verbose) printf("%ld bytes appended to %s\n", total, ofname);
       exit(EXIT_SUCCESS);
     }
   }
 
   return 0;
 }
-
