mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-15 07:27:42 -04:00
Create which command
This commit is contained in:
parent
35b18af084
commit
4af3126e5d
@ -1,5 +1,7 @@
|
|||||||
.PHONY: default clean force_look
|
.PHONY: default clean force_look
|
||||||
|
|
||||||
|
PROGRAMS=one hello long loop longone longtwo keyboard tsh cpuid shutdown reboot args stat cat which
|
||||||
|
|
||||||
default: dist
|
default: dist
|
||||||
|
|
||||||
tlib/libtlib.a: force_look
|
tlib/libtlib.a: force_look
|
||||||
@ -47,7 +49,10 @@ stat: force_look tlib/libtlib.a
|
|||||||
cat: force_look tlib/libtlib.a
|
cat: force_look tlib/libtlib.a
|
||||||
cd cat; $(MAKE) cat
|
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
|
mkdir -p dist
|
||||||
cp one/a.out dist/one
|
cp one/a.out dist/one
|
||||||
cp hello/a.out dist/hello
|
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 args/args dist/
|
||||||
cp stat/stat dist/
|
cp stat/stat dist/
|
||||||
cp cat/cat dist/
|
cp cat/cat dist/
|
||||||
|
cp which/which dist/
|
||||||
strip dist/*
|
strip dist/*
|
||||||
|
|
||||||
force_look:
|
force_look:
|
||||||
@ -83,4 +89,5 @@ clean:
|
|||||||
cd args; $(MAKE) clean
|
cd args; $(MAKE) clean
|
||||||
cd stat; $(MAKE) clean
|
cd stat; $(MAKE) clean
|
||||||
cd cat; $(MAKE) clean
|
cd cat; $(MAKE) clean
|
||||||
|
cd which; $(MAKE) clean
|
||||||
rm -rf dist
|
rm -rf dist
|
||||||
|
15
programs/which/Makefile
Normal file
15
programs/which/Makefile
Normal 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
|
40
programs/which/src/main.cpp
Normal file
40
programs/which/src/main.cpp
Normal 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
BIN
programs/which/which
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user