diff --git a/Makefile b/Makefile index e19fd07..66bf7bd 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,28 @@ CXX = g++ -TARGET = testcommand +CFLAGS = -Wall -Wextra -Werror +OBJS = command.o +TARGET = libcommand.so +PREFIX ?= /usr/local + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) -shared $(OBJS) -o $(TARGET) + +command.o: command.cpp command.cpp + $(CXX) $(CFLAGS) -fPIC -c command.cpp -o command.o + +install: $(TARGET) + mkdir -p $(PREFIX)/lib + mkdir -p $(PREFIX)/include + install -m 755 $(TARGET) $(PREFIX)/lib/ + install -m 644 command.hpp $(PREFIX)/include/ + +uninstall: + rm -f $(PREFIX)/lib/$(TARGET) + rm -f $(PREFIX)/include/command.hpp -$(TARGET): testcommand.cpp - $(CXX) testcommand.cpp -o $(TARGET) -run: - ./$(TARGET) clean: - rm $(TARGET) + rm -f $(TARGET) $(OBJS) + +.PHONY: all clean install uninstall diff --git a/README.md b/README.md index 1697729..1dedda8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ - 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.hpp`](command.hpp) to your project directory, write ```include "command.hpp``` to your C++ file and it is ready to use! ## Philosophy / History While developing my project [`pacostrap`](https://github.com/npc-gnu/pacostrap), I realized: @@ -17,33 +16,34 @@ So I wrote my own wrapper — `command()` — a minimal, readable and safe way t ## Example Usage ```cpp -#include // for cout, main, return function -#include // for string valuable -#include "command.hpp" // for my own super duper awesome command function +#include // 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); + command("ls"); return 0; } ``` -### Compiling +### Compiling library ```Bash make +sudo make install ``` -If you don't have make installed: + +### Compiling your own code with library ```Bash -./compile.sh +g++ main.cpp -lcommand -o main.elf ``` + ### Running ```Bash -make run +./main.elf ``` -Or: + +### Uninstalling library ```Bash -./testcommand +sudo make uninstall ``` + ## Licensing This project is licensed under the GNU Affero General Public License v3 (AGPLv3). Allowed License Reuse: diff --git a/command.cpp b/command.cpp new file mode 100644 index 0000000..866261f --- /dev/null +++ b/command.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include "command.hpp" + +std::vector split_command(const std::string& komut) { + std::istringstream iss(komut); + std::string kelime; + std::vector sonuc; + while (iss >> kelime) { + sonuc.push_back(kelime); + } + return sonuc; +} + +int command(const std::string& komut) { + std::vector args = split_command(komut); + if (args.empty()) { + std::cerr << "empty command" << std::endl; + return 1; + } std::vector argv; + for (auto& s : args) { + argv.push_back(strdup(s.c_str())); + } argv.push_back(nullptr); + pid_t pid = fork(); + if (pid == -1) { + std::cerr << "fork() error!" << std::endl; + return 1; + } else if (pid == 0) { + execvp(argv[0], argv.data()); + std::cerr << "execvp failed" << std::endl; + exit(1); + } else { + int durum; + waitpid(pid, &durum, 0); + for (char* ptr : argv) free(ptr); + if (WIFEXITED(durum)) { + return WEXITSTATUS(durum); // Çocuğun exit kodunu döndür + } else { + return 1; // abnormal termination + }}} diff --git a/command.hpp b/command.hpp index 2e9c2a1..cc4b7f7 100644 --- a/command.hpp +++ b/command.hpp @@ -1,46 +1,10 @@ #ifndef COMMAND_HPP #define COMMAND_HPP -#include -#include + #include -#include -#include -#include -#include -inline std::vector split_command(const std::string& komut) { - std::istringstream iss(komut); - std::string kelime; - std::vector sonuc; - while (iss >> kelime) { - sonuc.push_back(kelime); - } - return sonuc; -} +#include + +std::vector split_command(const std::string& komut); +int command(const std::string& komut); -inline int command(const std::string& komut) { - std::vector args = split_command(komut); - if (args.empty()) { - std::cerr << "empty command" << std::endl; - return 1; - } std::vector argv; - for (auto& s : args) { - argv.push_back(strdup(s.c_str())); - } argv.push_back(nullptr); - pid_t pid = fork(); - if (pid == -1) { - std::cerr << "fork() error!" << std::endl; - return 1; - } else if (pid == 0) { - execvp(argv[0], argv.data()); - std::cerr << "execvp failed" << std::endl; - exit(1); - } else { - int durum; - waitpid(pid, &durum, 0); - for (char* ptr : argv) free(ptr); - if (WIFEXITED(durum)) { - return WEXITSTATUS(durum); // Çocuğun exit kodunu döndür - } else { - return 1; // abnormal termination - }}} #endif diff --git a/compile.sh b/compile.sh deleted file mode 100755 index 05b89fd..0000000 --- a/compile.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -g++ testcommand.cpp -o testcommand diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e15017c --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + command("ls"); + return 0; +} diff --git a/testcommand b/testcommand deleted file mode 100755 index eece584..0000000 Binary files a/testcommand and /dev/null differ diff --git a/testcommand.cpp b/testcommand.cpp deleted file mode 100644 index ca69470..0000000 --- a/testcommand.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include "command.hpp" -int main(){ - std::cout << "Enter a directory.\n"; - std::string directory; - std::cin >> directory; - command("ls -a " + directory); - return 0; -}