20 static random_fn random_generate;
21 static bool_t random_generate(void *data, uint32_t bytes, uint8_t *buff)
23 struct rgen_data *st=data;
25 /* XXX XXX error checking */
26 read(st->fd,buff,bytes);
31 static list_t *random_apply(closure_t *self, struct cloc loc,
32 dict_t *context, list_t *args)
36 string_t filename=NULL;
38 st=safe_malloc(sizeof(*st),"random_apply");
40 st->cl.description="randomsource";
41 st->cl.type=CL_RANDOMSRC;
43 st->cl.interface=&st->ops;
45 st->ops.blocking=False;
46 st->ops.generate=random_generate;
49 arg1=list_elem(args,0);
50 arg2=list_elem(args,1);
53 fatal("randomsource: requires a filename\n");
55 if (arg1->type != t_string) {
56 cfgfatal(arg1->loc,"randomsource",
57 "filename (arg1) must be a string\n");
59 filename=arg1->data.string;
62 if (arg2->type != t_bool) {
63 cfgfatal(arg2->loc,"randomsource",
64 "blocking parameter (arg2) must be bool\n");
66 st->ops.blocking=arg2->data.bool;
70 fatal("randomsource requires a filename");
72 st->fd=open(filename,O_RDONLY);
74 fatal_perror("randomsource (%s:%d): cannot open %s",arg1->loc.file,
75 arg1->loc.line,filename);
77 return new_closure(&st->cl);
80 void random_module(dict_t *d)
82 add_closure(d,"randomfile",random_apply);