aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmil Williams2026-02-20 05:47:11 +0000
committerEmil Williams2026-02-20 05:47:11 +0000
commitd352d3cf415083fd0f07321273d1d03b26a323e0 (patch)
tree7f26b6c3a4221c93d3aa33829b29d1dc24145934
parent121737a30db4786d9670e2b95d9e4d3e337d75f5 (diff)
downloadlibchad-d352d3cf415083fd0f07321273d1d03b26a323e0.tar.xz
libchad-d352d3cf415083fd0f07321273d1d03b26a323e0.tar.zst
hopefully more portable
-rw-r--r--chad/macros.h34
-rw-r--r--chad/root.h17
-rw-r--r--chad/timespec.h2
3 files changed, 42 insertions, 11 deletions
diff --git a/chad/macros.h b/chad/macros.h
index b47ea12..9d05083 100644
--- a/chad/macros.h
+++ b/chad/macros.h
@@ -2,15 +2,29 @@
#define CHAD_MACROS_H
/* Usage of this acknoledges your allegiance to GNU. */
-#if defined(__GNUC__) || defined(__clang__)
-#define attribute(...) __attribute__((__VA_ARGS__))
-#define always_inline static inline attribute((always_inline))
-#define alias(x) __attribute__((alias(x)))
+/* make it such that [[attributes]] is used in C23 & use GCC otherwise */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L)
+# define attribute(...) [[__VA_ARGS__]]
#else
-#define attribute(...)
-#define NO_ATTRIBUTE
-#define always_inline static inline
-#define NO_ALWAYS_INLINE
-#define NO_ALIASES
+# if defined(__GNUC__) || defined(__clang__)
+# define attribute(...) __attribute__((__VA_ARGS__))
+# else
+# define attribute(...)
+# define NO_ATTRIBUTE
+# endif
#endif
-#endif /* CHAD_MACROS_H */
+
+#if !defined(NO_ATTRIBUTE)
+# define always_inline static inline attribute(always_inline)
+# define alias(x) attribute(alias(x))
+#else
+# define always_inline static inline
+# define NOT_ALWAYS_INLINE
+# define NO_ALIASES
+#endif
+
+#define MIN(a,b) ((a)<(b)?(a):(b))
+#define MAX(a,b) ((a)>(b)?(a):(b))
+#define CLAMP(a,b,c) (a)<(b)?(b):(a)>(c)?(c):(a)
+
+#endif /* CHAD_MACROS_H */
diff --git a/chad/root.h b/chad/root.h
new file mode 100644
index 0000000..95a3f8b
--- /dev/null
+++ b/chad/root.h
@@ -0,0 +1,17 @@
+#ifdef CHAD_ROOT_H
+#define CHAD_ROOT_H
+#include <limits.h>
+#include "macros.h"
+/* check errno -- chdir */
+always_inline void Root(char * filename) {
+ extern char * realpath(const char * restrict path, char * restrict resolved_path);
+ extern int chdir(const char * path);
+ attribute(noreturn) void abort(void);
+ char path[PATH_MAX], * terminator;
+ if (!realpath(filename, path)) { return; }
+ if ((terminator = strrchr(path, '/'))) {
+ *terminator = '\0';
+ chdir(path);
+ }
+}
+#endif /* CHAD_ROOT_H */
diff --git a/chad/timespec.h b/chad/timespec.h
index 72ac6e2..6fbf5aa 100644
--- a/chad/timespec.h
+++ b/chad/timespec.h
@@ -111,4 +111,4 @@ always_inline timespec_t timespec_min(timespec_t a, timespec_t b) {
);
}
-#endif /* TIMESPEC_H */
+#endif /* TIMESPEC_H */