Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from fxdpntthm/execl-example.c
Last active May 19, 2023 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RandyMcMillan/dcfcf1c11d53e12d2ff0e1b975033a70 to your computer and use it in GitHub Desktop.
Save RandyMcMillan/dcfcf1c11d53e12d2ff0e1b975033a70 to your computer and use it in GitHub Desktop.
Small sample program explaining how to use execl command
pyinbash.sh
py3-script.py
pyscript.py
scripts
scripts/*
execl-example
execl-example.dSYM/
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<strings.h>
#include<unistd.h>
#define BASH_EXEC "/bin/bash"
#define LS_EXEC "/bin/ls"
#define BSIZE 50
int main(int argc, char* argv[]){
printf("%d", argc);
if(argc < 2) {
//if we do not have path in our program arguments print help message and exit
printf("Usage: execl-example PATH \n eg. execl-example /usr/bin\n");
fflush(stdout);
return 0;
}
//this is a temp buffer used that will be used to build the argument
char buffer[BSIZE];
//get the buffer to be all NULLs
bzero(buffer, BSIZE);
//build the argument "ls -l /path/to/list/folders" and store it in buffer
sprintf(buffer, "%s -l %s", LS_EXEC, argv[1]);
//printf("buffer: %s\n", buffer);
//build the argument vector
//execute the command
if(execl(BASH_EXEC, BASH_EXEC, "-c", buffer, NULL) < 0){
printf("Something terrible happended!");
return 1;
}
return 0;
}
SHELL := /bin/bash
PWD ?= pwd_unknown
THIS_FILE := $(lastword $(MAKEFILE_LIST))
export THIS_FILE
TIME := $(shell date +%s)
export TIME
ARCH :=$(shell uname -m)
export ARCH
ifeq ($(ARCH),x86_64)
TRIPLET :=x86_64-linux-gnu
export TRIPLET
endif
ifeq ($(ARCH),arm64)
TRIPLET :=aarch64-linux-gnu
export TRIPLET
endif
GCC :=$(shell which gcc)
CLANG :=$(shell which clang)
export GCC
export CLANG
ifeq ($(user),)
HOST_USER := root
HOST_UID := $(strip $(if $(uid),$(uid),0))
else
HOST_USER := $(strip $(if $(USER),$(USER),nodummy))
HOST_UID := $(strip $(if $(shell id -u),$(shell id -u),4000))
endif
export HOST_USER
export HOST_UID
PYTHON := $(shell which python)
export PYTHON
PYTHON2 := $(shell which python2)
export PYTHON2
PYTHON3 := $(shell which python3)
export PYTHON3
PIP := $(shell which pip)
export PIP
PIP2 := $(shell which pip2)
export PIP2
PIP3 := $(shell which pip3)
export PIP3
python_version_full := $(wordlist 2,4,$(subst ., ,$(shell python3 --version 2>&1)))
python_version_major := $(word 1,${python_version_full})
python_version_minor := $(word 2,${python_version_full})
python_version_patch := $(word 3,${python_version_full})
my_cmd.python.3 := $(PYTHON3) some_script.py3
my_cmd := ${my_cmd.python.${python_version_major}}
PYTHON_VERSION := ${python_version_major}.${python_version_minor}.${python_version_patch}
PYTHON_VERSION_MAJOR := ${python_version_major}
PYTHON_VERSION_MINOR := ${python_version_minor}
export python_version_major
export python_version_minor
export python_version_patch
export PYTHON_VERSION
#PROJECT_NAME defaults to name of the current directory.
ifeq ($(project),)
PROJECT_NAME := $(notdir $(PWD))
else
PROJECT_NAME := $(project)
endif
export PROJECT_NAME
#GIT CONFIG
GIT_USER_NAME := $(shell git config user.name)
export GIT_USER_NAME
GIT_USER_EMAIL := $(shell git config user.email)
export GIT_USER_EMAIL
GIT_SERVER := https://github.com
export GIT_SERVER
GIT_REPO_NAME := $(PROJECT_NAME)
export GIT_REPO_NAME
#Usage
#make package-all profile=rsafier
#make package-all profile=asherp
#note on GH_TOKEN.txt file below
ifeq ($(profile),)
GIT_PROFILE := $(GIT_USER_NAME)
ifeq ($(GIT_REPO_ORIGIN),git@github.com:PLEBNET_PLAYGROUND/plebnet-playground-docker.dev.git)
GIT_PROFILE := PLEBNET-PLAYGROUND
endif
ifeq ($(GIT_REPO_ORIGIN),https://github.com/PLEBNET_PLAYGROUND/plebnet-playground-docker.dev.git)
GIT_PROFILE := PLEBNET-PLAYGROUND
endif
else
GIT_PROFILE := $(profile)
endif
export GIT_PROFILE
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
export GIT_BRANCH
GIT_HASH := $(shell git rev-parse --short HEAD || false)
export GIT_HASH
GIT_PREVIOUS_HASH := $(shell git rev-parse --short HEAD^1 || false)
export GIT_PREVIOUS_HASH
GIT_REPO_ORIGIN := $(shell git remote get-url origin || false)
export GIT_REPO_ORIGIN
GIT_REPO_PATH := $(PWD)
export GIT_REPO_PATH
ifeq ($(nocache),true)
NOCACHE := --no-cache
#Force parallel build when --no-cache to speed up build
PARALLEL := --parallel
else
NOCACHE :=
PARALLEL :=
endif
ifeq ($(parallel),true)
PARALLEL := --parallel
endif
ifeq ($(para),true)
PARALLEL := --parallel
endif
export NOCACHE
export PARALLEL
ifeq ($(verbose),true)
VERBOSE := --verbose
else
VERBOSE :=
endif
export VERBOSE
-:
#NOTE: 2 hashes are detected as 1st column output with color
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?##/ {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: help
help:## print verbose help
@echo 'make [COMMAND] [EXTRA_ARGUMENTS] '
@echo ''
@echo 'make '
@echo ' make all install and run playground and cluster'
@echo ' make help print help'
@echo ' make report print environment variables'
@echo ' nocache=true verbose=true'
@echo ''
@echo ' [DEV ENVIRONMENT]: '
@echo ''
@echo ' make signin profile=gh-user ~/GH_TOKEN.txt required from github.com'
@echo ' make build'
@echo ' make package-all'
@echo ''
@echo ' [EXAMPLES]:'
@echo ''
@echo ' make run nocache=true verbose=true'
@echo ''
@echo ' make init && play help'
@echo ''
@sed -n 's/^# //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/# /'
@sed -n 's/^## //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/## /'
@sed -n 's/^### //p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/### /'
.PHONY: report
report:## print environment arguments
@echo ''
@echo ' [ARGUMENTS] '
@echo ' args:'
@echo ' - THIS_FILE=${THIS_FILE}'
@echo ' - TIME=${TIME}'
@echo ' - ARCH=${ARCH}'
@echo ' - TRIPLET=${TRIPLET}'
@echo ' - PROJECT_NAME=${PROJECT_NAME}'
@echo ' - HOME=${HOME}'
@echo ' - PWD=${PWD}'
@echo ' - PYTHON=${PYTHON}'
@echo ' - PYTHON3=${PYTHON3}'
@echo ' - PYTHON_VERSION=${PYTHON_VERSION}'
@echo ' - PYTHON_VERSION_MAJOR=${PYTHON_VERSION_MAJOR}'
@echo ' - PYTHON_VERSION_MINOR=${PYTHON_VERSION_MINOR}'
@echo ' - PIP=${PIP}'
@echo ' - PIP3=${PIP3}'
@echo ' - PACKAGE_PREFIX=${PACKAGE_PREFIX}'
@echo ' - HOST_USER=${HOST_USER}'
@echo ' - HOST_UID=${HOST_UID}'
@echo ' - SERVICE_TARGET=${SERVICE_TARGET}'
@echo ' - DOCKER_COMPOSE=${DOCKER_COMPOSE}'
@echo ' - GIT_USER_NAME=${GIT_USER_NAME}'
@echo ' - GIT_USER_EMAIL=${GIT_USER_EMAIL}'
@echo ' - GIT_SERVER=${GIT_SERVER}'
@echo ' - GIT_PROFILE=${GIT_PROFILE}'
@echo ' - GIT_BRANCH=${GIT_BRANCH}'
@echo ' - GIT_HASH=${GIT_HASH}'
@echo ' - GIT_PREVIOUS_HASH=${GIT_PREVIOUS_HASH}'
@echo ' - GIT_REPO_ORIGIN=${GIT_REPO_ORIGIN}'
@echo ' - GIT_REPO_NAME=${GIT_REPO_NAME}'
@echo ' - GIT_REPO_PATH=${GIT_REPO_PATH}'
@echo ' - NOCACHE=${NOCACHE}'
@echo ' - VERBOSE=${VERBOSE}'
@echo ' - PASSWORD=${PASSWORD}'
@echo ' - CMD_ARGUMENTS=${CMD_ARGUMENTS}'
#######################
ORIGIN_DIR:=$(PWD)
MACOS_TARGET_DIR:=/var/root/$(PROJECT_NAME)
LINUX_TARGET_DIR:=/root/$(PROJECT_NAME)
export ORIGIN_DIR
export TARGET_DIR
.PHONY: all venv test-venv init
all: init execl-example## all
execl-example:##
$(GCC) -g execl-example.c -o execl-example
venv:## create python3 virtualenv .venv
test -d .venv || $(PYTHON3) -m virtualenv .venv
( \
source .venv/bin/activate; pip install -r requirements.txt; \
);
@echo "To activate (venv)"
@echo "try:"
@echo ". .venv/bin/activate"
@echo "or:"
@echo "make test-venv"
$(GCC) -g execl-example.c -o execl-example
test-venv:## test virutalenv .venv
# insert test commands here
test -d .venv || $(PYTHON3) -m virtualenv .venv
( \
source .venv/bin/activate; pip install -r requirements.txt; \
);
.PHONY: init
init: venv## basic setup
ifneq ($(shell id -u),0)
@echo
@echo $(shell id -u -n) 'try:'
@echo 'make super'
@echo 'If permissions issue...'
@echo
endif
git config --global --add safe.directory $(PWD)
$(PYTHON3) -m pip install --upgrade pip 2>/dev/null
$(PYTHON3) -m pip install -q -r requirements.txt 2>/dev/null
mkdir -p scripts
@./pyinbash.sh
install -v $(PWD)/pyinbash.sh $(PWD)/scripts/
chown -R $(shell id -u) * || echo
bash -c "scripts/pyinbash.sh"
install -v $(PWD)/py3-script.py $(PWD)/scripts/
pushd $(PWD)/scripts > /dev/null; for string in *; do sudo chmod -R o+rwx /usr/local/bin/$$string; done; popd 2>/dev/null || echo
pushd $(PWD)/scripts > /dev/null; for string in *; do install $$string /usr/local/bin/$$string; done; popd 2>/dev/null || echo
#######################
.PHONY: docs
docs: init
@echo "Use 'make docs nocache=true' to force docs rebuild..."
echo "## MAKE COMMAND" >> MAKE.md
echo '```' > MAKE.md
make help >> MAKE.md
echo '```' >> MAKE.md
#######################
.PHONY: push
push:
@echo push
git checkout -b $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
git push --set-upstream origin $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
git add docs
git commit --amend --no-edit --allow-empty || echo failed to commit --amend --no-edit
git push -f origin $(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH):$(TIME)/$(GIT_PREVIOUS_HASH)/$(GIT_HASH)
SIGNIN=randymcmillan
export SIGNIN
.PHONY: signin package-docker package-all
signin:
bash -c 'cat ~/GH_TOKEN.txt | docker login ghcr.io -u $(GIT_PROFILE) --password-stdin'
#Place a file named GH_TOKEN.txt in your $HOME - create in https://github.com/settings/tokens (Personal access tokens)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment