You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

randompass.c 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Generate text using the linux /dev/random device. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. static const char *allowed_symbols = "-+;!%=,.-#&@$~";
  7. char get_valid_char(FILE *fp);
  8. int main(void)
  9. {
  10. int len;
  11. int loopster;
  12. char *gentext;
  13. FILE *fp;
  14. printf("How long do you want the random string to be? ");
  15. scanf("%d", &len);
  16. gentext = malloc(len + 1);
  17. fp = fopen("/dev/random", "rb");
  18. printf("Generating... ");
  19. fflush(stdout);
  20. for(loopster = 0; loopster < len; loopster++)
  21. *(gentext + loopster) = get_valid_char(fp);
  22. *(gentext + len) = 0;
  23. printf("The string you requested: \"%s\"\n", gentext);
  24. fclose(fp);
  25. free(gentext);
  26. return 0;
  27. }
  28. char get_valid_char(FILE *fp)
  29. {
  30. char read_byte;
  31. int loopster;
  32. READ_LOOP_BEGIN:
  33. read_byte = getc(fp);
  34. if(isalnum(read_byte))
  35. goto READ_LOOP_END;
  36. for(loopster = 0; loopster < strlen(allowed_symbols); loopster++)
  37. if(read_byte == *(allowed_symbols + loopster))
  38. goto READ_LOOP_END;
  39. goto READ_LOOP_BEGIN;
  40. READ_LOOP_END:
  41. return read_byte;
  42. }