// priestmasters random nop sled generator <priest@priestmaster.org>
// Do what you want with this file.

#include <stdio.h>

// Generate random nop data.
int gen_nops(char *buffer, int size)
{
	int i, num;
	FILE *file;

	// Open urandom for srandom
	file = fopen("/dev/urandom", "r");

	// Iterate size times
	for(i=0;i<size;i++)
	{
		// Read random data for srandom
		num = fgetc(file);
	
		// Set random number seed
		srandom(num);

		// Get random value
	        num = random() % 26;
	
		// Add num to random value, to print always upper chars.
		buffer[i] = 65 + num;
	}

	// close urandom
	fclose(file);
	
	return 0;
}

// Print this if the start of the program fails
void usage(char *argv)
{
	printf("\nUsage: %s count\n\n", argv);
}


int main(int argc, char **argv)
{
	char *buf;
	int count;	

	// Test for parameters
	if(argc != 2)
	{
		usage(argv[0]);
		exit(-1);
	}

	// Read first parameter and convert it
	count = atoi(argv[1]);

	// Check for negative numbers
	if(count <= 0)
	{
		printf("\nOnly positive numbers allowed\n\n");
		exit(-1);
	}


	// Get memory for chars
	buf = (char *) malloc(count);
	
	// Launch get_nops with buf and count
	gen_nops(buf, count);

	// Null terminate buffer
	buf[count] = 0;

	// Print random data
	printf("%s", buf);
}
