commit 868bbcf88bfaf34de0bda2621b57d31e02cf3773
parent 7a1a6b99652d535f89cd696cd2f1514d9471fdfb
Author: vx-clutch <[email protected]>
Date: Thu, 17 Jul 2025 13:58:33 -0400
bug: second touch in main.c segfault
Diffstat:
| M | core/file.c | | | 33 | +++++++++++++++++++++++++-------- |
| M | yait/main.c | | | 61 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
2 files changed, 85 insertions(+), 9 deletions(-)
diff --git a/core/file.c b/core/file.c
@@ -7,14 +7,23 @@
#include <sys/stat.h>
#include <sys/types.h>
+bool
+contains_percent (const char *s)
+{
+ while (*s)
+ {
+ if (*s == '%')
+ return true;
+ s++;
+ }
+ return false;
+}
+
error_t
touch (char *path, char *format, ...)
{
error_t err = { 0 };
- err.null = true; // success by default
-
- va_list args;
- va_start (args, format);
+ err.null = true;
FILE *fp = fopen (path, "w");
if (!fp)
@@ -22,14 +31,22 @@ touch (char *path, char *format, ...)
err.null = false;
err.status = errno;
err.src = strerror (errno);
- va_end (args);
return err;
}
- vfprintf (fp, format, args);
- fclose (fp);
+ if (contains_percent (format))
+ {
+ va_list args;
+ va_start (args, format);
+ vfprintf (fp, format, args);
+ va_end (args);
+ }
+ else
+ {
+ fputs (format, fp);
+ }
- va_end (args);
+ fclose (fp);
return err;
}
diff --git a/yait/main.c b/yait/main.c
@@ -1,6 +1,7 @@
#include "../core/file.h"
#include "../core/print.h"
#include "format.h"
+#include <stdio.h>
int create (format_t);
@@ -37,6 +38,64 @@ create (format_t fmt)
"proident, sunt in\n"
"culpa qui officia deserunt mollit anim id est laborum.",
fmt.name);
- touch ("hello", "world");
+ touch ("configure",
+ "#!/bin/sh\n"
+ "\n"
+ "usage() {\n"
+ "cat <<EOF\n"
+ "Usage: $0 [OPTION]... [VAR=VALUE]...\n"
+ "\n"
+ "To assign environment variables (e.g., CC, CFLAGS...), specify them "
+ "as\n"
+ "VAR=VALUE.\n"
+ "\n"
+ " CC C compiler command [detected]\n"
+ " CFLAGS C compiler flags [-g, ...]\n"
+ "\n"
+ "EOF\n"
+ "exit 0\n"
+ "}\n"
+ "\n"
+ "echo () { printf \"%s\\n\" \"$*\" ; }\n"
+ "cmdexists () { type \"$1\" >/dev/null 2>&1 ; }\n"
+ "trycc () { test -z \"$CC\" && cmdexists \"$1\" && CC=$1 ; }\n"
+ "\n"
+ "prefix=/usr/bin/\n"
+ "CFLAGS=\"-Wall -Wextra -O2\"\n"
+ "LDFLAGS=\n"
+ "CC=\n"
+ "\n"
+ "for arg ; do\n"
+ "case \"$arg\" in\n"
+ "--help|h) usage ;;\n"
+ "CFLAGS=*) CFLAGS=${arg#*=} ;;\n"
+ "LDFLAGS=*) LDFLAGS=${arg#*=} ;;\n"
+ "esac\n"
+ "done\n"
+ "\n"
+ "printf \"checking for C compiler... \"\n"
+ "trycc gcc\n"
+ "trycc cc\n"
+ "trycc clang\n"
+ "printf \"%s\\n\" \"$CC\"\n"
+ "\n"
+ "printf \"checking weather C compiler works... \"\n"
+ "status=\"fail\"\n"
+ "tmpc=\"$(mktemp -d)/test.c\"\n"
+ "echo \"typedef int x;\" > \"$tmpc\"\n"
+ "if output=$($CC $CFLAGS -c -o /dev/null \"$tmpc\" 2>&1) ; then\n"
+ "printf \"yes\\n\"\n"
+ "else\n"
+ "printf \"no; %s\\n\" \"$output\"\n"
+ "exit 1\n"
+ "fi\n"
+ "\n"
+ "printf \"creating config.mak... \"\n"
+ "printf \"PREFIX=%s\\n\" \"$prefix\" > config.mak\n"
+ "printf \"CFLAGS=%s\\n\" \"$CFLAGS\" >> config.mak\n"
+ "printf \"LDFLAGS=%s\\n\" \"$LDFLAGS\" >> config.mak\n"
+ "printf \"CC=%s\\n\" \"$CC\" >> config.mak\n"
+ "printf \"done\\n\"\n",
+ NULL);
return 0;
}