Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. /* This program's generates random numbers and write them out to a file
  2.       Then get the md5sum of the file.
  3. */
  4.  
  5. /* EDITORS NOTE:
  6.     Finally had time to check this out this morning.
  7.  
  8.     I made quite a few assumptions in this program for the sake of brevity.
  9.     It assumes a few things, like for example that the numbers generated
  10.     are 32bit signed ints or less. Since they come from rand() this should always
  11.     be the case, but the 'number' requirement was non-specific so I just chose
  12.     the simplest type.
  13.  
  14.     There's also next to no error/input handling, and assumptions are made as to
  15.     os, md5sum being installed, and the ability to write in the local directory.
  16.     I did not see portability requirements specified, so I chose the path of
  17.     least resistance on all of these fronts.
  18.  
  19.     I left the original code intact, and tried to document my thoughts when
  20.     rewriting this. C isn't as scary as it initially seemed, I didn't have to
  21.     use malloc a single time :)
  22.  
  23.     Compiled with: gcc source.c -g -Wall -O3 -o gen
  24.  
  25.     Generated a million lines in half a second on the cheap vps I wrote it on, so
  26.     I think performance is ok.
  27. */
  28.  
  29.  
  30.  
  31. //#include "/usr/include/stdio.h"
  32. #include <stdio.h> // I think the compiler knows where to find this one lol
  33.  
  34. #include <stdlib.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <string.h>
  39. #include <time.h>
  40.  
  41. //void get_random_number(void *random){ //i'd rather avoid pointers thank you
  42. int get_random_number(void) {
  43.     int r = rand(); // not even random in the original implementation
  44.     //random = r; // see above
  45.     return r;
  46. }
  47.  
  48. /* See comments on line 93, and please stop putting tabs everywhere.
  49. char *getStringFromInt(long i, char**string)
  50. {
  51.  
  52.         char str[8];
  53.         sprintf(str, "%-d", i);
  54.     *string = str;
  55.         return str;
  56. }
  57. */
  58.  
  59. /* no thanks
  60. void write_string_to_file(char *random_number)
  61. {
  62.  
  63.         int fd = open("output.txt", O_WRONLY | O_APPEND, 0777);
  64.         write(fd, random_number, strlen(random_number));
  65.         return;
  66.  
  67. }
  68.  
  69. /* no thanks
  70. void printRandomNumber(char *file)
  71. {
  72.  
  73.         char *command = malloc(32);
  74.         sprintf(command, "md5sum %s", file);
  75.         system(command);
  76.         free(command);
  77.  
  78. }
  79. */
  80.  
  81. int main(int argc, char** argv)
  82. {
  83.     srand(time(NULL)); // make rand() actually pseudo-random
  84.  
  85.     FILE * fptr; // pointer for future filesystem interraction
  86.  
  87.     printf("About to generate ");
  88.     printf(argv[1]);
  89.     printf(" random numbers...\n");
  90.  
  91.     printf("Press 'y' to continue..  ");
  92.  
  93.     char answer[2]; // limited to 1 character for y/n, 2nd character null byte,
  94.  
  95.     //gets(answer); // 1. why are using the y/n variable 2. why are we using gets,
  96.                     // perhaps this is a memory exploration program
  97.  
  98.     //printf("answer: %s %p\n", answer, answer); // Why are we printing a pointer?
  99.                                                 // plus, answer isnt even set yet
  100.  
  101.     fgets(answer, 2, stdin); // accepts 1 character string, 2nd character is \0
  102.     printf("answer: %s\n", answer);
  103.  
  104.     char number[11]; // Up to 10 characters accepted as input for amount to generate
  105.                     // 32bit signed int max value is 10 characters anyway
  106.  
  107.  
  108.     //if(answer[0] == "y"){ // 'y' usually means the user wants to continue, not abort
  109.     if (answer[0] != 'Y' && answer[0] != 'y' && answer[0] != '\n') { // also count newline because entering y is annoying
  110.         printf("You answered %s, aborting\n",answer);
  111.         return 0;
  112.     }
  113.  
  114.     //short count = atoi(argv[1]); // limiting ourselves to 65k, 2.1b sounds like a better limit
  115.     int count = atoi(argv[1]);
  116.  
  117.     fptr = fopen("numgen.txt", "w");
  118.     if (fptr == NULL) {
  119.         printf("Cannot open file \"numgen.txt\", aborting.\n");
  120.         return 0;
  121.     }
  122.  
  123.     //while(--count){ // off by 1 error, 1 short
  124.     for(int i=0; i<count; i++) {
  125.         //int *a = malloc(4); // i'd like to avoid malloc when possible
  126.         int a = get_random_number(); // this is limited to 32bit signed ints for simplicity
  127.                                     // it could be a different datatype if numbers > 2.1b are needed
  128.         //get_random_number(a); // I like my approach above more
  129.  
  130.         //char *str;
  131.         //getStringFromInt(a, &str); // didn't even read this function, I definitely don't want to have an
  132.                                     // entire function declared which only does this
  133.  
  134.         char buffer[11]; // assuming 32 bit int again, 1 for terminator
  135.         snprintf(buffer, 10, "%d", a); // assuming we want a base 10 int of course :^)
  136.  
  137.         fprintf(fptr, "%s\n", buffer); // writes numbers to new file, numgen.txt, every time
  138.                                     // default perms, def not 777 like other function LOL
  139.  
  140.         //write_string_to_file(str); // why do we need a function for this, plus file only needs to be opened once
  141.         //printRandomNumber("output.txt");
  142.  
  143.     }
  144.  
  145.     fclose(fptr);
  146.  
  147.     system("md5sum numgen.txt\n"); // This isn't portable but realsticically it's what I'd use
  148.                                 // If I am supposed to make it portable, I could, but I'd rather just save
  149.                                 // time and complexity by assuming this is running on a linux system w/ md5sum
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement