]> Trent Huber's Code - thus.git/commitdiff
Install and uninstall utils added
authorTrent Huber <trentmhuber@gmail.com>
Sat, 23 Aug 2025 03:36:46 +0000 (23:36 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Sat, 23 Aug 2025 03:36:46 +0000 (23:36 -0400)
.gitignore
build.c
src/builtin/bg.c
src/builtin/builtin.c
src/builtin/pwd.c [new file with mode: 0644]
src/builtin/source.c
src/history.c
src/utils.c
tools/build.c [new file with mode: 0644]
tools/install.c [new file with mode: 0644]
tools/uninstall.c [new file with mode: 0644]

index c685519a375be24df8ed1031599d1673bdc530ee..dec10b7b953dc6968e2f830a894cbea19d30d434 100644 (file)
@@ -1,2 +1,4 @@
+install
+uninstall
 *build
 *.o
diff --git a/build.c b/build.c
index 41025fd540f8651b3ae6df599e57550a8b91c03d..224592ee4a8de76ea809625115d10bebec473195 100644 (file)
--- a/build.c
+++ b/build.c
@@ -4,6 +4,7 @@ int main(void) {
    build("./");
 
    build("src/");
+   build("tools/");
 
    return EXIT_SUCCESS;
 }
index 15457b031f7d1aacf2b76939157223c3695465e0..b8503a3a7df4861ab841471d05095c38d08c7141 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/errno.h>
+#include <sys/wait.h>
 #include <termios.h>
 
 #include "bg.h"
index d9ef4d130e75e32e04a95273c8db6f24fff47190..52ea765319b6e28a75b63193a7c7cbd29b35a496 100644 (file)
@@ -16,10 +16,14 @@ int isbuiltin(char **args) {
            status = builtinp->func(n, args);
            return 1;
        }
+
    return 0;
 }
 
 int usage(char *program, char *options) {
-   fprintf(stderr, "Usage: %s %s\n", program, options);
+   fprintf(stderr, "Usage: %s", program);
+   if (options) fprintf(stderr, " %s", options);
+   fputc('\n', stderr);
+
    return EXIT_FAILURE;
 }
diff --git a/src/builtin/pwd.c b/src/builtin/pwd.c
new file mode 100644 (file)
index 0000000..c9e6e2d
--- /dev/null
@@ -0,0 +1,20 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "builtin.h"
+#include "utils.h"
+
+BUILTIN(pwd) {
+   char *cwd, buffer[PATH_MAX];
+
+   if (argc != 1) return usage(argv[0], NULL);
+
+   if (!(cwd = getenv("PWD")) && !(cwd = getcwd(buffer, PATH_MAX)))
+       fatal("Unable to get current working directory");
+
+   puts(cwd);
+
+   return EXIT_SUCCESS;
+}
index 91f0570a75b7e10e48d030cdf7bd716cbb9a9dd4..51001b01362ab46d9ee79cb4faf6fb98ddef6856 100644 (file)
@@ -1,6 +1,8 @@
+#include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "builtin.h"
 #include "context.h"
@@ -35,6 +37,18 @@ BUILTIN(source) {
 
 void config(char *name) {
    char path[PATH_MAX];
+   int fd;
 
-   source(2, (char *[]){"source", catpath(home, name, path), NULL});
+   if (!catpath(home, name, path)) return;
+
+   if ((fd = open(path, O_RDONLY | O_CREAT, 0644)) == -1) {
+       note("Unable to create `%s'", path);
+       return;
+   }
+   if (close(fd) == -1) {
+       note("Unable to close `%s'", path);
+       return;
+   }
+
+   source(2, (char *[]){"source", path, NULL});
 }
index 511ce7aaf0d95356ef56e233e80367af6e388c51..b7a1b36d4bef2fba89cc413bf4a0979b6ebabe30 100644 (file)
@@ -1,3 +1,4 @@
+#include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -21,7 +22,7 @@ static struct {
 void inithistory(void) {
    FILE *file;
 
-   if (!catpath(home, ".ashhistory", history.path)) exit(EXIT_FAILURE);
+   if (!catpath(home, ".thushistory", history.path)) exit(EXIT_FAILURE);
    if (!(file = fopen(history.path, "r"))) {
        if (errno == ENOENT) return;
        fatal("Unable to open history file for reading");
@@ -54,12 +55,17 @@ void sethistory(char *buffer) {
 }
 
 void deinithistory(void) {
+   int fd;
    FILE *file;
 
-   if (!(file = fopen(history.path, "w"))) {
+   if ((fd = open(history.path, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1) {
        note("Unable to open history file for writing");
        return;
    }
+   if (!(file = fdopen(fd, "w"))) {
+       note("Unable to open history file descriptor as FILE pointer");
+       return;
+   }
    for (history.c = history.b; history.c != history.t; INC(c)) {
        if (fputs(history.entries[history.c], file) == EOF) {
            note("Unable to write to history file");
index d02330e5aa34dd94d9f36e7b2ad5af4789a77aed..0e0c25eb2f647dade9161246542199003f4a99d3 100644 (file)
@@ -28,9 +28,7 @@ void note(char *fmt, ...) {
 void fatal(char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
-   (errno ? vwarn : vwarnx)(fmt, args);
-   va_end(args);
-   exit(EXIT_FAILURE);
+   (errno ? verr : verrx)(EXIT_FAILURE, fmt, args);
 }
 
 void init(void) {
@@ -65,10 +63,12 @@ void init(void) {
 }
 
 char *catpath(char *dir, char *filename, char *buffer) {
+   size_t l;
    int slash;
 
-   slash = dir[strlen(dir) - 1] == '/';
-   if (strlen(dir) + slash + strlen(filename) + 1 > PATH_MAX) {
+   l = strlen(dir);
+   slash = dir[l - 1] == '/';
+   if (l + slash + strlen(filename) + 1 > PATH_MAX) {
        note("Path name `%s%s%s' too long", dir, slash ? "/" : "", filename);
        return NULL;
    }
diff --git a/tools/build.c b/tools/build.c
new file mode 100644 (file)
index 0000000..d2a16ee
--- /dev/null
@@ -0,0 +1,11 @@
+#include "../external/cbs/cbs.c"
+
+int main(void) {
+   build("./");
+
+   cflags = LIST("-I../external/cbs/");
+   compile("install");
+   load('x', "../install", LIST("install"));
+
+   return EXIT_SUCCESS;
+}
diff --git a/tools/install.c b/tools/install.c
new file mode 100644 (file)
index 0000000..85c7394
--- /dev/null
@@ -0,0 +1,44 @@
+#include <limits.h>
+
+#include "cbs.c"
+
+int main(int argc, char **argv) {
+   char define[7 + 1 + PATH_MAX + 1], *path;
+   size_t l;
+   int slash;
+   pid_t cpid;
+
+   if (argc > 2) errx(EXIT_FAILURE, "%s [prefix]", argv[0]);
+
+   path = stpcpy(define, "-DPATH=\"");
+   if (argc == 2) {
+       l = strlen(argv[1]);
+       slash = argv[1][l - 1] == '/';
+       if (l + slash + strlen("bin/thus") + 1 > PATH_MAX)
+           errx(EXIT_FAILURE, "Path name `%s%sbin/thus' too long",
+                argv[1], slash ? "/" : "");
+       strcpy(path, argv[1]);
+       if (!slash) strcat(path, "/");
+   } else strcpy(path, "/usr/local/");
+
+   strcat(path, "bin/");
+   if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
+   else if (cpid == 0)
+       run("/bin/mkdir", LIST("mkdir", "-p", path), "create", path);
+   await(cpid, "create", path);
+
+   strcat(path, "thus");
+   if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
+   else if (cpid == 0)
+       run("/bin/cp", LIST("cp", "bin/thus", path), "copy", "bin/thus");
+   await(cpid, "copy", "bin/thus");
+
+   l = strlen(define);
+   define[l] = '\"';
+   define[l + 1] = '\0';
+   cflags = LIST(define, "-Iexternal/cbs");
+   compile("tools/uninstall");
+   load('x', "uninstall", LIST("tools/uninstall"));
+
+   return EXIT_SUCCESS;
+}
diff --git a/tools/uninstall.c b/tools/uninstall.c
new file mode 100644 (file)
index 0000000..da5876b
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+
+#include "cbs.c"
+
+int main(void) {
+   pid_t cpid;
+
+   if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
+   else if (cpid == 0)
+       run("/bin/rm", LIST("rm", PATH, "uninstall"), "remove", PATH);
+   await(cpid, "remove", PATH);
+
+   return EXIT_SUCCESS;
+}