markizano

urand

Feb 17th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <math.h>
  9.  
  10. int main (int argc, char **argv) {
  11.     int rfd, i, out_size;
  12.     out_size = 64;
  13.     char random_output[out_size];
  14.  
  15.     rfd = open("/dev/urandom", O_RDONLY);
  16.  
  17.     if (rfd < 0) {
  18.         fprintf(stderr, "Could not open /dev/urandom: %m\n");
  19.         goto endofrandom;
  20.     }
  21.  
  22.     if (read(rfd, random_output, out_size) < 0) {
  23.         fprintf(stderr, "Could not read from /dev/urandom. %m\n");
  24.         goto endofrandom;
  25.     }
  26.  
  27.     if (argc == 2) {
  28.         if ( !strcmp(argv[1], "-hex") || !strcmp(argv[1], "--hex") )  {
  29.             for ( i = 0; i <= out_size; ++i ) {
  30.                 printf("\\x%X", abs(*(random_output +i)));
  31.             }
  32.         } else if ( !strcmp(argv[1], "-dec") || !strcmp(argv[1], "--dec") ) {
  33.             for ( i = 0; i <= out_size; ++i ) {
  34.                 printf("\\%d", abs(*(random_output +i)));
  35.             }
  36.         } else if ( !strcmp(argv[1], "-oct") || !strcmp(argv[1], "--oct") ) {
  37.             printf("Future feature\n");
  38.         }
  39.  
  40.         printf("%c", 10);
  41.  
  42.     } else {
  43.         printf("Usage: %s: -hex|-dec|-oct\n");
  44.     }
  45. endofrandom:
  46.     close(rfd);
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment