No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tools / genrandom.c
blob12fe8b975e1be0c8fc28e07a08bd80a167c8e357
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: genrandom.c,v 1.4 2009/03/07 23:47:45 tbox Exp */
22 /*! \file */
23 #include <config.h>
25 #include <stdio.h>
26 #include <time.h>
28 #include <isc/stdlib.h>
30 int
31 main(int argc, char **argv) {
32 unsigned int bytes;
33 unsigned int k;
34 char *endp;
35 FILE *fp;
37 if (argc != 3) {
38 printf("usage: genrandom k file\n");
39 exit(1);
41 k = strtoul(argv[1], &endp, 10);
42 if (*endp != 0) {
43 printf("usage: genrandom k file\n");
44 exit(1);
46 bytes = k << 10;
48 fp = fopen(argv[2], "w");
49 if (fp == NULL) {
50 printf("failed to open %s\n", argv[2]);
51 exit(1);
54 #ifndef HAVE_ARC4RANDOM
55 srand(0x12345678);
56 #endif
57 while (bytes > 0) {
58 #ifndef HAVE_ARC4RANDOM
59 unsigned short int x = (rand() & 0xFFFF);
60 #else
61 unsigned short int x = (arc4random() & 0xFFFF);
62 #endif
63 unsigned char c = x & 0xFF;
64 if (putc(c, fp) == EOF) {
65 printf("error writing to file\n");
66 exit(1);
68 c = x >> 8;
69 if (putc(c, fp) == EOF) {
70 printf("error writing to file\n");
71 exit(1);
73 bytes -= 2;
75 fclose(fp);
77 return (0);