]> Trent Huber's Code - thus.git/commitdiff
History bug fix, remove unnecessary builtins.c build flag main
authorTrent Huber <trentmhuber@gmail.com>
Wed, 24 Jun 2026 03:29:39 +0000 (23:29 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Wed, 24 Jun 2026 03:29:39 +0000 (23:29 -0400)
src/builtins/build.c
src/history.c

index 4d2e118e5e74c20cca2541ca89b7a35b9a2dd1b7..0d00076bc8f0bbb43c93974e3b3584ae53382083 100644 (file)
@@ -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 <stddef.h>\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);
 
index fe966c08d79666293fb0b395cfd58c4bdfdd31eb..6298ec7dc41ebea7e518defe2350e14f2197f75d 100644 (file)
@@ -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");