1/*
2 * Copyright (C) 2004, 2005, 2007, 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: genrandom.c,v 1.7 2010/05/17 23:51:04 tbox Exp $ */
19
20/*! \file */
21#include <config.h>
22
23#include <isc/commandline.h>
24#include <isc/print.h>
25#include <isc/stdlib.h>
26#include <isc/util.h>
27
28#include <stdio.h>
29#include <string.h>
30
31const char *program = "genrandom";
32
33ISC_PLATFORM_NORETURN_PRE static void
34usage(void) ISC_PLATFORM_NORETURN_POST;
35
36static void
37usage(void) {
38	fprintf(stderr, "usage: %s [-n 2..9] k file\n", program);
39	exit(1);
40}
41
42static void
43generate(char *filename, unsigned int bytes) {
44	FILE *fp;
45
46	fp = fopen(filename, "w");
47	if (fp == NULL) {
48		printf("failed to open %s\n", filename);
49		exit(1);
50	}
51
52	while (bytes > 0) {
53#ifndef HAVE_ARC4RANDOM
54		unsigned short int x = (rand() & 0xFFFF);
55#else
56		unsigned short int x = (arc4random() & 0xFFFF);
57#endif
58		unsigned char c = x & 0xFF;
59		if (putc(c, fp) == EOF) {
60			printf("error writing to %s\n", filename);
61			exit(1);
62		}
63		c = x >> 8;
64		if (putc(c, fp) == EOF) {
65			printf("error writing to %s\n", filename);
66			exit(1);
67		}
68		bytes -= 2;
69	}
70	fclose(fp);
71}
72
73int
74main(int argc, char **argv) {
75	unsigned int bytes;
76	unsigned int k;
77	char *endp;
78	int c, i, n = 1;
79	size_t len;
80	char *name;
81
82	isc_commandline_errprint = ISC_FALSE;
83
84	while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) {
85		switch (c) {
86		case 'n':
87			n = strtol(isc_commandline_argument, &endp, 10);
88			if ((*endp != 0) || (n <= 1) || (n > 9))
89				usage();
90			break;
91
92		case '?':
93			if (isc_commandline_option != '?')
94				fprintf(stderr, "%s: invalid argument -%c\n",
95					program, isc_commandline_option);
96		case 'h':
97			usage();
98
99		default:
100			fprintf(stderr, "%s: unhandled option -%c\n",
101				program, isc_commandline_option);
102			exit(1);
103		}
104	}
105
106	if (isc_commandline_index + 2 != argc)
107		usage();
108
109	k = strtoul(argv[isc_commandline_index++], &endp, 10);
110	if (*endp != 0)
111		usage();
112	bytes = k << 10;
113
114#ifndef HAVE_ARC4RANDOM
115	srand(0x12345678);
116#endif
117	if (n == 1) {
118		generate(argv[isc_commandline_index], bytes);
119		return (0);
120	}
121
122	len = strlen(argv[isc_commandline_index]) + 2;
123	name = (char *) malloc(len);
124	if (name == NULL) {
125		perror("malloc");
126		exit(1);
127	}
128
129	for (i = 1; i <= n; i++) {
130		snprintf(name, len, "%s%d", argv[isc_commandline_index], i);
131		generate(name, bytes);
132	}
133	free(name);
134
135	return (0);
136}
137