command()is a small but powerful FOSS C++ function.- Its formula is basically: system()-like syntax + fork()/exec() safety = command()
- Designed to provide an easy, readable, shell-free way to run system commands.i
- It is single .hpp file function, so it is very easy for adding it to your C++ project:
- Just copy
command.hppto your project directory, writeinclude "command.hppto your C++ file and it is ready to use!
While developing my project pacostrap, I realized:
system()is too risky due to shell injection.fork()andexec()are safe, but painful to read and write.
So I wrote my own wrapper — command() — a minimal, readable and safe way to execute commands.
#include <iostream> // for cout, main, return function
#include <string> // for string valuable
#include "command.hpp" // for my own super duper awesome command function
int main() {
std::cout << "Enter directory:\n";
std::string directory;
std::cin >> directory;
command("ls -a " + directory);
return 0;
}makeIf you don't have make installed:
./compile.shmake runOr:
./testcommandThis project is licensed under the GNU Affero General Public License v3 (AGPLv3). Allowed License Reuse: You may reuse this function in projects that are licensed under copyleft (strong or weak) licenses, such as:
- GNU GPLv2, GPLv3, AGPLv3, LGPLv2, LGPLv3
- SSPL
- EUPL
- CeCILL v2.1
- OSL
- RTPL
- MPL For more detail, please look at LICENSE file
