mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
Userland: Allow executing binaries from PATH with env.
This is useful for shebangs: #!/bin/env Shell echo "Hello, World!"
This commit is contained in:
parent
f1b1a78f26
commit
2229b13c97
1 changed files with 35 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -24,17 +24,46 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibCore/DirIterator.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int, char**)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (pledge("stdio", nullptr) < 0) {
|
if (pledge("stdio rpath exec", nullptr) < 0) {
|
||||||
perror("pledge");
|
perror("pledge");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; environ[i]; ++i)
|
const char* filename = nullptr;
|
||||||
printf("%s\n", environ[i]);
|
|
||||||
|
for (int idx = 1; idx < argc; ++idx) {
|
||||||
|
if (StringView { argv[idx] }.contains('=')) {
|
||||||
|
putenv(argv[idx]);
|
||||||
|
} else {
|
||||||
|
filename = argv[idx];
|
||||||
|
argv += idx;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filename == nullptr) {
|
||||||
|
for (auto entry = environ; *entry != nullptr; ++entry)
|
||||||
|
printf("%s\n", *entry);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String filepath = Core::find_executable_in_path(filename);
|
||||||
|
|
||||||
|
if (filepath.is_null()) {
|
||||||
|
warn() << "no " << filename << " in path";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
execv(filepath.characters(), argv);
|
||||||
|
|
||||||
|
perror("execv");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue