Start making the code initialization logic

This commit is contained in:
Z. Charles Dziura 2025-06-02 20:36:04 -04:00
parent 325116e15f
commit 018003ebd4
9 changed files with 65 additions and 4 deletions

View file

@ -1 +1,7 @@
SUBDIRS = src
AM_CFLAGS = -std=c17
.PHONY: run
run:
./src/fckgw

View file

@ -2,6 +2,5 @@ AC_INIT([fckgw],[1.0])
AC_CONFIG_SRCDIR([src/main.c])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
CFLAGS="$CFLAGS -std=c17"
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT

View file

@ -1,2 +1,2 @@
bin_PROGRAMS = fckgw
fckgw_SOURCES = main.c api_key.c
fckgw_SOURCES = main.c code.c

View file

View file

24
src/code.c Normal file
View file

@ -0,0 +1,24 @@
#include <stdint.h>
#include <stdio.h>
#include <sys/random.h>
#include <time.h>
#include "code.h"
#include "errors.h"
int code_init(struct code *key) {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("Unable to read clock 'CLOCK_REALTIME'");
return ERR_CLOCK_READ;
}
key->unix_ts_sec = ts.tv_sec;
key->unix_ts_msec = ts.tv_nsec / 1000000;
key->purpose = ACCESS;
return 0;
}

23
src/code.h Normal file
View file

@ -0,0 +1,23 @@
#include <stdint.h>
#include <time.h>
#ifdef __SIZEOF_INT128__
typedef unsigned __int128 uint128_t;
#endif
enum code_purpose {
ACCESS,
DISCOUNT,
};
struct code {
time_t unix_ts_sec;
long unix_ts_msec;
enum code_purpose purpose;
uint16_t rand_a;
uint64_t rand_b;
};
int code_init(struct code *key);
uint128_t code_convert_to_num(struct code *key);

1
src/errors.h Normal file
View file

@ -0,0 +1 @@
#define ERR_CLOCK_READ 1

View file

@ -1,6 +1,14 @@
#include <stdio.h>
#include <./api_key.h>
#include "code.h"
int main(void) {
return 0;
struct code key;
int key_init_err = code_init(&key);
if (key_init_err != 0) {
return key_init_err;
}
printf("unix_ts_sec: %ld, unix_ts_nsec: %ld, purpose: %d\n", key.unix_ts_sec, key.unix_ts_msec, key.purpose);
}