commit ccd28fc9bf225fe3b19a62e4531d5a5c45b55706
parent 868bbcf88bfaf34de0bda2621b57d31e02cf3773
Author: vx-clutch <[email protected]>
Date: Thu, 17 Jul 2025 16:31:35 -0400
fix: segfault
Diffstat:
4 files changed, 54 insertions(+), 64 deletions(-)
diff --git a/core/file.c b/core/file.c
@@ -7,69 +7,60 @@
#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 };
+error_t touch(char *path, char *format, ...) {
+ error_t err = {0};
err.null = true;
- FILE *fp = fopen (path, "w");
- if (!fp)
- {
- err.null = false;
- err.status = errno;
- err.src = strerror (errno);
- return err;
- }
+ FILE *fp = fopen(path, "w");
+ if (!fp) {
+ err.null = false;
+ err.status = errno;
+ err.src = strerror(errno);
+ return err;
+ }
- if (contains_percent (format))
- {
- va_list args;
- va_start (args, format);
- vfprintf (fp, format, args);
- va_end (args);
- }
- else
- {
- fputs (format, fp);
- }
+ else {
+ va_list args;
+ va_start(args, format);
+ vfprintf(fp, format, args);
+ va_end(args);
+ }
- fclose (fp);
+ fclose(fp);
return err;
}
-error_t
-dir (char *format, ...)
-{
- error_t err = { 0 };
+error_t dir(char *format, ...) {
+ error_t err = {0};
err.null = true; // success by default
va_list args;
- va_start (args, format);
+ va_start(args, format);
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) {
+ err.null = false;
+ err.status = errno;
+ err.src = strerror(errno);
+ }
- if (mkdir (path, 0777) < 0)
- {
- err.null = false;
- err.status = errno;
- err.src = strerror (errno);
- }
+ 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;
}
diff --git a/core/file.h b/core/file.h
@@ -4,9 +4,7 @@
#include "e.h"
#include <unistd.h>
-#define take(x, ...) \
- dir(x, ##__VA_ARGS__); \
- chdir(x);
+error_t take(const char *dirname);
error_t touch(char *, char *, ...);
error_t dir(char *, ...);
diff --git a/yait/format.h b/yait/format.h
@@ -20,7 +20,6 @@ typedef struct {
bool nogit;
licence_t licence;
char *name;
- lib_t libraries[];
} format_t;
#endif
diff --git a/yait/main.c b/yait/main.c
@@ -1,7 +1,6 @@
#include "../core/file.h"
#include "../core/print.h"
#include "format.h"
-#include <stdio.h>
int create (format_t);
@@ -23,7 +22,11 @@ main (int argc, char **argv)
int
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",
"%s ( concise description )\n\n"
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
@@ -56,7 +59,7 @@ create (format_t fmt)
"exit 0\n"
"}\n"
"\n"
- "echo () { printf \"%s\\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"
@@ -77,7 +80,7 @@ create (format_t fmt)
"trycc gcc\n"
"trycc cc\n"
"trycc clang\n"
- "printf \"%s\\n\" \"$CC\"\n"
+ "printf \"%%s\\n\" \"$CC\"\n"
"\n"
"printf \"checking weather C compiler works... \"\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"
"printf \"yes\\n\"\n"
"else\n"
- "printf \"no; %s\\n\" \"$output\"\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);
+ "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");
return 0;
}