rc Rand/crypto utility
This commit is contained in:
2
user/rc/.gitignore
vendored
Normal file
2
user/rc/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.o
|
||||||
|
rc
|
||||||
24
user/rc/Makefile
Normal file
24
user/rc/Makefile
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
include $(ROOT)/mk/grabsrc.mk
|
||||||
|
include ../Makefile.inc
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
TARGET := rc
|
||||||
|
|
||||||
|
LDFLAGS += -L$(ROOT)/ulib -l:libulib.a
|
||||||
|
|
||||||
|
SRCFILES := $(call GRABSRC, .)
|
||||||
|
CFILES := $(call GET_CFILES, $(SRCFILES))
|
||||||
|
OBJ := $(call GET_OBJ, $(SRCFILES))
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJ)
|
||||||
|
$(LD) $^ $(LDFLAGS) -o $@
|
||||||
|
echo $$(realpath $(TARGET)) >> $(FILES)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJ) $(TARGET)
|
||||||
24
user/rc/main.c
Normal file
24
user/rc/main.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <ulib.h>
|
||||||
|
|
||||||
|
#define CMDS(X) \
|
||||||
|
X(rr) \
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
if (argslen() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *cmd = args()[0];
|
||||||
|
|
||||||
|
#define X(name) if (string_strcmp(cmd, #name) == 0) { \
|
||||||
|
extern void rc_ ## name(void); \
|
||||||
|
rc_ ## name(); \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
CMDS(X)
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
uprintf("rc: unknown command %s\n", cmd);
|
||||||
|
}
|
||||||
50
user/rc/rr.c
Normal file
50
user/rc/rr.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <ulib.h>
|
||||||
|
|
||||||
|
int32_t rr(int32_t min, int32_t max) {
|
||||||
|
if (max <= min)
|
||||||
|
return min;
|
||||||
|
|
||||||
|
uint32_t range = (uint32_t)(max - min + 1);
|
||||||
|
uint32_t val;
|
||||||
|
int32_t rnd;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 10; i++) {
|
||||||
|
rnd = rand();
|
||||||
|
if (rnd == -1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
val = (uint32_t)rnd;
|
||||||
|
|
||||||
|
uint32_t limit = (uint32_t)(-range) % range;
|
||||||
|
if (val >= limit) {
|
||||||
|
return min + (val % range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rc_rr(void) {
|
||||||
|
if (argslen() < 3) {
|
||||||
|
uprintf("rc: Not enough arguments\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *beginstr = *(args()+1);
|
||||||
|
char *endstr = *(args()+2);
|
||||||
|
|
||||||
|
char *endb;
|
||||||
|
long begin = string_conv_strtol(beginstr, &endb, 10);
|
||||||
|
char *ende;
|
||||||
|
long end = string_conv_strtol(endstr, &ende, 10);
|
||||||
|
|
||||||
|
int32_t r = rr(begin, end);
|
||||||
|
if (rr == -1) {
|
||||||
|
uprintf("rc: failed to generate random number in range\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uprintf("%d\n", r);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user