Create which command

This commit is contained in:
Baptiste Wicht 2014-02-23 17:37:01 +01:00
parent 35b18af084
commit 4af3126e5d
4 changed files with 63 additions and 1 deletions

View File

@ -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

15
programs/which/Makefile Normal file
View File

@ -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

View File

@ -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 <file.hpp>
#include <system.hpp>
#include <errors.hpp>
#include <print.hpp>
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);
}

BIN
programs/which/which Executable file

Binary file not shown.