Skip to content

Instantly share code, notes, and snippets.

@AlecTaylor
Created September 7, 2018 05:59
Show Gist options
  • Save AlecTaylor/cf3e6cfd0ef2c118e873375a8bb0c6b8 to your computer and use it in GitHub Desktop.
Save AlecTaylor/cf3e6cfd0ef2c118e873375a8bb0c6b8 to your computer and use it in GitHub Desktop.
#include <string>
#include <unistd.h>
static inline void execute(const char *, const char *);
static inline void ltrim(std::string &);
static inline void rtrim(std::string &);
static inline void trim(std::string &);
int main(int argc, char *argv[]) {
if (argv[1] == nullptr || argv[2] == nullptr) {
printf("Usage %s PATH 'cmd0 ; cmd1 ; ...\n", argv[0]);
exit(2);
}
const std::string delim = ";", s = argv[2];
size_t start = 0U, end = s.find(delim);
/*if (end == std::string::npos) {
execute(argv[1], s.c_str());
}
else */while (end != std::string::npos) {
std::string token = s.substr(start, end - start);
trim(token);
execute(argv[1], token.c_str());
start = end + delim.length();
end = s.find(delim, start);
}
}
static inline void execute(const char *path, const char *command) {
const pid_t i = fork();
if (i > 0) {
char *cmd = (char *) malloc(strlen(path) + strlen(command) + 2);
sprintf(cmd, "%s/%s", path, command);
const int ret = execl(cmd, command, "-1", (char *) 0);
free(cmd);
_exit(ret);
} /*else {
perror("fork failed");
_exit(3);
}*/
}
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
cmake_minimum_required(VERSION 3.12)
project(cc)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_DEBUG -O3)
set(CMAKE_CXX_FLAGS_RELEASE -O3)
add_definitions(-O3)
set(CMAKE_CXX_FLAGS "-O3")
message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")
message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
add_executable(c_s c_s.cc)
$ mkdir build; cd $_; cmake -DCMAKE_BUILD_TYPE=Release ..; make
-- The C compiler identification is AppleClang 10.0.0.10001043
-- The CXX compiler identification is AppleClang 10.0.0.10001043
-- Check for working C compiler: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMAKE_CXX_FLAGS_DEBUG is -O3
CMAKE_CXX_FLAGS_RELEASE is -O3
-- Configuring done
-- Generating done
-- Build files have been written to: cc/build
Scanning dependencies of target c_s
[ 50%] Building CXX object CMakeFiles/c_s.dir/c_s.cc.o
[100%] Linking CXX executable c_s
[100%] Built target crappy_sh
$ g++ -std=c++11 -O3 ../c_s.cc -o c_s.manual
$ stat -c "%s %n" c_s*
9920 c_s.manual
14016 c_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment