From: Trent Huber Date: Wed, 24 Jun 2026 03:29:39 +0000 (-0400) Subject: History bug fix, remove unnecessary builtins.c build flag X-Git-Url: http://trenthuber.com/code?a=commitdiff_plain;ds=sidebyside;p=thus.git History bug fix, remove unnecessary builtins.c build flag --- diff --git a/src/builtins/build.c b/src/builtins/build.c index 4d2e118..0d00076 100644 --- a/src/builtins/build.c +++ b/src/builtins/build.c @@ -5,51 +5,52 @@ #define MAXBUILTINS 50 int main(void) { - int fd, d; + int fd; DIR *dir; char *srcs[MAXBUILTINS + 2 + 1], **src, *decl; + size_t l; struct dirent *entry; build("./"); if ((fd = open("builtins.c", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) err(errno, "Unable to open/create `builtins.c'"); - if (!(dir = opendir("./"))) err(errno, "Unable to open current directory"); dprintf(fd, "#include \n\n#include \"builtins.h\"\n\n"); src = srcs; - errno = 0; + if (!(dir = opendir("./"))) err(errno, "Unable to open current directory"); while ((entry = readdir(dir))) { - if (strcmp(entry->d_name, "build.c") == 0 - || !(*src = strrchr(entry->d_name, '.')) || strcmp(*src, ".c") != 0) + if ((l = strlen(*src = entry->d_name)) < 2 || strcmp(*src + l - 2, ".c") != 0 + || strcmp(*src, "build.c") == 0 || strcmp(*src, "builtins.c") == 0) continue; - if (!(*src = strdup(entry->d_name))) - err(errno, "Unable to duplicate directory entry"); - (*src)[strlen(*src) - 2] = '\0'; if (src - srcs == 2 + MAXBUILTINS + 1) errx(EXIT_FAILURE, "Unable to add `%s' built-in, maximum reached (%d)", *src, MAXBUILTINS); - if (strcmp(*src, "builtins") != 0) - dprintf(fd, "int %s(char **args, size_t numargs);\n", *src); - ++src; + if (!(*src = strdup(entry->d_name))) + err(errno, "Unable to duplicate directory entry"); + (*src)[strlen(*src) - 2] = '\0'; + dprintf(fd, "int %s(char **args, size_t numargs);\n", *src++); } if (errno) err(errno, "Unable to read from current directory"); + if (closedir(dir) == -1) err(errno, "Unable to close current directory"); *src = NULL; - d = (int)strlen(decl = "struct builtin *builtins = (struct builtin []){"); + l = strlen(decl = "struct builtin *builtins = (struct builtin []){"); dprintf(fd, "\n%s", decl); for (src = srcs; *src; ++src) - if (strcmp(*src, "builtins") != 0) - dprintf(fd, "{\"%s\", %s},\n%*s", *src, *src, d, ""); + dprintf(fd, "{\"%s\", %s},\n%*s", *src, *src, (int)l, ""); dprintf(fd, "{NULL}};\n"); - if (closedir(dir) == -1) err(errno, "Unable to close current directory"); if (close(fd) == -1) err(errno, "Unable to close `builtins.c'"); cflags = LIST("-I../"); for (src = srcs; *src; ++src) compile(*src); + cflags = NONE; + compile(*src++ = "builtins"); + *src = NULL; load('s', "../builtins", srcs); + *--src = NULL; for (src = srcs; *src; ++src) free(*src); diff --git a/src/history.c b/src/history.c index fe966c0..6298ec7 100644 --- a/src/history.c +++ b/src/history.c @@ -16,12 +16,12 @@ static char *inc(char **x) { } static void readhistory(FILE *file) { - b = t = history[0]; + t = b = history[0]; while (fgets(t, MAXCHARS + 1, file)) { t[strlen(t) - 1] = '\0'; if (inc(&t) == b) inc(&b); } - s = c = t; + c = s = t; } void inithistory(void) { @@ -30,8 +30,9 @@ void inithistory(void) { strcpy(path, home); strcat(path, ".thushistory"); if (!(file = fopen(path, "r"))) { - if (errno == ENOENT) return; - fatal("Unable to open history file for reading"); + if (errno != ENOENT) fatal("Unable to open history file for reading"); + t = c = s = b = history[0]; + return; } readhistory(file); if (ferror(file) || !feof(file)) fatal("Unable to read from history file");