2024-02-23 22:56:10 -08:00
|
|
|
/*
|
|
|
|
* util.c - Utility functions
|
|
|
|
* Copyright (C) 2024 Alexander Rosenberg
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version. See the LICENSE file for more information.
|
|
|
|
*/
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <err.h>
|
2024-02-25 23:59:26 -08:00
|
|
|
#include <string.h>
|
2024-02-23 22:56:10 -08:00
|
|
|
|
2024-02-25 23:59:26 -08:00
|
|
|
Options GLOBAL_OPTS;
|
|
|
|
|
|
|
|
void cleanup_options(Options *opts) {
|
|
|
|
FREE_CHECKED(opts->config_path);
|
|
|
|
FREE_CHECKED(opts->gpio_path);
|
|
|
|
}
|
2024-02-23 22:56:10 -08:00
|
|
|
|
|
|
|
void *malloc_checked(size_t n) {
|
|
|
|
return realloc_checked(NULL, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *realloc_checked(void *old_ptr, size_t n) {
|
|
|
|
void *ptr = realloc(old_ptr, n);
|
|
|
|
if (n && !ptr) {
|
|
|
|
errx(1, "out of memory!");
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
2024-02-25 23:59:26 -08:00
|
|
|
|
|
|
|
void *strdup_checked(const char *str) {
|
|
|
|
char *new_str = strdup(str);
|
|
|
|
if (!new_str) {
|
|
|
|
errx(1, "out of memory!");
|
|
|
|
}
|
|
|
|
return new_str;
|
|
|
|
}
|