~ubuntu-branches/ubuntu/natty/bind9/natty-updates

« back to all changes in this revision

Viewing changes to bin/tools/genrandom.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones, Internet Software Consortium, Inc, LaMont Jones
  • Date: 2010-06-21 09:53:30 UTC
  • mfrom: (1.6.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100621095330-k6874kbj59lfo3xm
Tags: 1:9.7.1.dfsg-1
[Internet Software Consortium, Inc]

* 9.7.1

[LaMont Jones]

* Add freebsd support.  Closes: #578447
* soname changes
* freshen root cache.  LP: #596363

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
 
2
 * Copyright (C) 2004, 2005, 2007, 2009, 2010  Internet Systems Consortium, Inc. ("ISC")
3
3
 * Copyright (C) 2000-2003  Internet Software Consortium.
4
4
 *
5
5
 * Permission to use, copy, modify, and/or distribute this software for any
15
15
 * PERFORMANCE OF THIS SOFTWARE.
16
16
 */
17
17
 
18
 
/* $Id: genrandom.c,v 1.4 2009/03/07 23:47:45 tbox Exp $ */
 
18
/* $Id: genrandom.c,v 1.4.152.3 2010/05/17 23:49:51 tbox Exp $ */
19
19
 
20
20
/*! \file */
21
21
#include <config.h>
22
22
 
 
23
#include <isc/commandline.h>
 
24
#include <isc/print.h>
 
25
#include <isc/stdlib.h>
 
26
#include <isc/util.h>
 
27
 
23
28
#include <stdio.h>
24
 
#include <time.h>
25
 
 
26
 
#include <isc/stdlib.h>
27
 
 
28
 
int
29
 
main(int argc, char **argv) {
30
 
        unsigned int bytes;
31
 
        unsigned int k;
32
 
        char *endp;
 
29
#include <string.h>
 
30
 
 
31
const char *program = "genrandom";
 
32
 
 
33
ISC_PLATFORM_NORETURN_PRE static void
 
34
usage(void) ISC_PLATFORM_NORETURN_POST;
 
35
 
 
36
static void
 
37
usage(void) {
 
38
        fprintf(stderr, "usage: %s [-n 2..9] k file\n", program);
 
39
        exit(1);
 
40
}
 
41
 
 
42
static void
 
43
generate(char *filename, unsigned int bytes) {
33
44
        FILE *fp;
34
45
 
35
 
        if (argc != 3) {
36
 
                printf("usage: genrandom k file\n");
37
 
                exit(1);
38
 
        }
39
 
        k = strtoul(argv[1], &endp, 10);
40
 
        if (*endp != 0) {
41
 
                printf("usage: genrandom k file\n");
42
 
                exit(1);
43
 
        }
44
 
        bytes = k << 10;
45
 
 
46
 
        fp = fopen(argv[2], "w");
 
46
        fp = fopen(filename, "w");
47
47
        if (fp == NULL) {
48
 
                printf("failed to open %s\n", argv[2]);
 
48
                printf("failed to open %s\n", filename);
49
49
                exit(1);
50
50
        }
51
51
 
52
 
#ifndef HAVE_ARC4RANDOM
53
 
        srand(0x12345678);
54
 
#endif
55
52
        while (bytes > 0) {
56
53
#ifndef HAVE_ARC4RANDOM
57
54
                unsigned short int x = (rand() & 0xFFFF);
60
57
#endif
61
58
                unsigned char c = x & 0xFF;
62
59
                if (putc(c, fp) == EOF) {
63
 
                        printf("error writing to file\n");
 
60
                        printf("error writing to %s\n", filename);
64
61
                        exit(1);
65
62
                }
66
63
                c = x >> 8;
67
64
                if (putc(c, fp) == EOF) {
68
 
                        printf("error writing to file\n");
 
65
                        printf("error writing to %s\n", filename);
69
66
                        exit(1);
70
67
                }
71
68
                bytes -= 2;
72
69
        }
73
70
        fclose(fp);
 
71
}
 
72
 
 
73
int
 
74
main(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);
74
134
 
75
135
        return (0);
76
136
}