Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * randasc.c
- * Generates random ASCII bytes (values 0x20 to 0x7E)
- * Copyright 2012 ctzmsc3
- * License: Do whatever you want.
- */
- #include <stdio.h>
- #include <stdlib.h>
- int
- main(int argc, char** argv)
- /* first arg is number of chars desired
- * second is number of chars per line before line break
- (defaults to 0, or no breaks)
- * third is randomness source (defaults to /dev/urandom)
- */
- {
- FILE* rando;
- int c, n, i;
- unsigned int s=0;
- char* src;
- char a;
- if (argc < 2)
- {
- fprintf(stderr, "Error: not enough args\n");
- return 1;
- }
- n=atoi(argv[1]);
- if (argc >= 3)
- s=atoi(argv[2]);
- if (argc >= 4)
- src=argv[3];
- else
- src="/dev/urandom";
- rando=fopen(src, "r");
- if (rando == NULL)
- {
- fprintf(stderr, "Error: could not open %s\n", src);
- return 1;
- }
- /*do stuff*/
- i=0;
- do
- {
- c=getc(rando);
- if (c == EOF)
- {
- fprintf(stderr, "Error: EOF before finish\n");
- fclose(rando);
- return 1;
- }
- a=c%128; /*eliminate MSB*/
- if (a >= 0x21)
- {
- putchar(a-1); /*eliminate 7f*/
- i++;
- if (s && !(i%s))
- putchar('\n');
- }
- } while (i<n);
- fclose(rando);
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment