From 46f0d4955f2037a58fddcd5727b218803a10c5c9 Mon Sep 17 00:00:00 2001 From: DTB Date: Tue, 23 Jul 2024 17:39:55 -0600 Subject: [PATCH] pschdir(1): add source file --- src/pschdir.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/pschdir.c diff --git a/src/pschdir.c b/src/pschdir.c new file mode 100644 index 0000000..4088d92 --- /dev/null +++ b/src/pschdir.c @@ -0,0 +1,30 @@ +#include /* EACCESS, ELOOP, ENAMETOOLONG, ENOENT, ENOTDIR, errno */ +#include /* fprintf(3), stderr */ +#include /* strerror(3) */ +#include /* EX_OSERR, EX_NOPERM, EX_NOINPUT, EX_UNAVAILABLE, + * EX_USAGE */ +#include /* chdir(2) */ + +char *program_name = "pschdir"; + +int main(int argc, char *argv[]){ + if (argc < 3) { + fprintf(stderr, "Usage: %s [directory] [command (argument...)]\n", + argv[0] == NULL ? program_name : argv[0]); + return EX_USAGE; + } + + if(chdir(argv[1]) != 0){ + fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], strerror(errno)); + + switch (errno) { + case ENAMETOOLONG: return EX_OSERR; + case EACCES: return EX_NOPERM; + case ENOENT: + case ENOTDIR: return EX_NOINPUT; + default: return EX_UNAVAILABLE; + } + } + + execvp(argv[2], &argv[2]); +}