]> git.leonardobizzoni.com Git - CBuild/commitdiff
File read/write/delete/rename & rmdir & examples
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Aug 2025 09:41:01 +0000 (11:41 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Aug 2025 09:41:01 +0000 (11:41 +0200)
cbuild.h
examples/01-basics/build.c [new file with mode: 0644]
examples/01-basics/main.c [new file with mode: 0644]
examples/02-async-commands/build.c [new file with mode: 0644]
examples/03-stream-redirection/build.c [new file with mode: 0644]
examples/XX-codebase-build/build.c [new file with mode: 0644]
examples/XX-codebase-build/src/base [new submodule]
examples/XX-codebase-build/src/main.c [new file with mode: 0644]

index bddc19aea63336e042a9e5cbc3d9b6f9303f9991..0126e7a8fd22fca7a49e9270ee9a79b98c66b5a7 100755 (executable)
--- a/cbuild.h
+++ b/cbuild.h
@@ -68,6 +68,7 @@ typedef enum {false, true} bool;
 #include <string.h>
 #include <errno.h>
 #include <stdint.h>
+#include <stdio.h>
 
 #if OS_WINDOWS
 #  include <windows.h>
@@ -348,9 +349,12 @@ static cb_proc_handle _cb_run(cb_cmd *cmd, struct cb_run_args args) {
 #else
   res = fork();
   if (!res) {
-    if (args.stdin)  { dup2(args.stdin, STDIN_FILENO); }
     if (args.stdout) { dup2(args.stdout, STDOUT_FILENO); }
     if (args.stderr) { dup2(args.stderr, STDERR_FILENO); }
+    if (args.stdin)  {
+      lseek(args.stdin, 0, SEEK_SET);
+      dup2(args.stdin, STDIN_FILENO);
+    }
 
     cb_cmd _cmd = {};
     cb_cmd_append_dyn(&_cmd, cmd->values, cmd->count);
@@ -391,30 +395,6 @@ static void cb_procs_wait(cb_procs *procs) {
   cb_dyn_free(procs);
 }
 
-static size_t _last_occurance_of(char *string, char ch) {
-  char *res = string;
-  for (char *curr = string; curr && *curr; ++curr) {
-    if (*curr == ch) { res = curr; }
-  }
-  return res - string;
-}
-
-static bool cb_mkdir(char *path) {
-  int32_t mkdir_res = _cb_platform_mkdir(path);
-  if (mkdir_res < 0 && errno == ENOENT) {
-    size_t parent_end = _last_occurance_of(path, '/');
-    if (!parent_end) { return false; }
-    char *parent = malloc(parent_end + 1);
-    memcpy(parent, path, parent_end);
-    parent[parent_end] = 0;
-    cb_mkdir(parent);
-    free(parent);
-    _cb_platform_mkdir(path);
-  }
-
-  return !mkdir_res;
-}
-
 static cb_fd cb_open(char *path, cb_acf permission) {
 #if OS_WINDOWS
   DWORD access_flags = 0;
@@ -440,6 +420,27 @@ static cb_fd cb_open(char *path, cb_acf permission) {
 #endif
 }
 
+static char* cb_read(cb_fd fd) {
+#if OS_WINDOWS
+#else
+  struct stat file_stat;
+  if (!fstat(fd, &file_stat)) {
+    char *res = malloc(file_stat.st_size);
+    if(pread(fd, res, file_stat.st_size, 0) >= 0) {
+      return res;
+    }
+  }
+  return 0;
+#endif
+}
+
+static void cb_write(cb_fd fd, char *buffer, size_t buffsize) {
+#if OS_WINDOWS
+#else
+  write(fd, buffer, buffsize);
+#endif
+}
+
 static void cb_close(cb_fd fd) {
 #if OS_WINDOWS
   if (fd != CB_FD_INVALID) { FlushFileBuffers(fd); }
@@ -450,4 +451,49 @@ static void cb_close(cb_fd fd) {
 #endif
 }
 
+static size_t _last_occurance_of(char *string, char ch) {
+  char *res = string;
+  for (char *curr = string; curr && *curr; ++curr) {
+    if (*curr == ch) { res = curr; }
+  }
+  return res - string;
+}
+
+static bool cb_mkdir(char *path) {
+  int32_t mkdir_res = _cb_platform_mkdir(path);
+  if (mkdir_res < 0 && errno == ENOENT) {
+    size_t parent_end = _last_occurance_of(path, '/');
+    if (!parent_end) { return false; }
+    char *parent = malloc(parent_end + 1);
+    memcpy(parent, path, parent_end);
+    parent[parent_end] = 0;
+    cb_mkdir(parent);
+    free(parent);
+    _cb_platform_mkdir(path);
+  }
+
+  return !mkdir_res;
+}
+
+static void cb_rmdir(char *path) {
+#if OS_WINDOWS
+#else
+  rmdir(path);
+#endif
+}
+
+static void cb_fs_delete(char *path) {
+#if OS_WINDOWS
+#else
+  unlink(path);
+#endif
+}
+
+static void cb_fs_rename(char *path, char *to) {
+#if OS_WINDOWS
+#else
+  rename(path, to);
+#endif
+}
+
 #endif
diff --git a/examples/01-basics/build.c b/examples/01-basics/build.c
new file mode 100644 (file)
index 0000000..179a18d
--- /dev/null
@@ -0,0 +1,13 @@
+#include "../../cbuild.h"
+
+int main(int argc, char **argv) {
+  cb_rebuild_self(argc, argv);
+
+  cb_cmd cmd = {};
+#if OS_WINDOWS
+  cb_cmd_append(&cmd, "cl.exe", "main.c", "/Femain.exe");
+#else
+  cb_cmd_append(&cmd, "cc", "main.c", "-o", "main");
+#endif
+  cb_run(&cmd);
+}
diff --git a/examples/01-basics/main.c b/examples/01-basics/main.c
new file mode 100644 (file)
index 0000000..f1814cc
--- /dev/null
@@ -0,0 +1,5 @@
+#include <stdio.h>
+
+int main(void) {
+  printf("Hello, World!\n");
+}
diff --git a/examples/02-async-commands/build.c b/examples/02-async-commands/build.c
new file mode 100644 (file)
index 0000000..f93fac3
--- /dev/null
@@ -0,0 +1,16 @@
+#include "../../cbuild.h"
+
+int main(int argc, char **argv) {
+  cb_rebuild_self(argc, argv);
+
+  cb_cmd cmd = {};
+  cb_procs procs = {};
+  cb_cmd_append(&cmd, "ls", "-lah", ".");
+  cb_procs_push(&procs, cb_run(&cmd, .async = true));
+  cb_cmd_append(&cmd, "ls", "-lah", "/");
+  cb_procs_push(&procs, cb_run(&cmd, .async = true));
+  cb_cmd_append(&cmd, "pwd");
+  cb_procs_push(&procs, cb_run(&cmd, .async = true));
+
+  cb_procs_wait(&procs);
+}
diff --git a/examples/03-stream-redirection/build.c b/examples/03-stream-redirection/build.c
new file mode 100644 (file)
index 0000000..6e024c9
--- /dev/null
@@ -0,0 +1,16 @@
+#include "../../cbuild.h"
+
+int main(int argc, char **argv) {
+  cb_rebuild_self(argc, argv);
+
+  cb_fd file = cb_open("ls-curdir-log", CB_ACF_READ | CB_ACF_WRITE);
+
+  cb_cmd cmd = {};
+  cb_cmd_append(&cmd, "ls", "-lah", ".");
+  cb_run(&cmd, .stdout = file);
+
+  cb_cmd_append(&cmd, "cat");
+  cb_run(&cmd, .stdin = file);
+
+  cb_close(file);
+}
diff --git a/examples/XX-codebase-build/build.c b/examples/XX-codebase-build/build.c
new file mode 100644 (file)
index 0000000..c1eb124
--- /dev/null
@@ -0,0 +1,126 @@
+#include "../../cbuild.h"
+
+#define Codebase_Path "-I./src/base"
+#define Codebase_Module_Gui "/DOS_GUI=1"
+#define Codebase_Module_Sound "/DOS_SOUND=1"
+
+#define Infile  "src/main.c"
+#define Outfile "main"
+
+#if OS_WINDOWS
+#  define SystemSharedLibs "/link", "Ws2_32.lib"
+#  define GenericFlags "/Zc:preprocessor", "/RTC1", "/GA", "/Gw", \
+                       "/permissive", "/fastfail", "/sdl"
+#  define ReleaseFlags "/O2", "/Ot", "/Ob3", "/GL", "/Qpar", \
+                       "/Qspectre-load", "/Qspectre-load-cf"
+#  define DebugFlags "/Od", "/Zi", "/fsanitize=address", \
+                     "/DDEBUG=1", "/DENABLE_ASSERT=1"
+#  define CppFlags "/TP", "/std:c++latest"
+#  define CAnnoyingWarnings "/wd4477", "/wd4996"
+#  define CppAnnoyingWarnings
+#  define Output "/Fe" Outfile ".exe"
+#else
+#  define SystemSharedLibs "-lpthread", "-lm"
+#  define GenericFlags "-pedantic", "-Wall", "-Werror", "-Wextra",         \
+                       "-Wconversion", "-Wdouble-promotion", "-Wshadow",   \
+                       "-Wundef", "-Wcast-qual", "-Wmissing-declarations", \
+                       "-Wredundant-decls"
+#  define ReleaseFlags "-O3", "-s", "-march=native", "-flto", \
+                       "-D_FORTIFY_SOURCE=2", "-Wno-unused-variable"
+#  define DebugFlags "-O0", "-g3", "-ggdb", "-DENABLE_ASSERT=1",       \
+                     "-DDEBUG=1", "-fsanitize=address,undefined,leak", \
+                     "-fsanitize-trap", "-fstack-protector-strong"
+#  define CppFlags "-std=c++23", "-fno-exceptions"
+#  define CAnnoyingWarnings "-Wno-unused-function",                   \
+                            "-Wno-initializer-overrides",             \
+                            "-Wno-c23-extensions",                    \
+                            "-Wno-gnu-zero-variadic-macro-arguments", \
+                            "-Wno-sign-conversion",                   \
+                            "-Wno-unused-parameter"
+#  define CppAnnoyingWarnings CAnnoyingWarnings            \
+                              "-Wno-gnu-anonymous-struct", \
+                              "-Wno-gnu-anonymous-struct", \
+                              "-Wno-nested-anon-types"
+#  define Output "-o", Outfile
+
+#  define OPENGL "-lGL", "-lGLU", "-DUSING_OPENGL=1"
+#  if OS_LINUX
+#    define X11 "-DLNX_X11=1", "-lX11", "-lXext"
+#    define Wayland "-DLNX_WAYLAND=1", "-lxkbcommon"
+#  elif OS_BSD
+#    define X11 "-DBSD_X11=1", "-lX11", "-lXext"
+#    define Wayland "-DBSD_WAYLAND=1", "-lxkbcommon"
+#  elif OS_MAC
+#    error I dont have a mac
+#  endif
+#endif
+
+int main(int argc, char **argv) {
+  cb_rebuild_self(argc, argv);
+
+  bool dbg   = true;
+  bool cpp   = false;
+  bool clang = true;
+  bool gui   = false;
+  bool sound = false;
+
+  for (int32_t i = 1; i < argc; ++i) {
+    if (!strcmp(argv[i], "release")) {
+      dbg = false;
+    } else if (!strcmp(argv[i], "cpp")) {
+      cpp = true;
+    } else if (!strcmp(argv[i], "gcc")) {
+      clang = false;
+    } else if (!strcmp(argv[i], "gui")) {
+      gui = true;
+    } else if (!strcmp(argv[i], "sound")) {
+      sound = true;
+    }
+  }
+
+  cb_cmd cmd = {};
+  cb_cmd_append(&cmd, "git", "submodule", "update", "--recursive");
+  cb_proc_handle codebase_updater = cb_run(&cmd, .async = true);
+
+#if OS_WINDOWS
+  cb_cmd_push(&cmd, "cl.exe");
+#else
+  if (clang) {
+    cb_cmd_push(&cmd, "clang");
+  } else {
+    cb_cmd_push(&cmd, "gcc");
+  }
+#endif
+
+  cb_cmd_append(&cmd, Output);
+  cb_cmd_append(&cmd, Infile, Codebase_Path);
+  cb_cmd_append(&cmd, GenericFlags, SystemSharedLibs);
+
+  if (cpp) {
+    cb_cmd_append(&cmd, CppFlags, CppAnnoyingWarnings);
+  } else {
+    cb_cmd_append(&cmd, CAnnoyingWarnings);
+  }
+
+  if (dbg) {
+    cb_cmd_append(&cmd, DebugFlags);
+  } else {
+    cb_cmd_append(&cmd, ReleaseFlags);
+  }
+
+  if (gui) {
+    cb_cmd_append(&cmd, Codebase_Module_Gui);
+    cb_cmd_append(&cmd, OPENGL);
+#if OS_LINUX || OS_BSD
+    cb_cmd_append(&cmd, !strcmp(cb_getenv("XDG_SESSION_TYPE"), "x11")
+                        ? X11 : Wayland);
+#endif
+  }
+
+  if (sound) {
+    cb_cmd_append(&cmd, Codebase_Module_Sound);
+  }
+
+  cb_proc_wait(codebase_updater);
+  cb_run(&cmd);
+}
diff --git a/examples/XX-codebase-build/src/base b/examples/XX-codebase-build/src/base
new file mode 160000 (submodule)
index 0000000..793e883
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 793e883523577377344cbd96b8222e2bc8bf3cf1
diff --git a/examples/XX-codebase-build/src/main.c b/examples/XX-codebase-build/src/main.c
new file mode 100644 (file)
index 0000000..9d37779
--- /dev/null
@@ -0,0 +1,9 @@
+#include <base/base_inc.h>
+#include <OS/os_inc.h>
+
+#include <base/base_inc.c>
+#include <OS/os_inc.c>
+
+fn void start(CmdLine *cli) {
+  Info("Hello, World!");
+}