initial commit

master
Rachel Lambda Samuelsson 2021-12-11 14:26:39 +01:00
commit 451b1d1b6b
6 changed files with 168 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
*.o
secd

18
Makefile 100644
View File

@ -0,0 +1,18 @@
.POSIX:
.PHONY: all clean
DEPS=src/main.o src/util.o
all: secd
secd: $(DEPS)
$(CC) $(CFLAGS) $(DEPS) -o secd
run: secd
@./secd
clean:
rm -f $(DEPS)
.SUFFIXES: .c .o
.c.o:
$(CC) -c $(CFLAGS) $< -o $@

31
src/dbg.h 100644
View File

@ -0,0 +1,31 @@
#ifndef __DBG_H
#define __DBG_H
#include <stdio.h>
#ifdef ENABLE_DBG_H
# define DEBUG(fmt, ...) fprintf(stderr, "[%s:%s:%d]: " fmt "\n", \
__FILE__, __func__, __LINE__, __VA_ARGS__)
# define VALFMTS "[%s:%s():%d] %s: "
# define VALFMTP "\n"
# define VAL(X) fprintf(stderr, _Generic((X), \
char: VALFMTS "%c" VALFMTP, \
unsigned char: VALFMTS "%hhu" VALFMTP, \
short: VALFMTS "%h" VALFMTP, \
unsigned short: VALFMTS "%hu" VALFMTP, \
int: VALFMTS "%d" VALFMTP, \
unsigned int: VALFMTS "%u" VALFMTP, \
long: VALFMTS "%l" VALFMTP, \
unsigned long: VALFMTS "%lu" VALFMTP, \
char*: VALFMTS "'%s'" VALFMTP, \
default: VALFMTS "0x%x" VALFMTP\
), __FILE__, __func__, __LINE__, #X, X)
#else
# define DEBUG(...)
# define VAL(_)
#endif
#endif

5
src/main.c 100644
View File

@ -0,0 +1,5 @@
#include <stdio.h>
#include "dbg.h"
int main(int argc, char** argv) {
}

97
src/util.c 100644
View File

@ -0,0 +1,97 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "dbg.h"
#include "util.h"
#ifdef POSIX
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
// print message to stderr and then exit
void die(const char* s) {
fprintf(stderr, "FATAL ERROR: %s.\n", s);
exit(1);
}
// print perror message to stderr and then exit
void diePerror(const char* s) {
perror(s);
exit(1);
}
// malloc which dies on failure
void* xmalloc(size_t bytes) {
void* p = malloc(bytes);
GUARD(p);
return p;
}
// realloc which dies on failure
void* xrealloc(void* p, size_t bytes) {
p = realloc(p, bytes);
GUARD(p);
return p;
}
// free which is garantueed null pointer safe
void xfree(void* p) {
if (p) {
free(p);
}
}
// read the file at the given path and return a byte buffer
// pointing to the contents of the file, the length of which
// is stored in rsize
uint8_t* readFile(char* path, size_t* rsize) {
size_t size = 0;
uint8_t* buf = 0;
#ifdef POSIX
int fd = 0;
fd = open(path, O_RDONLY);
SGUARD(fd);
size = lseek(fd, 0, SEEK_END);
SGUARD(size);
buf = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
SGUARD(buf);
SGUARD(close(fd));
#else
FILE* fp = 0;
fp = fopen(path, "rb");
GUARD(fp);
GUARD(fseek(fp, 0, SEEK_END));
size = ftell(fp);
rewind(fp);
buf = xmalloc(size);
GUARD(fread(buf, 1, size, fp) == size);
GUARD(fclose(fp));
#endif
*rsize = size;
return buf;
}
// free read file
void freeFile(uint8_t* buf, size_t size) {
#ifdef POSIX
SGUARD(munmap(buf, size));
#else
xfree(buf);
#endif
}

15
src/util.h 100644
View File

@ -0,0 +1,15 @@
#ifndef _UTIL_H
#define _UTIL_H
#define GUARD(p) if (!p) { diePerror(__func__); }
#define SGUARD(p) if (p < 0) { diePerror(__func__); }
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#define POSIX
#endif
#ifdef __WIN32__
#define WINDOWS
#endif
#endif