diff --git a/programs/Makefile b/programs/Makefile index 1286e0f2..4d582fa0 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -1,5 +1,7 @@ .PHONY: default clean force_look +PROGRAMS=one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot args stat cat which + default: dist tlib/libtlib.a: force_look @@ -47,7 +49,10 @@ stat: force_look tlib/libtlib.a cat: force_look tlib/libtlib.a cd cat; $(MAKE) cat -dist: one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot args stat cat +which: force_look tlib/libtlib.a + cd which; $(MAKE) which + +dist: $(PROGRAMS) mkdir -p dist cp one/a.out dist/one cp hello/a.out dist/hello @@ -63,6 +68,7 @@ dist: one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot arg cp args/args dist/ cp stat/stat dist/ cp cat/cat dist/ + cp which/which dist/ strip dist/* force_look: @@ -83,4 +89,5 @@ clean: cd args; $(MAKE) clean cd stat; $(MAKE) clean cd cat; $(MAKE) clean + cd which; $(MAKE) clean rm -rf dist diff --git a/programs/which/Makefile b/programs/which/Makefile new file mode 100644 index 00000000..77e8f71d --- /dev/null +++ b/programs/which/Makefile @@ -0,0 +1,15 @@ +.PHONY: default clean + +default: which + +include ../../cpp.mk + +%.cpp.o: src/%.cpp + $(CC) -c $< -o $@ $(PROGRAM_FLAGS) + +which: main.cpp.o + $(CC) -o which main.cpp.o $(PROGRAM_LINK_FLAGS) + +clean: + rm -f *.cpp.o + rm -rf which diff --git a/programs/which/src/main.cpp b/programs/which/src/main.cpp new file mode 100644 index 00000000..699ad47f --- /dev/null +++ b/programs/which/src/main.cpp @@ -0,0 +1,40 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#include +#include +#include +#include + +int main(int argc, char* argv[]){ + if(argc == 1){ + print_line("Usage: which executable_path"); + exit(1); + } + + std::string path(argv[1]); + + if(path[0] != '/'){ + path = "/bin/" + path; + } + + auto fd = open(path.c_str()); + + if(fd.valid()){ + print(path); + + close(*fd); + } else { + if(fd.has_error(std::ERROR_NOT_EXISTS)){ + printf("%s not found\n", argv[1]); + } else { + printf("which: error: %s\n", std::error_message(fd.error())); + } + } + + exit(0); +} \ No newline at end of file diff --git a/programs/which/which b/programs/which/which new file mode 100755 index 00000000..c3d945ed Binary files /dev/null and b/programs/which/which differ