bug: second touch in main.c segfault

This commit is contained in:
2025-07-17 13:58:33 -04:00
parent 7a1a6b9965
commit 868bbcf88b
2 changed files with 85 additions and 9 deletions

View File

@@ -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;
}
if (contains_percent (format))
{
va_list args;
va_start (args, format);
vfprintf (fp, format, args);
fclose (fp);
va_end (args);
}
else
{
fputs (format, fp);
}
fclose (fp);
return err;
}

View File

@@ -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;
}