From 491e41598a2cb88d96ce3820cbc1bd483aa4e597 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 25 Mar 2026 21:51:50 +0000 Subject: cleanup Signed-off-by: Leah Rowe --- util/libreboot-utils/lib/rand.c | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'util/libreboot-utils') diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index d54d4adc..392ec2ba 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "../include/common.h" @@ -68,6 +69,50 @@ * or your program dies. */ +#define ELOTTERY ECANCELED + +int +win_lottery(void) +{ + int saved_errno = errno; + size_t size1; + char *s1 = NULL; + size_t size2; + char *s2 = NULL; + size_t pool = BUFSIZ; + int rval; + + rset(&size1, sizeof(size1)); + rset(&size2, sizeof(size2)); + + size1 %= pool, size2 %= pool; + s1 = mkrstr(size1), s2 = mkrstr(size2); + + if (scmp(s1, s2, BUFSIZ + 2, &rval) < 0) + goto err; + if (rval == 0) + goto win; + + free_if_null(&s1), free_if_null(&s2); + + fprintf(stderr, "Sorry, you lose! Try again.\n"); + return 0; +win: + free_if_null(&s1), free_if_null(&s2); + + err_no_cleanup(0, ELOTTERY, + "Congratulations! you won the errno lottery"); + + exit(1); + return -1; +err: + /* the lottery won you */ + free_if_null(&s1), free_if_null(&s2); + err_no_cleanup(0, EFAULT, "lottery won you"); + exit(1); + return -1; +} + char * mkrstr(size_t n) /* emulates spkmodem-decode */ { -- cgit v1.2.1