Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a37f47d9f6 | ||
|
|
42264b6402 | ||
|
|
6189bdcf92 | ||
|
|
10d1aab05f | ||
|
|
ef1dcf7c39 | ||
|
|
40c48980b4 | ||
|
|
28e8aeb707 | ||
|
|
4607afed9a | ||
|
|
6ef3d4a660 | ||
|
|
40aa1b59e6 | ||
|
|
50964df129 | ||
|
|
8c8386740b | ||
|
|
6acaff36f7 | ||
|
|
1da9e4243e | ||
|
|
c8dce9dc0e |
80
.github/workflows/publish.yaml
vendored
Normal file
80
.github/workflows/publish.yaml
vendored
Normal 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
14
.github/workflows/validate.yaml
vendored
Normal 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
11
.gitignore
vendored
@@ -1,3 +1,8 @@
|
||||
.vscode
|
||||
build
|
||||
package/usr
|
||||
.*
|
||||
|
||||
!.github/
|
||||
!.gitignore
|
||||
|
||||
/bin/
|
||||
/obj/
|
||||
/compile_commands.json
|
||||
|
||||
32
makefile
Normal file
32
makefile
Normal 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)
|
||||
@@ -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"
|
||||
74
src/main.c
74
src/main.c
@@ -1,18 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#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 <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";
|
||||
}
|
||||
|
||||
printf("%s\n", result);
|
||||
return 0;
|
||||
noreturn static inline void error(char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user