15 Commits
v1.0 ... v1.1

Author SHA1 Message Date
Quinn
a37f47d9f6 fix: no such thing as $default-branch 2025-04-09 15:42:25 +02:00
Quinn
42264b6402 fix: compiler wasn't available 2025-04-09 15:35:53 +02:00
Quinn
6189bdcf92 fix: bear not available 2025-04-09 15:35:53 +02:00
Quinn
10d1aab05f add new gh workflows / most of it 2025-04-09 15:35:11 +02:00
Quinn
ef1dcf7c39 small fixes in the code, mainly to ensure things work properly. 2025-04-09 14:57:14 +02:00
Quinn
40c48980b4 use makefile build system 2025-04-09 14:56:24 +02:00
Quinn
28e8aeb707 fix: forgot newlime innlog message 2025-04-06 17:34:01 +02:00
Quinn
4607afed9a use 64 bit integers for count, rather than 32 bit.
Was facing issues with integer wrapping
2025-04-06 17:32:26 +02:00
Quinn
6ef3d4a660 add bounds to numbered input 2025-04-06 17:27:55 +02:00
Quinn
40aa1b59e6 add error management script 2025-04-06 17:23:22 +02:00
Quinn
50964df129 refactor and pedantic fixes 2025-04-06 17:22:56 +02:00
Quinn
8c8386740b Fix: no need for the if condition there
Instead check if no arguments were given and exit sooner.
2025-04-06 16:55:54 +02:00
Quinn
6acaff36f7 remove package directory 2025-04-06 15:55:47 +02:00
Quinn
1da9e4243e allow for parsing command line arguments for multiple coinflips at once 2025-04-06 15:55:33 +02:00
unset
c8dce9dc0e improved random seed to be less predictable 2025-01-30 16:09:23 +01:00
6 changed files with 192 additions and 25 deletions

80
.github/workflows/publish.yaml vendored Normal file
View File

@@ -0,0 +1,80 @@
name: publish
on:
release:
types: published
workflow_dispatch:
inputs:
release_tag:
required: true
description: the tag to release for
default: ""
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make compile
- uses: actions/upload-artifact@v4
with:
name: bin
path: bin/
# release_deb:
# runs-on: ubuntu-latest
# needs: compile
# steps:
# - uses: actions/download-artifact@v4
# with:
# name: bin
# path: ${{github.workspace}}/bin
# - if: ${{!env.ACT}}
# uses: softprops/action-gh-release@v2
# with:
# tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
# files: bin/coinflip
# fail_on_unmatched_files: true
# env:
# GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
# - if: ${{env.ACT}}
# uses: actions/upload-artifact@v4
# with:
# path: bin/coinflip
# release_aur-bin:
# runs-on: ubuntu-latest
# needs: compile
# steps:
# - uses: actions/download-artifact@v4
# with:
# name: bin
# path: ${{github.workspace}}/bin
# - if: ${{!env.ACT}}
# uses: softprops/action-gh-release@v2
# with:
# tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
# files: bin/coinflip
# fail_on_unmatched_files: true
# env:
# GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
# - if: ${{env.ACT}}
# uses: actions/upload-artifact@v4
# with:
# path: bin/coinflip
release_bin:
runs-on: ubuntu-latest
needs: compile
steps:
- if: ${{!env.ACT}}
uses: actions/download-artifact@v4
with:
name: bin
path: ${{github.workspace}}/bin
- uses: softprops/action-gh-release@v2
with:
tag_name: ${{github.event.release.tag_name || github.event.inputs.release_tag}}
files: bin/coinflip
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- if: ${{env.ACT}}
uses: actions/upload-artifact@v4
with:
path: bin/coinflip

14
.github/workflows/validate.yaml vendored Normal file
View File

@@ -0,0 +1,14 @@
name: validate
on:
pull_request:
branches:
- '**'
push:
branches:
- '**'
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make compile

11
.gitignore vendored
View File

@@ -1,3 +1,8 @@
.vscode
build
package/usr
.*
!.github/
!.gitignore
/bin/
/obj/
/compile_commands.json

32
makefile Normal file
View File

@@ -0,0 +1,32 @@
CC := x86_64-linux-gnu-gcc
CFLAGS := -Wall -Wextra -Wpedantic -O2 -std=gnu17
LDFLAGS :=
SRC = $(shell find src/ -name '*.c')
OBJ := $(patsubst src/%,obj/%,$(SRC:.c=.o))
BIN := bin/coinflip
compile: compile_commands.json $(BIN)
clean:
rm -rf obj/ bin/ compile_commands.json
# link the .o files
$(BIN): $(OBJ)
@mkdir -p $(@D)
$(CC) $(LDFLAGS) $(OBJ) -o $(BIN)
# compile .o files (w/ .d files)
obj/%.o: src/%.c
@mkdir -p $(@D)
$(CC) -c -MD -MP $(CFLAGS) -o $@ $<
ifneq ($(shell which bear),)
compile_commands.json: makefile
$(MAKE) clean
@touch compile_commands.json
bear -- make compile
else
compile_commands.json:
endif
-include $(OBJ:.o=.d)

View File

@@ -1,6 +0,0 @@
Package: coinflip
Version: 1.0
Architecture: all
Priority: optional
Maintainer: Quinn
Description: adds a coinflip command which has a 50% chance of printing "heads" and 50% chance of printing "tails"

View File

@@ -1,18 +1,60 @@
#include <features.h>
#if _POSIX_C_SOURCE >= 199309L
# include <bits/time.h>
# include <errno.h>
# include <limits.h>
# include <stdarg.h>
# include <stdint.h>
# include <stdio.h>
#include <time.h>
# include <stdlib.h>
# include <stdnoreturn.h>
# include <time.h>
int main(void) {
srand(time(NULL)); // set the random seed to the system time
char* result = NULL;
if (rand() & (~1) != 0) {
result = "heads";
}
else {
result = "tails";
noreturn static inline void error(char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(1);
}
printf("%s\n", result);
int main(int argc, char** argv) {
// set the seed to the number of seconds xor with nanoseconds
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds
}
// just print whether it's heads or tails
if (argc == 1) {
printf("%s\n", (rand() & 1) ? "heads" : "tails");
return 0;
}
// if we have input; perform coin flips for those times
for (uint32_t i = 1; i < (unsigned)argc; ++i) {
int n = 0; // contains the random data
uint64_t headsc = 0; // amount of heads
uint64_t tailsc = 0; // amount of tails
// get the integer from the string, return 1 if an error occurred
errno = 0;
long const c = strtol(argv[i], NULL, 10);
if (errno == EINVAL) error("syntax error for string: %s\n", argv[i]);
if (c < 1 || c > (LONG_MAX - 1)) error("count may only be in between 1 and %l. Received %l\n", LONG_MAX, c);
// perform for the input count
for (long j = 0; j < c; ++j) {
if (n == 0) n = rand(); // get a new random number if the random data ran out
(void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter
n >>= 1; // shift the random integer right by 1
}
// print the results of this cycle
printf("results:\n heads: %lu\n tails: %lu\n", headsc, tailsc);
}
}
#else
# error "platform unsupported"
#endif