File: rand.c

package info (click to toggle)
libdumbnet 1.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,084 kB
  • sloc: ansic: 11,563; sh: 4,732; python: 226; makefile: 92
file content (60 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * rand.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id$
 */

#include "config.h"

#include <sys/types.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "dumbnet.h"
#include "mod.h"

void
rand_usage(void)
{
	fprintf(stderr, "Usage: dnet rand <len>\n");
	exit(1);
}

int
rand_main(int argc, char *argv[])
{
	rand_t *r;
	int len;
	u_char *p;
	
	if (argc != 2 || *(argv[1]) == '-')
		rand_usage();
	
	if ((len = atoi(argv[1])) == 0)
		rand_usage();

	if ((p = malloc(len)) == NULL)
		err(1, "malloc");

	if ((r = rand_open()) == NULL)
		err(1, "rand_init");

	if (rand_get(r, p, len) < 0)
		err(1, "rand_get");
	
	if (write(STDOUT_FILENO, p, len) != len)
		err(1, "write");
	
	return (0);
}

struct mod mod_rand = {
	"rand",
	MOD_TYPE_DATA,
	rand_main
};