From 868bbcf88bfaf34de0bda2621b57d31e02cf3773 Mon Sep 17 00:00:00 2001 From: vx-clutch Date: Thu, 17 Jul 2025 13:58:33 -0400 Subject: [PATCH] bug: second touch in main.c segfault --- core/file.c | 33 ++++++++++++++++++++++------- yait/main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/core/file.c b/core/file.c index 5649b77..dd21003 100644 --- a/core/file.c +++ b/core/file.c @@ -7,14 +7,23 @@ #include #include +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 index d315330..4c7ba0e 100644 --- a/yait/main.c +++ b/yait/main.c @@ -1,6 +1,7 @@ #include "../core/file.h" #include "../core/print.h" #include "format.h" +#include 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 </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; }