From d8baf576057910f7773ca9bccc59e024279ccea9 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 1 Apr 2023 21:54:14 -0400 Subject: basic implementation of type --- builtins.c | 2 ++ type.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/builtins.c b/builtins.c index a1952fa..55d765e 100644 --- a/builtins.c +++ b/builtins.c @@ -79,6 +79,7 @@ int newgrp_main(int argc, char *argv[]); int pwd_main(int argc, char *argv[]); int read_main(int argc, char *argv[]); int true_main(int argc, char *argv[]); +int type_main(int argc, char *argv[]); int ulimit_main(int argc, char *argv[]); int umask_main(int argc, char *argv[]); int unalias_main(int argc, char *argv[]); @@ -103,6 +104,7 @@ static struct builtin regular_builtins[] = { { "pwd", pwd_main }, { "read", read_main }, { "true", true_main }, + { "type", type_main }, { "ulimit", ulimit_main }, { "umask", umask_main }, { "unalias", unalias_main }, diff --git a/type.c b/type.c index b3f0bf5..4be22c1 100644 --- a/type.c +++ b/type.c @@ -1,7 +1,60 @@ +#include "sh.h" #include +#include +#include int type_main(int argc, char **argv) { - printf("Sorry, %s isn't implemented yet.\n", argv[0]); - return argc; + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + return 1; + } + } + + if (optind >= argc) { + fprintf(stderr, "type: missing operands\n"); + return 1; + } + + int ret = 0; + for (int i = optind; i < argc; i++) { + if (sh_is_special_builtin(argv[i])) { + printf("%s: special built-in\n", argv[i]); + continue; + } + + /* + if (sh_is_function(argv[i])) { + printf("%s: function\n", argv[i]); + continue; + } + */ + + if (sh_is_regular_builtin(argv[i])) { + printf("%s: built-in\n", argv[i]); + continue; + } + + char *alias = sh_get_alias(argv[i]); + if (alias != NULL) { + printf("%s: alias (%s)\n", argv[i], alias); + continue; + } + + // keyword + + char *path = sh_find_in_path(argv[i], "PATH"); + if (path) { + printf("%s: %s\n", argv[i], path); + free(path); + continue; + } + + fprintf(stderr, "type: %s not found\n", argv[i]); + ret = 1; + } + + return ret; } -- cgit v1.2.1