From 878df8b856bec7d79c89227a9e516707f5943ab8 Mon Sep 17 00:00:00 2001 From: Trent Huber Date: Fri, 22 Aug 2025 23:36:46 -0400 Subject: [PATCH] Install and uninstall utils added --- .gitignore | 2 ++ build.c | 1 + src/builtin/bg.c | 1 + src/builtin/builtin.c | 6 +++++- src/builtin/pwd.c | 20 ++++++++++++++++++++ src/builtin/source.c | 16 +++++++++++++++- src/history.c | 10 ++++++++-- src/utils.c | 10 +++++----- tools/build.c | 11 +++++++++++ tools/install.c | 44 +++++++++++++++++++++++++++++++++++++++++++ tools/uninstall.c | 14 ++++++++++++++ 11 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 src/builtin/pwd.c create mode 100644 tools/build.c create mode 100644 tools/install.c create mode 100644 tools/uninstall.c diff --git a/.gitignore b/.gitignore index c685519..dec10b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ +install +uninstall *build *.o diff --git a/build.c b/build.c index 41025fd..224592e 100644 --- a/build.c +++ b/build.c @@ -4,6 +4,7 @@ int main(void) { build("./"); build("src/"); + build("tools/"); return EXIT_SUCCESS; } diff --git a/src/builtin/bg.c b/src/builtin/bg.c index 15457b0..b8503a3 100644 --- a/src/builtin/bg.c +++ b/src/builtin/bg.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "bg.h" diff --git a/src/builtin/builtin.c b/src/builtin/builtin.c index d9ef4d1..52ea765 100644 --- a/src/builtin/builtin.c +++ b/src/builtin/builtin.c @@ -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 index 0000000..c9e6e2d --- /dev/null +++ b/src/builtin/pwd.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#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; +} diff --git a/src/builtin/source.c b/src/builtin/source.c index 91f0570..51001b0 100644 --- a/src/builtin/source.c +++ b/src/builtin/source.c @@ -1,6 +1,8 @@ +#include #include #include #include +#include #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}); } diff --git a/src/history.c b/src/history.c index 511ce7a..b7a1b36 100644 --- a/src/history.c +++ b/src/history.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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"); diff --git a/src/utils.c b/src/utils.c index d02330e..0e0c25e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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 index 0000000..d2a16ee --- /dev/null +++ b/tools/build.c @@ -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 index 0000000..85c7394 --- /dev/null +++ b/tools/install.c @@ -0,0 +1,44 @@ +#include + +#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 index 0000000..da5876b --- /dev/null +++ b/tools/uninstall.c @@ -0,0 +1,14 @@ +#include + +#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; +} -- 2.51.0