Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,33 +16,34 @@ So I wrote my own wrapper — `command()` — a minimal, readable and safe way t

## Example Usage
```cpp
#include <iostream> // for cout, main, return function
#include <string> // for string valuable
#include "command.hpp" // for my own super duper awesome command function
#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);
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:
Expand Down
45 changes: 45 additions & 0 deletions command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstring>
#include "command.hpp"

std::vector<std::string> split_command(const std::string& komut) {
std::istringstream iss(komut);
std::string kelime;
std::vector<std::string> sonuc;
while (iss >> kelime) {
sonuc.push_back(kelime);
}
return sonuc;
}

int command(const std::string& komut) {
std::vector<std::string> args = split_command(komut);
if (args.empty()) {
std::cerr << "empty command" << std::endl;
return 1;
} std::vector<char*> 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
}}}
46 changes: 5 additions & 41 deletions command.hpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
#ifndef COMMAND_HPP
#define COMMAND_HPP
#include <iostream>
#include <string>

#include <vector>
#include <sstream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstring>
inline std::vector<std::string> split_command(const std::string& komut) {
std::istringstream iss(komut);
std::string kelime;
std::vector<std::string> sonuc;
while (iss >> kelime) {
sonuc.push_back(kelime);
}
return sonuc;
}
#include <string>

std::vector<std::string> split_command(const std::string& komut);
int command(const std::string& komut);

inline int command(const std::string& komut) {
std::vector<std::string> args = split_command(komut);
if (args.empty()) {
std::cerr << "empty command" << std::endl;
return 1;
} std::vector<char*> 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
2 changes: 0 additions & 2 deletions compile.sh

This file was deleted.

6 changes: 6 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <command.hpp>

int main() {
command("ls");
return 0;
}
Binary file removed testcommand
Binary file not shown.
10 changes: 0 additions & 10 deletions testcommand.cpp

This file was deleted.