home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / randdrv / test / rand.cpp next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  2.0 KB  |  82 lines

  1. /*
  2.  RANDDRV.SYS random number driver for RNG-810 random # or similar.
  3.  Copyright (C) 1994 Paul Elliot
  4.  
  5.  This program is free software; you can redistribute it and/or
  6.  modify it under the terms of the GNU General Public License
  7.  as published by the Free Software Foundation; either version 2
  8.  of the License, or (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. Paul.Elliott@Hrnowl.LoneStar.Org
  20.  
  21. Paul Elliott
  22.  
  23. 3986 South Gessner #224 Houston TX 77063
  24.  
  25. */
  26. /*
  27. compile with borlands compiler for os/2
  28.  
  29. invoke
  30.  
  31.     rand filespec file_size
  32.  
  33.     creates a file with file_size random bytes.
  34. */
  35. #include <assert.h>
  36. #include <stdio.h>
  37. #include <iostream.h>
  38. #include <iomanip.h>
  39. #include <fstream.h>
  40. #include <stdlib.h>
  41. #define INCL_DOS
  42. #define INCL_DOSERRORS
  43. #include <os2.h>
  44. int main(int argc,char *argv[])
  45. {
  46.     APIRET rv;
  47.     HFILE handle;
  48.     ULONG action;
  49.     ULONG parameterBufferLength=0;
  50.     ULONG commandBufferLength=0;
  51.  
  52.     if (argc != 3)
  53.     {
  54.       cerr << "Incorrect number of arguements." << endl;
  55.       return 1;
  56.     }
  57.     rv = DosOpen( "$RANDOM$", &handle, &action, 0, 0, 1, OPEN_FLAGS_FAIL_ON_ERROR | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE, 0 );
  58.     assert( rv == NO_ERROR );
  59.     ifstream rand(handle);
  60.     if(!rand)
  61.     {
  62.       perror("Unable to open random number gnerator.");
  63.       return 2;
  64.     };
  65.     ofstream out(argv[1],ios::binary);
  66.     if(!out)
  67.     {
  68.       perror("Unaable to open output file.");
  69.       return 3;
  70.     };
  71.     char buf[512];
  72.     int remain=atoi(argv[2]);
  73.     while (remain)
  74.     {
  75.         int count = (remain > sizeof(buf)) ? sizeof(buf) : remain;
  76.         rand.read(buf,count);
  77.         int size = rand.gcount();
  78.         out.write(buf,size);
  79.         remain -= size;
  80.     };
  81. }
  82.