summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/rand.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-25 21:51:50 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-26 06:59:42 +0000
commitdc7a02da2dd65fd7d9d4785bda9bf5467fc660c0 (patch)
tree18f7db715525567798572a92a038c18e1f4ab0db /util/libreboot-utils/lib/rand.c
parentc66f381e9062f0e75e2731e6b6774c2090933e75 (diff)
cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/rand.c')
-rw-r--r--util/libreboot-utils/lib/rand.c45
1 files changed, 45 insertions, 0 deletions
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 <stdlib.h>
#include <string.h>
#include <stdint.h>
+#include <stdio.h>
#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 */
{