#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);
}
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) {
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");