Restructure project, add gebs_exists() and gebs_mkdir()

This commit is contained in:
kamkow1
2025-05-19 12:25:08 +02:00
parent 0087c523b5
commit c905379d14
6 changed files with 45 additions and 14 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build
gebs

3
example/.gitignore vendored
View File

@ -1,3 +0,0 @@
gebs
a.out
main

View File

@ -1,7 +0,0 @@
#include <stdio.h>
int main(void)
{
printf("Hello world\n");
return 0;
}

11
example/self_rebuild.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#define GEBS_IMPLEMENTATION
#include "../gebs.h"
int main(int argc, char ** argv)
{
gebs_rebuild_self(argc, argv, "cc", "-o", "self_rebuild", __FILE__);
printf("Hello world\n");
return 0;
}

View File

@ -1,13 +1,16 @@
#define GEBS_IMPLEMENTATION
#include "../gebs.h"
#include "gebs.h"
int main(int argc, char ** argv)
{
gebs_rebuild_self(argc, argv, "cc", "-o", "gebs", __FILE__);
if (GEBS_CMD("gcc", "-o", "main", "main.c") != 0) {
return 1;
if (!gebs_exists("build")) {
gebs_mkdir("build");
}
if (GEBS_CMD("gcc", "-o", "build/self_rebuild", "example/self_rebuild.c") != 0)
return 1;
return 0;
}

25
gebs.h
View File

@ -164,6 +164,8 @@ typedef struct {
bool gebs_rename(const char *a, const char *b);
bool gebs_remove(const char *p);
bool gebs_mkdir(const char *p);
bool gebs_exists(const char *p);
// ----------------------------------------------------------------------------
// Commands
@ -216,6 +218,10 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
int gebs_cmd_run_sync_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd)
{
Gebs_String_Builder sb = {0};
gebs_nsl_join_alloc(alloc, cmd, &sb, " ");
GEBS_LOGI("cmd `%s`\n", sb.items);
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
// Clone the list
Gebs_Cmd new_cmd = {0};
@ -287,6 +293,25 @@ void *gebs_default_allocator_realloc(void *memory, size_t prev_size, size_t new_
// Filesystem operations
// ----------------------------------------------------------------------------
bool gebs_mkdir(const char *p)
{
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
return mkdir(p, 0777) == 0;
#else
# error "gebs_mkdir unknown platform"
#endif
}
bool gebs_exists(const char *p)
{
#if GEBS_PLATFORM == GEBS_PLATFORM_POSIX
struct stat st;
return stat(p, &st) == 0;
#else
# error "gebs_exists unknown platform"
#endif
}
bool gebs_rename(const char *a, const char *b)
{
GEBS_LOGO("rename %s -> %s\n", a, b);