forked from freifunk-franken/firmware
fff-random: rewrite in C
Fixes: #51 Signed-off-by: Tim Niemeyer <tim@tn-x.org> Tested-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
This commit is contained in:
parent
15d514bbd1
commit
daa613722c
|
@ -1,40 +1,41 @@
|
||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=fff-random
|
PKG_NAME:=fff-random
|
||||||
PKG_VERSION:=0.0.1
|
PKG_VERSION:=2
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=1
|
||||||
|
|
||||||
PKG_BUILD_DIR:=$(BUILD_DIR)/fff-random
|
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
||||||
|
|
||||||
include $(INCLUDE_DIR)/package.mk
|
include $(INCLUDE_DIR)/package.mk
|
||||||
|
|
||||||
define Package/fff-random
|
define Package/fff-random
|
||||||
SECTION:=base
|
SECTION:=base
|
||||||
CATEGORY:=Freifunk
|
CATEGORY:=Freifunk
|
||||||
TITLE:=Freifunk-Franken Random
|
TITLE:=Freifunk-Franken Random
|
||||||
URL:=http://www.freifunk-franken.de
|
URL:=http://www.freifunk-franken.de
|
||||||
DEPENDS:=+@BUSYBOX_CONFIG_ASH
|
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Package/fff-random/description
|
define Package/fff-random/description
|
||||||
This is the Freifunk Franken Firmware Random package
|
This is the Freifunk Franken Firmware Random package
|
||||||
This packages provides a random function.
|
This packages provides a random function.
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Build/Prepare
|
define Build/Prepare
|
||||||
echo "all: " > $(PKG_BUILD_DIR)/Makefile
|
mkdir -p $(PKG_BUILD_DIR)
|
||||||
|
$(CP) ./src/* $(PKG_BUILD_DIR)/
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Build/Configure
|
define Build/Configure
|
||||||
# nothing
|
# nothing
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Build/Compile
|
define Build/Compile
|
||||||
# nothing
|
CFLAGS="$(TARGET_CFLAGS)" CPPFLAGS="$(TARGET_CPPFLAGS)" $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Package/fff-random/install
|
define Package/fff-random/install
|
||||||
$(CP) ./files/* $(1)/
|
$(INSTALL_DIR) $(1)/usr/bin
|
||||||
|
$(INSTALL_BIN) $(PKG_BUILD_DIR)/random $(1)/usr/bin/
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(eval $(call BuildPackage,fff-random))
|
$(eval $(call BuildPackage,fff-random))
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
# Freifunk Franken Random Number Script
|
|
||||||
# Tim Niemeyer
|
|
||||||
# 29.11.2015
|
|
||||||
# License GPLv2
|
|
||||||
|
|
||||||
FROM=${1:-0}
|
|
||||||
UNTIL=${2:-100}
|
|
||||||
|
|
||||||
diff=$(( UNTIL - FROM ))
|
|
||||||
numbers=$(( $(echo $diff | wc -c) -1 ))
|
|
||||||
|
|
||||||
rand=$(</dev/urandom tr -dc 0-9 | head -c $numbers | sed -e 's/^0*//g')
|
|
||||||
|
|
||||||
echo $(( (rand % diff) + FROM ))
|
|
||||||
|
|
4
src/packages/fff/fff-random/src/Makefile
Normal file
4
src/packages/fff/fff-random/src/Makefile
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
all: random
|
||||||
|
|
||||||
|
random: random.c
|
||||||
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)
|
53
src/packages/fff/fff-random/src/random.c
Normal file
53
src/packages/fff/fff-random/src/random.c
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Simple random tool, used to get a random value
|
||||||
|
* 2017-07-02
|
||||||
|
* Tim Niemeyer
|
||||||
|
* GPLv2
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int from = 0;
|
||||||
|
int to = 100;
|
||||||
|
int diff = 1;
|
||||||
|
FILE *f = 0;
|
||||||
|
unsigned int r = 0;
|
||||||
|
|
||||||
|
if (argc != 1 && argc != 3)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s <from> <to>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else if (argc == 3)
|
||||||
|
{
|
||||||
|
from = atoi(argv[1]);
|
||||||
|
to = atoi(argv[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
diff = to - from;
|
||||||
|
if (diff <= 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Bad from/to\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
f = fopen("/dev/urandom", "r");
|
||||||
|
if (!f)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Can't open /dev/urandom\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1U != fread(&r, sizeof(unsigned int), 1U, f))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Can't read /dev/urandom\n");
|
||||||
|
fclose(f);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%u\n", (r % (diff +1 )) + from);
|
||||||
|
fclose(f);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user