fix: segfault

This commit is contained in:
2025-07-17 16:31:35 -04:00
parent 868bbcf88b
commit ccd28fc9bf
4 changed files with 54 additions and 64 deletions

View File

@@ -7,69 +7,60 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
bool error_t touch(char *path, char *format, ...) {
contains_percent (const char *s) error_t err = {0};
{
while (*s)
{
if (*s == '%')
return true;
s++;
}
return false;
}
error_t
touch (char *path, char *format, ...)
{
error_t err = { 0 };
err.null = true; err.null = true;
FILE *fp = fopen (path, "w"); FILE *fp = fopen(path, "w");
if (!fp) if (!fp) {
{
err.null = false; err.null = false;
err.status = errno; err.status = errno;
err.src = strerror (errno); err.src = strerror(errno);
return err; return err;
} }
if (contains_percent (format)) else {
{
va_list args; va_list args;
va_start (args, format); va_start(args, format);
vfprintf (fp, format, args); vfprintf(fp, format, args);
va_end (args); va_end(args);
}
else
{
fputs (format, fp);
} }
fclose (fp); fclose(fp);
return err; return err;
} }
error_t error_t dir(char *format, ...) {
dir (char *format, ...) error_t err = {0};
{
error_t err = { 0 };
err.null = true; // success by default err.null = true; // success by default
va_list args; va_list args;
va_start (args, format); va_start(args, format);
char path[1024]; char path[1024];
vsnprintf (path, sizeof (path), format, args); vsnprintf(path, sizeof(path), format, args);
va_end (args); va_end(args);
if (mkdir (path, 0777) < 0) if (mkdir(path, 0777) < 0) {
{
err.null = false; err.null = false;
err.status = errno; err.status = errno;
err.src = strerror (errno); err.src = strerror(errno);
} }
return err; return err;
} }
error_t take(const char *dirname) {
error_t err = dir("%s", dirname);
if (!err.null) {
return err;
}
if (chdir(dirname) != 0) {
err.null = false;
err.status = errno;
err.src = strerror(errno);
return err;
}
return err;
}

View File

@@ -4,9 +4,7 @@
#include "e.h" #include "e.h"
#include <unistd.h> #include <unistd.h>
#define take(x, ...) \ error_t take(const char *dirname);
dir(x, ##__VA_ARGS__); \
chdir(x);
error_t touch(char *, char *, ...); error_t touch(char *, char *, ...);
error_t dir(char *, ...); error_t dir(char *, ...);

View File

@@ -20,7 +20,6 @@ typedef struct {
bool nogit; bool nogit;
licence_t licence; licence_t licence;
char *name; char *name;
lib_t libraries[];
} format_t; } format_t;
#endif #endif

View File

@@ -1,7 +1,6 @@
#include "../core/file.h" #include "../core/file.h"
#include "../core/print.h" #include "../core/print.h"
#include "format.h" #include "format.h"
#include <stdio.h>
int create (format_t); int create (format_t);
@@ -23,7 +22,11 @@ main (int argc, char **argv)
int int
create (format_t fmt) create (format_t fmt)
{ {
take (fmt.name); error_t err = take(fmt.name);
if (!err.null) {
printfn("failed to create or enter directory: %s", err.src);
return 1;
}
touch ("README", touch ("README",
"%s ( concise description )\n\n" "%s ( concise description )\n\n"
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
@@ -56,7 +59,7 @@ create (format_t fmt)
"exit 0\n" "exit 0\n"
"}\n" "}\n"
"\n" "\n"
"echo () { printf \"%s\\n\" \"$*\" ; }\n" "echo () { printf \"%%s\\n\" \"$*\" ; }\n"
"cmdexists () { type \"$1\" >/dev/null 2>&1 ; }\n" "cmdexists () { type \"$1\" >/dev/null 2>&1 ; }\n"
"trycc () { test -z \"$CC\" && cmdexists \"$1\" && CC=$1 ; }\n" "trycc () { test -z \"$CC\" && cmdexists \"$1\" && CC=$1 ; }\n"
"\n" "\n"
@@ -77,7 +80,7 @@ create (format_t fmt)
"trycc gcc\n" "trycc gcc\n"
"trycc cc\n" "trycc cc\n"
"trycc clang\n" "trycc clang\n"
"printf \"%s\\n\" \"$CC\"\n" "printf \"%%s\\n\" \"$CC\"\n"
"\n" "\n"
"printf \"checking weather C compiler works... \"\n" "printf \"checking weather C compiler works... \"\n"
"status=\"fail\"\n" "status=\"fail\"\n"
@@ -86,16 +89,15 @@ create (format_t fmt)
"if output=$($CC $CFLAGS -c -o /dev/null \"$tmpc\" 2>&1) ; then\n" "if output=$($CC $CFLAGS -c -o /dev/null \"$tmpc\" 2>&1) ; then\n"
"printf \"yes\\n\"\n" "printf \"yes\\n\"\n"
"else\n" "else\n"
"printf \"no; %s\\n\" \"$output\"\n" "printf \"no; %%s\\n\" \"$output\"\n"
"exit 1\n" "exit 1\n"
"fi\n" "fi\n"
"\n" "\n"
"printf \"creating config.mak... \"\n" "printf \"creating config.mak... \"\n"
"printf \"PREFIX=%s\\n\" \"$prefix\" > config.mak\n" "printf \"PREFIX=%%s\\n\" \"$prefix\" > config.mak\n"
"printf \"CFLAGS=%s\\n\" \"$CFLAGS\" >> config.mak\n" "printf \"CFLAGS=%%s\\n\" \"$CFLAGS\" >> config.mak\n"
"printf \"LDFLAGS=%s\\n\" \"$LDFLAGS\" >> config.mak\n" "printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
"printf \"CC=%s\\n\" \"$CC\" >> config.mak\n" "printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
"printf \"done\\n\"\n", "printf \"done\\n\"\n");
NULL);
return 0; return 0;
} }