Skip to content
Snippets Groups Projects
Commit e9938cb9 authored by Eric Vidal's avatar Eric Vidal :speech_balloon:
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
.git/
config.mak
src/include/66-tools/config.h
*.lo
*.a.*
66-envfile
66-getenv
66-gnwenv
66-which
66-writenv
execl-cmdline
execl-envfile
execl-subuidgid
Main author:
Eric Vidal <eric@obarun.org>
Contributors:
Danilo Spinella <danyspin97@protonmail.com> //66-which API
COPYING 0 → 100644
Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
All rights reserved.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
INSTALL 0 → 100644
Build Instructions
------------------
* Requirements
------------
- A POSIX-compliant C development environment
- GNU make version 3.81 or later
- skalibs version 2.8.1.0 or later
- execline version 2.5.1.0 or later
- oblibs version 0.0.1.3 or later
- If cross-compiling: the sysdeps for your target architecture
(see the "Cross-compilation" section below)
This software will install on any operating system that implements
POSIX.1-2008, available at:
http://pubs.opengroup.org/onlinepubs/9699919799/
* Standard usage
--------------
./configure && make && sudo make install
will work for most users.
It will install the static libraries in /usr/lib/66-tools, the shared
libraries in /lib.
Please note that static libraries in /usr/lib/66-tools *will not*
be found by a default linker invocation: you need -L/usr/lib/66-tools.
Other obarun.org software automatically handles that case if the
default configuration is used, but if you change the configuration,
remember to use the appropriate --with-lib configure option.
You can strip the libraries of their extra symbols via "make strip"
before the "make install" phase. It will shave a few bytes off them.
* Customization
-------------
You can customize the installation process via flags given to configure.
See ./configure --help for a list of all available configure options.
* Environment variables
---------------------
Controlling a build process via environment variables is a big and
dangerous hammer. You should try and pass flags to configure instead;
nevertheless, a few standard environment variables are recognized.
If the CC environment variable is set, its value will override compiler
detection by configure. The --host=HOST option will still add a HOST-
prefix to the value of CC.
The values of CFLAGS, CPPFLAGS and LDFLAGS will be appended to the
default flags set by configure. To override those defaults instead
of appending to them, use the CPPFLAGS, CFLAGS and LDFLAGS
_make variables_ instead of environment variables.
* Make variables
--------------
You can invoke make with a few variables for more configuration.
CC, CFLAGS, CPPFLAGS, LDFLAGS, LDLIBS, AR, RANLIB, STRIP, INSTALL and
CROSS_COMPILE can all be overridden on the make command line. This is
an even bigger hammer than running ./configure with environment
variables, so it is advised to only do this when it is the only way of
obtaining the behaviour you want.
DESTDIR can be given on the "make install" command line in order to
install to a staging directory.
* Shared libraries
----------------
Software from obarun.org is small enough that shared libraries are
generally not worth using. Static linking is simpler and incurs less
runtime overhead and less points of failure: but since 66-tools only
provides libraries, both versions are built by default.
Nevertheless, you can:
* avoid building shared libraries: --disable-shared
* avoid building static libraries: --disable-static
If you are using a GNU/Linux system, be aware that the GNU libc
behaves badly with static linking and produces huge executables,
so if you plan on making static executables, you should consider
using another libc, which you also need to use when compiling
66-tools. musl is recommended: http://musl-libc.org/
* Cross-compilation
-----------------
skarnet.org packages centralize all the difficulty of
cross-compilation in one place: skalibs. Once you have
cross-compiled skalibs, the rest is easy.
* Use the --host=HOST option to configure, HOST being the triplet
for your target.
* Make sure your cross-toolchain binaries (i.e. prefixed with HOST-)
are accessible via your PATH environment variable.
* Make sure to use the correct version of skalibs for your target,
and the correct sysdeps directory, making use of the
--with-include, --with-lib, --with-dynlib and --with-sysdeps
options as necessary.
* The slashpackage convention
---------------------------
The slashpackage convention (http://cr.yp.to/slashpackage.html)
is a package installation scheme that provides a few guarantees
over other conventions such as the FHS, for instance fixed
absolute pathnames. obarun.org packages support it: use the
--enable-slashpackage option to configure, or
--enable-slashpackage=DIR for a prefixed DIR/package tree.
This option will activate slashpackage support during the build
and set slashpackage-compatible installation directories. If
$version is the current 66-tools version number:
--bindir will be set to /package/prog/66-tools-$version/command
--includedir will be set to /package/prog/66-tools-$version/include
--libdir will be set to /package/prog/66-tools-$version/library
--dynlibdir will be set to /package/prog/66-tools-$version/library.so
Note that --datadir will be unchanged, because the data exported
by 66-tools, i.e. the leap second table, is system-wide. You should
manually specify --datadir=... if you want to deviate from the default.
--prefix is pretty much ignored when you use --enable-slashpackage:
it will only impact --datadir. You should probably not use both
--enable-slashpackage and --prefix.
When using slashpackage, two additional Makefile targets are
available after "make install":
- "make -L update" changes the default version of the software to the
freshly installed one. (This is useful when you have several installed
versions of the same software, which slashpackage supports.)
- "make -L global-links" adds links from DIR/command and DIR/library.so
to the default version of the binaries and shared libraries.
The "-L" option to make is necessary because targets are symbolic links,
and the default make behaviour is to check the pointed file's timestamp
and not the symlink's timestamp.
* Out-of-tree builds
------------------
obarun.org packages do not support out-of-tree builds. They
are small, so it does not cost much to duplicate the entire
source tree if parallel builds are needed.
Makefile 0 → 100644
#
# This Makefile requires GNU make.
#
# Do not make changes here.
# Use the included .mak files.
#
it: all
make_need := 3.81
ifeq "" "$(strip $(filter $(make_need), $(firstword $(sort $(make_need) $(MAKE_VERSION)))))"
fail := $(error Your make ($(MAKE_VERSION)) is too old. You need $(make_need) or newer)
endif
CC = $(error Please use ./configure first)
STATIC_LIBS :=
SHARED_LIBS :=
INTERNAL_LIBS :=
EXTRA_TARGETS :=
LIB_DEFS :=
define library_definition
LIB$(firstword $(subst =, ,$(1))) := lib$(lastword $(subst =, ,$(1))).$(if $(DO_ALLSTATIC),a,so).xyzzy
ifdef DO_SHARED
SHARED_LIBS += lib$(lastword $(subst =, ,$(1))).so.xyzzy
endif
ifdef DO_STATIC
STATIC_LIBS += lib$(lastword $(subst =, ,$(1))).a.xyzzy
endif
endef
-include config.mak
include package/targets.mak
$(foreach var,$(LIB_DEFS),$(eval $(call library_definition,$(var))))
include package/deps.mak
version_m := $(basename $(version))
version_M := $(basename $(version_m))
version_l := $(basename $(version_M))
CPPFLAGS_ALL := $(CPPFLAGS_AUTO) $(CPPFLAGS)
CFLAGS_ALL := $(CFLAGS_AUTO) $(CFLAGS)
ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
CFLAGS_SHARED := -fPIC
else
CFLAGS_SHARED :=
endif
LDFLAGS_ALL := $(LDFLAGS_AUTO) $(LDFLAGS)
AR := $(CROSS_COMPILE)ar
RANLIB := $(CROSS_COMPILE)ranlib
STRIP := $(CROSS_COMPILE)strip
INSTALL := ./tools/install.sh
ALL_BINS := $(LIBEXEC_TARGETS) $(BIN_TARGETS)
ALL_LIBS := $(SHARED_LIBS) $(STATIC_LIBS) $(INTERNAL_LIBS)
ALL_INCLUDES := $(wildcard src/include/$(package)/*.h)
all: $(ALL_LIBS) $(ALL_BINS) $(ALL_INCLUDES)
clean:
@exec rm -f $(ALL_LIBS) $(ALL_BINS) $(wildcard src/*/*.o src/*/*.lo) $(EXTRA_TARGETS)
distclean: clean
@exec rm -f config.mak src/include/$(package)/config.h
tgz: distclean
@. package/info && \
rm -rf /tmp/$$package-$$version && \
cp -a . /tmp/$$package-$$version && \
cd /tmp && \
tar -zpcv --owner=0 --group=0 --numeric-owner --exclude=.git* -f /tmp/$$package-$$version.tar.gz $$package-$$version && \
exec rm -rf /tmp/$$package-$$version
strip: $(ALL_LIBS) $(ALL_BINS)
ifneq ($(strip $(STATIC_LIBS)),)
exec $(STRIP) -x -R .note -R .comment -R .note.GNU-stack $(STATIC_LIBS)
endif
ifneq ($(strip $(ALL_BINS)$(SHARED_LIBS)),)
exec $(STRIP) -R .note -R .comment -R .note.GNU-stack $(ALL_BINS) $(SHARED_LIBS)
endif
install: install-dynlib install-libexec install-bin install-lib install-include
install-dynlib: $(SHARED_LIBS:lib%.so.xyzzy=$(DESTDIR)$(dynlibdir)/lib%.so)
install-libexec: $(LIBEXEC_TARGETS:%=$(DESTDIR)$(libexecdir)/%)
install-bin: $(BIN_TARGETS:%=$(DESTDIR)$(bindir)/%)
install-lib: $(STATIC_LIBS:lib%.a.xyzzy=$(DESTDIR)$(libdir)/lib%.a)
install-include: $(ALL_INCLUDES:src/include/$(package)/%.h=$(DESTDIR)$(includedir)/$(package)/%.h)
install-data: $(ALL_DATA:src/etc/%=$(DESTDIR)$(datadir)/%)
ifneq ($(exthome),)
$(DESTDIR)$(exthome): $(DESTDIR)$(home)
exec $(INSTALL) -l $(notdir $(home)) $(DESTDIR)$(exthome)
update: $(DESTDIR)$(exthome)
global-links: $(DESTDIR)$(exthome) $(SHARED_LIBS:lib%.so.xyzzy=$(DESTDIR)$(sproot)/library.so/lib%.so.$(version_M)) $(BIN_TARGETS:%=$(DESTDIR)$(sproot)/command/%)
$(DESTDIR)$(sproot)/command/%: $(DESTDIR)$(home)/command/%
exec $(INSTALL) -D -l ..$(subst $(sproot),,$(exthome))/command/$(<F) $@
$(DESTDIR)$(sproot)/library.so/lib%.so.$(version_M): $(DESTDIR)$(dynlibdir)/lib%.so.$(version_M)
exec $(INSTALL) -D -l ..$(subst $(sproot),,$(exthome))/library.so/$(<F) $@
.PHONY: update global-links
endif
$(DESTDIR)$(datadir)/%: src/etc/%
exec $(INSTALL) -D -m 644 $< $@
$(DESTDIR)$(dynlibdir)/lib%.so: lib%.so.xyzzy
$(INSTALL) -D -m 755 $< $@.$(version) && \
$(INSTALL) -l $(@F).$(version) $@.$(version_m) && \
$(INSTALL) -l $(@F).$(version_m) $@.$(version_M) && \
exec $(INSTALL) -l $(@F).$(version_M) $@
$(DESTDIR)$(libexecdir)/% $(DESTDIR)$(bindir)/%: % package/modes
exec $(INSTALL) -D -m 600 $< $@
grep -- ^$(@F) < package/modes | { read name mode owner && \
if [ x$$owner != x ] ; then chown -- $$owner $@ ; fi && \
chmod $$mode $@ ; }
$(DESTDIR)$(libdir)/lib%.a: lib%.a.xyzzy
exec $(INSTALL) -D -m 644 $< $@
$(DESTDIR)$(includedir)/$(package)/%.h: src/include/$(package)/%.h
exec $(INSTALL) -D -m 644 $< $@
%.o: %.c
exec $(CC) $(CPPFLAGS_ALL) $(CFLAGS_ALL) -c -o $@ $<
%.lo: %.c
exec $(CC) $(CPPFLAGS_ALL) $(CFLAGS_ALL) $(CFLAGS_SHARED) -c -o $@ $<
$(ALL_BINS):
exec $(CC) -o $@ $(CFLAGS_ALL) $(LDFLAGS_ALL) $(LDFLAGS_NOSHARED) $^ $(EXTRA_LIBS) $(LDLIBS)
lib%.a.xyzzy:
exec $(AR) rc $@ $^
exec $(RANLIB) $@
lib%.so.xyzzy:
exec $(CC) -o $@ $(CFLAGS_ALL) $(CFLAGS_SHARED) $(LDFLAGS_ALL) $(LDFLAGS_SHARED) -Wl,-soname,$(patsubst lib%.so.xyzzy,lib%.so.$(version_M),$@) $^ $(EXTRA_LIBS) $(LDLIBS)
.PHONY: it all clean distclean tgz strip install install-dynlib install-bin install-lib install-include install-data
.DELETE_ON_ERROR:
configure 0 → 100755
#!/bin/sh
cd `dirname "$0"`
. package/info
usage () {
cat <<EOF
Usage: $0 [OPTION]... [TARGET]
Defaults for the options are specified in brackets.
System types:
--target=TARGET configure to run on target TARGET [detected]
--host=TARGET same as --target
Installation directories:
--prefix=PREFIX main installation prefix [/]
--exec-prefix=EPREFIX installation prefix for executable files [PREFIX]
Fine tuning of the installation directories:
--dynlibdir=DIR shared library files [PREFIX/lib]
--bindir=BINDIR user executables [EPREFIX/bin]
--libexecdir=DIR package-scoped executables [EPREFIX/libexec]
--libdir=DIR static library files [PREFIX/lib/$package]
--includedir=DIR C header files [PREFIX/include]
If no --prefix option is given, by default libdir (but not dynlibdir) will be
/usr/lib/$package, and includedir will be /usr/include.
Dependencies:
--with-sysdeps=DIR use sysdeps in DIR [PREFIX/lib/skalibs/sysdeps]
--with-include=DIR add DIR to the list of searched directories for headers
--with-lib=DIR add DIR to the list of searched directories for static libraries
--with-dynlib=DIR add DIR to the list of searched directories for shared libraries
If no --prefix option is given, by default sysdeps will be fetched from
/usr/lib/skalibs/sysdeps.
Optional features:
--enable-shared build shared libraries [disabled]
--disable-static do not build static libraries [enabled]
--disable-allstatic do not prefer linking against static libraries [enabled]
--enable-static-libc make entirely static binaries [disabled]
--disable-all-pic do not build executables and static libs as PIC [enabled]
--enable-slashpackage[=ROOT] assume /package installation at ROOT [disabled]
--enable-absolute-paths do not rely on PATH to access this package's binaries,
hardcode absolute BINDIR/foobar paths instead [disabled]
EOF
exit 0
}
# Helper functions
# If your system does not have printf, you can comment this, but it is
# generally not a good idea to use echo.
# See http://www.etalabs.net/sh_tricks.html
echo () {
IFS=" "
printf %s\\n "$*"
}
quote () {
tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
$1
EOF
echo "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#" -e "s|\*/|* /|g"
}
fail () {
echo "$*"
exit 1
}
fnmatch () {
eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac"
}
cmdexists () {
type "$1" >/dev/null 2>&1
}
trycc () {
test -z "$CC_AUTO" && cmdexists "$1" && CC_AUTO="$*"
}
stripdir () {
while eval "fnmatch '*/' \"\${$1}\"" ; do
eval "$1=\${$1%/}"
done
}
tryflag () {
echo "Checking whether compiler accepts $2 ..."
echo "typedef int x;" > "$tmpc"
if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST "$2" -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
echo " ... yes"
eval "$1=\"\${$1} \$2\""
eval "$1=\${$1# }"
return 0
else
echo " ... no"
return 1
fi
}
tryldflag () {
echo "Checking whether linker accepts $2 ..."
echo "typedef int x;" > "$tmpc"
if $CC_AUTO $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -nostdlib "$2" -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
echo " ... yes"
eval "$1=\"\${$1} \$2\""
eval "$1=\${$1# }"
return 0
else
echo " ... no"
return 1
fi
}
# Actual script
CC_AUTO=
CPPFLAGS_AUTO="-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -iquote src/include-local -Isrc/include"
CPPFLAGS_POST="$CPPFLAGS"
CPPFLAGS=
CFLAGS_AUTO="-pipe -Wall"
CFLAGS_POST="$CFLAGS"
CFLAGS=-O2
LDFLAGS_AUTO=
LDFLAGS_POST="$LDFLAGS"
LDFLAGS=
LDFLAGS_NOSHARED=
LDFLAGS_SHARED=-shared
prefix=
exec_prefix='$prefix'
dynlibdir='$prefix/lib'
libexecdir='$exec_prefix/libexec'
bindir='$exec_prefix/bin'
libdir='$prefix/lib/$package'
includedir='$prefix/include'
sysdeps='$prefix/lib/skalibs/sysdeps'
manualsysdeps=false
shared=false
static=true
allpic=true
slashpackage=false
abspath=false
sproot=
home=
exthome=
allstatic=true
evenmorestatic=false
addincpath=''
addlibspath=''
addlibdpath=''
vpaths=''
vpathd=''
build=
for arg ; do
case "$arg" in
--help) usage ;;
--prefix=*) prefix=${arg#*=} ;;
--exec-prefix=*) exec_prefix=${arg#*=} ;;
--dynlibdir=*) dynlibdir=${arg#*=} ;;
--libexecdir=*) libexecdir=${arg#*=} ;;
--bindir=*) bindir=${arg#*=} ;;
--libdir=*) libdir=${arg#*=} ;;
--includedir=*) includedir=${arg#*=} ;;
--with-sysdeps=*) sysdeps=${arg#*=} manualsysdeps=true ;;
--with-include=*) var=${arg#*=} ; stripdir var ; addincpath="$addincpath -I$var" ;;
--with-lib=*) var=${arg#*=} ; stripdir var ; addlibspath="$addlibspath -L$var" ; vpaths="$vpaths $var" ;;
--with-dynlib=*) var=${arg#*=} ; stripdir var ; addlibdpath="$addlibdpath -L$var" ; vpathd="$vpathd $var" ;;
--enable-shared|--enable-shared=yes) shared=true ;;
--disable-shared|--enable-shared=no) shared=false ;;
--enable-static|--enable-static=yes) static=true ;;
--disable-static|--enable-static=no) static=false ;;
--enable-allstatic|--enable-allstatic=yes) allstatic=true ;;
--disable-allstatic|--enable-allstatic=no) allstatic=false ; evenmorestatic=false ;;
--enable-static-libc|--enable-static-libc=yes) allstatic=true ; evenmorestatic=true ;;
--disable-static-libc|--enable-static-libc=no) evenmorestatic=false ;;
--enable-all-pic|--enable-all-pic=yes) allpic=true ;;
--disable-all-pic|--enable-all-pic=no) allpic=false ;;
--enable-slashpackage=*) sproot=${arg#*=} ; slashpackage=true ; ;;
--enable-slashpackage) sproot= ; slashpackage=true ;;
--disable-slashpackage) sproot= ; slashpackage=false ;;
--enable-absolute-paths|--enable-absolute-paths=yes) abspath=true ;;
--disable-absolute-paths|--enable-absolute-paths=no) abspath=false ;;
--enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;;
--host=*|--target=*) target=${arg#*=} ;;
--build=*) build=${arg#*=} ;;
-* ) echo "$0: unknown option $arg" ;;
*=*) ;;
*) target=$arg ;;
esac
done
# Add /usr in the default default case
if test -z "$prefix" ; then
if test "$libdir" = '$prefix/lib/$package' ; then
libdir=/usr/lib/$package
fi
if test "$includedir" = '$prefix/include' ; then
includedir=/usr/include
fi
if test "$sysdeps" = '$prefix/lib/skalibs/sysdeps' ; then
sysdeps=/usr/lib/skalibs/sysdeps
fi
fi
# Expand installation directories
stripdir prefix
for i in exec_prefix dynlibdir libexecdir bindir libdir includedir sysdeps sproot ; do
eval tmp=\${$i}
eval $i=$tmp
stripdir $i
done
# Get usable temp filenames
i=0
set -C
while : ; do
i=$(($i+1))
tmpc="./tmp-configure-$$-$PPID-$i.c"
tmpe="./tmp-configure-$$-$PPID-$i.tmp"
2>|/dev/null > "$tmpc" && break
2>|/dev/null > "$tmpe" && break
test "$i" -gt 50 && fail "$0: cannot create temporary files"
done
set +C
trap 'rm -f "$tmpc" "$tmpe"' EXIT ABRT INT QUIT TERM HUP
# Set slashpackage values
if $slashpackage ; then
home=${sproot}/package/${category}/${package}-${version}
exthome=${sproot}/package/${category}/${package}
if $manualsysdeps ; then
:
else
sysdeps=${sproot}/package/prog/skalibs/sysdeps
fi
extbinprefix=${exthome}/command
dynlibdir=${home}/library.so
bindir=${home}/command
libdir=${home}/library
libexecdir=$bindir
includedir=${home}/include
while read dep condvar ; do
if test -n "$condvar" ; then
eval "cond=$condvar"
else
cond=true
fi
if $cond ; then
addincpath="$addincpath -I${sproot}${dep}/include"
vpaths="$vpaths ${sproot}${dep}/library"
addlibspath="$addlibspath -L${sproot}${dep}/library"
vpathd="$vpathd ${sproot}${dep}/library.so"
addlibdpath="$addlibdpath -L${sproot}${dep}/library.so"
fi
done < package/deps-build
fi
# Find a C compiler to use
if test -n "$target" && test x${build} != x${target} ; then
cross=${target}-
else
cross=
fi
echo "Checking for C compiler..."
trycc ${CC}
if test -n "$CC_AUTO" ; then
b=`basename "$CC"`
adjust_cross=false
if test "$b" != "$CC" ; then
adjust_cross=true
echo "$0: warning: compiler $CC is declared with its own path. If it's not accessible via PATH, you will need to pass AR, RANLIB and STRIP make variables to the make invocation." 1>&2
fi
if test -n "$cross" ; then
if test "$b" = "${b##$cross}" ; then
echo "$0: warning: compiler $CC is declared as a cross-compiler for target $target but does not start with prefix ${cross}" 1>&2
elif $adjust_cross ; then
cross=`dirname "$CC"`/"$cross"
fi
fi
fi
trycc ${cross}gcc
trycc ${cross}clang
trycc ${cross}cc
test -n "$CC_AUTO" || { echo "$0: cannot find a C compiler" ; exit 1 ; }
echo " ... $CC_AUTO"
echo "Checking whether C compiler works... "
echo "typedef int x;" > "$tmpc"
if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -c -o /dev/null "$tmpc" 2>"$tmpe" ; then
echo " ... yes"
else
echo " ... no. Compiler output follows:"
cat < "$tmpe"
exit 1
fi
echo "Checking target system type..."
if test -z "$target" ; then
if test -n "$build" ; then
target=$build ;
else
target=$($CC_AUTO -dumpmachine 2>/dev/null) || target=unknown
fi
fi
echo " ... $target"
if test ! -d $sysdeps || test ! -f $sysdeps/target ; then
echo "$0: error: $sysdeps is not a valid sysdeps directory"
exit 1
fi
if [ "x$target" != "x$(cat $sysdeps/target)" ] ; then
echo "$0: error: target $target does not match the contents of $sysdeps/target"
exit 1
fi
if $allpic ; then
tryflag CPPFLAGS_AUTO -fPIC
fi
spawn_lib=$(cat $sysdeps/spawn.lib)
socket_lib=$(cat $sysdeps/socket.lib)
sysclock_lib=$(cat $sysdeps/sysclock.lib)
tainnow_lib=$(cat $sysdeps/tainnow.lib)
timer_lib=$(cat $sysdeps/timer.lib)
util_lib=$(cat $sysdeps/util.lib)
tryflag CFLAGS_AUTO -std=c99
tryflag CFLAGS -fomit-frame-pointer
tryflag CFLAGS_AUTO -fno-exceptions
tryflag CFLAGS_AUTO -fno-unwind-tables
tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
tryflag CFLAGS_AUTO -Wa,--noexecstack
tryflag CFLAGS -fno-stack-protector
tryflag CPPFLAGS_AUTO -Werror=implicit-function-declaration
tryflag CPPFLAGS_AUTO -Werror=implicit-int
tryflag CPPFLAGS_AUTO -Werror=pointer-sign
tryflag CPPFLAGS_AUTO -Werror=pointer-arith
tryflag CFLAGS_AUTO -ffunction-sections
tryflag CFLAGS_AUTO -fdata-sections
tryldflag LDFLAGS_AUTO -Wl,--sort-section=alignment
tryldflag LDFLAGS_AUTO -Wl,--sort-common
CPPFLAGS_AUTO="${CPPFLAGS_AUTO}${addincpath}"
if $evenmorestatic ; then
LDFLAGS_NOSHARED=-static
fi
if $shared ; then
tryldflag LDFLAGS -Wl,--hash-style=both
fi
LDFLAGS_SHARED="${LDFLAGS_SHARED}${addlibdpath}"
if $allstatic ; then
LDFLAGS_NOSHARED="${LDFLAGS_NOSHARED}${addlibspath}"
tryldflag LDFLAGS_NOSHARED -Wl,--gc-sections
else
LDFLAGS_NOSHARED="${LDFLAGS_NOSHARED}${addlibdpath}"
fi
if test -z "$vpaths" ; then
while read dep ; do
base=$(basename $dep) ;
vpaths="$vpaths /usr/lib/$base"
addlibspath="$addlibspath -L/usr/lib/$base"
done < package/deps-build
fi
echo "Creating config.mak..."
cmdline=$(quote "$0")
for i ; do cmdline="$cmdline $(quote "$i")" ; done
exec 3>&1 1>config.mak
cat << EOF
# This file was generated by:
# $cmdline
# Any changes made here will be lost if configure is re-run.
target := $target
package := $package
prefix := $prefix
exec_prefix := $exec_prefix
dynlibdir := $dynlibdir
libexecdir := $libexecdir
bindir := $bindir
libdir := $libdir
includedir := $includedir
sysdeps := $sysdeps
slashpackage := $slashpackage
sproot := $sproot
version := $version
home := $home
exthome := $exthome
SPAWN_LIB := ${spawn_lib}
SOCKET_LIB := ${socket_lib}
SYSCLOCK_LIB := ${sysclock_lib}
TAINNOW_LIB := ${tainnow_lib}
TIMER_LIB := ${timer_lib}
UTIL_LIB := ${util_lib}
CC := ${CC_AUTO##${cross}}
CPPFLAGS_AUTO := $CPPFLAGS_AUTO
CPPFLAGS := $CPPFLAGS $CPPFLAGS_POST
CFLAGS_AUTO := $CFLAGS_AUTO
CFLAGS := $CFLAGS $CFLAGS_POST
LDFLAGS_AUTO := $LDFLAGS_AUTO
LDFLAGS := $LDFLAGS $LDFLAGS_POST
LDFLAGS_SHARED := $LDFLAGS_SHARED
LDFLAGS_NOSHARED := $LDFLAGS_NOSHARED
CROSS_COMPILE := $cross
vpath lib%.a$vpaths
vpath lib%.so$vpathd
EOF
if $allstatic ; then
echo ".LIBPATTERNS := lib%.a"
echo "DO_ALLSTATIC := 1"
else
echo ".LIBPATTERNS := lib%.so"
fi
if $static ; then
echo "DO_STATIC := 1"
else
echo "DO_STATIC :="
fi
if $shared ; then
echo "DO_SHARED := 1"
else
echo "DO_SHARED :="
fi
if $allpic ; then
echo "STATIC_LIBS_ARE_PIC := 1"
else
echo "STATIC_LIBS_ARE_PIC :="
fi
exec 1>&3 3>&-
echo " ... done."
echo "Creating src/include/${package}/config.h..."
mkdir -p -m 0755 src/include/${package}
exec 3>&1 1> src/include/${package}/config.h
cat <<EOF
/* ISC license. */
/* Generated by: $cmdline */
#ifndef ${package_macro_name}_CONFIG_H
#define ${package_macro_name}_CONFIG_H
#define ${package_macro_name}_VERSION "$version"
EOF
if $slashpackage ; then
echo "#define ${package_macro_name}_BINPREFIX \"$bindir/\""
echo "#define ${package_macro_name}_EXTBINPREFIX \"$extbinprefix/\""
elif $abspath ; then
echo "#define ${package_macro_name}_BINPREFIX \"$bindir/\""
echo "#define ${package_macro_name}_EXTBINPREFIX \"$bindir/\""
else
echo "#define ${package_macro_name}_BINPREFIX \"\""
echo "#define ${package_macro_name}_EXTBINPREFIX \"\""
fi
echo "#define ${package_macro_name}_LIBEXECPREFIX \"$libexecdir/\""
echo
echo "#endif"
exec 1>&3 3>&-
echo " ... done."
/package/prog/skalibs
/package/admin/execline
/package/prog/oblibs
#
# This file has been generated by tools/gen-deps.sh
#
src/66-tools/66-envfile.o src/66-tools/66-envfile.lo: src/66-tools/66-envfile.c
src/66-tools/66-getenv.o src/66-tools/66-getenv.lo: src/66-tools/66-getenv.c
src/66-tools/66-gnwenv.o src/66-tools/66-gnwenv.lo: src/66-tools/66-gnwenv.c
src/66-tools/66-which.o src/66-tools/66-which.lo: src/66-tools/66-which.c
src/66-tools/66-writenv.o src/66-tools/66-writenv.lo: src/66-tools/66-writenv.c
src/66-tools/execl-cmdline.o src/66-tools/execl-cmdline.lo: src/66-tools/execl-cmdline.c
src/66-tools/execl-envfile.o src/66-tools/execl-envfile.lo: src/66-tools/execl-envfile.c
src/66-tools/execl-subuidgid.o src/66-tools/execl-subuidgid.lo: src/66-tools/execl-subuidgid.c
66-envfile: EXTRA_LIBS :=
66-envfile: src/66-tools/66-envfile.o -lskarnet
66-getenv: EXTRA_LIBS :=
66-getenv: src/66-tools/66-getenv.o -loblibs -lskarnet
66-gnwenv: EXTRA_LIBS :=
66-gnwenv: src/66-tools/66-gnwenv.o -loblibs -lskarnet
66-which: EXTRA_LIBS :=
66-which: src/66-tools/66-which.o -lskarnet -loblibs
66-writenv: EXTRA_LIBS :=
66-writenv: src/66-tools/66-writenv.o -lskarnet
execl-cmdline: EXTRA_LIBS :=
execl-cmdline: src/66-tools/execl-cmdline.o -lexecline -loblibs -lskarnet
execl-envfile: EXTRA_LIBS :=
execl-envfile: src/66-tools/execl-envfile.o ${LIBEXECLINE} -loblibs -lexecline -lskarnet
execl-subuidgid: EXTRA_LIBS :=
execl-subuidgid: src/66-tools/execl-subuidgid.o -loblibs -lexecline -lskarnet ${LIBEXECLINE}
package=66-tools
version=0.0.1.0
category=prog
package_macro_name=66-TOOLS
66-envfile 0755
66-getenv 0755
66-gnwenv 0755
66-which 0755
66-writenv 0755
execl-cmdline 0755
execl-envfile 0755
execl-subuidgid 0755
BIN_TARGETS := \
66-envfile \
66-getenv \
66-gnwenv \
66-which \
66-writenv \
execl-cmdline \
execl-envfile \
execl-subuidgid
LIBEXEC_TARGETS :=
LIB_DEFS := 66-TOOLS=66-tools
/*
* 66-envfile.c
*
* Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <skalibs/djbunix.h>
#include <skalibs/strerr2.h>
int main (int argc, char const *const *argv, char const *const *envp)
{
PROG = "66-envfile" ;
int i = 0 ;
strerr_warnw1x("the 66-envfile is obsolescent, please use execl-envfile instead") ;
argv++ ;
char const *cmd[argc] ;
cmd[0] = "execl-envfile" ;
for(; i < argc ;i++)
cmd[i+1] = argv[i] ;
cmd[i+1] = 0 ;
pathexec_run(cmd[0],cmd,envp) ;
}
File added
/*
* 66-getenv.c
*
* Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <unistd.h>//read
#include <stdlib.h>//malloc
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <regex.h>
#include <oblibs/sastr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/stralloc.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/types.h>
#define MAXBUF 1024*64*2
static char const *delim = "\n" ;
static char const *pattern = 0 ;
static unsigned int EXACT = 0 ;
#define USAGE "66-getenv [ -h ] [ -x ] [ -d delim ] process"
#define dieusage() strerr_dieusage(100, USAGE)
static inline void info_help (void)
{
static char const *help =
"66-getenv <options> process\n"
"\n"
"options :\n"
" -h: print this help\n"
" -d: specify output delimiter\n"
" -x: match exactly with the process name\n"
;
if (buffer_putsflush(buffer_1, help) < 0)
strerr_diefu1sys(111, "write to stdout") ;
}
static int read_line(stralloc *dst, char const *line)
{
char b[MAXBUF] ;
int fd ;
unsigned int n = 0, m = MAXBUF ;
fd = open(line, O_RDONLY) ;
if (fd == -1) return 0 ;
for(;;)
{
ssize_t r = read(fd,b+n,m-n);
if (r == -1)
{
if (errno == EINTR) continue ;
break ;
}
n += r ;
// buffer is full
if (n == m)
{
--n ;
break ;
}
// end of file
if (r == 0) break ;
}
close(fd) ;
if(n)
{
int i = n ;
// remove trailing zeroes
while (i && b[i-1] == '\0') --i ;
while (i--)
if (b[i] == '\n' || b[i] == '\0') b[i] = ' ' ;
if (b[n-1] == ' ') b[n-1] = '\0' ;
}
b[n] = '\0';
if (!stralloc_cats(dst,b) ||
!stralloc_0(dst)) strerr_diefu1sys(111,"append stralloc") ;
return n ;
}
static regex_t *regex_cmp (void)
{
regex_t *preg = 0 ;
size_t plen = strlen(pattern) ;
char re[plen + 4 + 1] ;
char errbuf[256] ;
int r ;
preg = malloc (sizeof (regex_t)) ;
if (!preg) strerr_diefu1sys(111,"allocate preg") ;
if (EXACT)
{
memcpy(re,"^(",2) ;
memcpy(re + 2,pattern,plen) ;
memcpy(re + 2 + plen,")$",2) ;
re[2 + plen + 2] = 0 ;
}
else
{
memcpy(re,pattern,plen) ;
re[plen] = 0 ;
}
r = regcomp (preg, re, REG_EXTENDED | REG_NOSUB) ;
if (r)
{
regerror (r, preg, errbuf, sizeof(errbuf)) ;
strerr_diefu1x(111,errbuf) ;
}
return preg ;
}
void get_procs ()
{
char *proc = "/proc" ;
char *cmdline = "/cmdline" ;
char *environ = "/environ" ;
size_t proclen = 5, linelen = 8, i = 0, len ;
char myself [PID_FMT] ;
myself[pid_fmt(myself,getpid())] = 0 ;
regex_t *preg ;
preg = regex_cmp() ;
stralloc satmp = STRALLOC_ZERO ;
stralloc saproc = STRALLOC_ZERO ;
if (!sastr_dir_get(&satmp,proc,"",S_IFDIR)) strerr_diefu1sys(111,"get content of /proc") ;
i = 0, len = satmp.len ;
for (;i < len; i += strlen(satmp.s + i) + 1)
{
char *name = satmp.s + i ;
char c = name[0] ;
// keep only pid directories
if ( c >= '0' && c <= '9' )
if (!stralloc_catb(&saproc,name,strlen(name) + 1)) strerr_diefu1sys(111,"append stralloc") ;
}
i = 0, len = saproc.len ;
for (;i < len; i += strlen(saproc.s + i) + 1)
{
satmp.len = 0 ;
int found = 1 ;
char *name = saproc.s + i ;
size_t namelen = strlen(name) ;
if (!strcmp(name,myself)) continue ;
char tmp[proclen + 1 + namelen + linelen + 1] ;
memcpy(tmp,proc,proclen) ;
tmp[proclen] = '/' ;
memcpy(tmp + proclen + 1,name,namelen) ;
memcpy(tmp + proclen + 1 + namelen,cmdline,linelen) ;
tmp[proclen + 1 + namelen + linelen] = 0 ;
if (!read_line(&satmp,tmp)) continue ;
if (regexec (preg, satmp.s, 0, NULL, 0) != 0)
found = 0 ;
satmp.len = 0 ;
memcpy(tmp + proclen + 1 + namelen,environ,linelen) ;
tmp[proclen + 1 + namelen + linelen] = 0 ;
if (!read_line(&satmp,tmp)) continue ;
if (found)
{
size_t j = 0 ;
for(;j < satmp.len; j++)
{
char ch[2] = { satmp.s[j], 0 } ;
if (satmp.s[j] == ' ' || satmp.s[j] == '\0')
{
if (buffer_putsflush(buffer_1, delim) < 0) strerr_diefu1sys(111, "write to stdout") ;
}
else if (buffer_puts(buffer_1, ch) < 0) strerr_diefu1sys(111, "write to stdout") ;
}
break ;
}
}
stralloc_free(&satmp) ;
stralloc_free(&saproc) ;
regfree(preg) ;
}
int main (int argc, char const *const *argv, char const *const *envp)
{
PROG = "66-getenv" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "hxd:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'h' : info_help() ; return 0 ;
case 'x' : EXACT = 1 ; break ;
case 'd' : delim = l.arg ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 1) dieusage() ;
pattern = *argv ;
get_procs() ;
return 0 ;
}
File added
/*
* 66-gnwenv.c
*
* Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <oblibs/string.h>
#include <skalibs/types.h>
#include <skalibs/buffer.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/env.h>
#include <skalibs/djbunix.h>
#define MAX_ENV 4095
static char const *pattern = 0 ;
static unsigned int EXACT = 0 ;
#define USAGE "66-gnwenv [ -h ] [ -x ] [ -m mode ] process dir file"
#define dieusage() strerr_dieusage(100, USAGE)
static inline void info_help (void)
{
static char const *help =
"66-gnwenv <options> process dir file\n"
"\n"
"options :\n"
" -h: print this help\n"
" -m: create dir with given mode\n"
" -x: match exactly with the process name\n"
;
if (buffer_putsflush(buffer_1, help) < 0)
strerr_diefu1sys(111, "write to stdout") ;
}
static void string_env(char *tmp,char const *s,size_t len)
{
int pos = 0 ;
ssize_t r = 0 ;
while ((pos < len) && (r != -1))
{
r = get_len_until(s+pos,'\n') ;
memcpy(tmp+pos,s+pos,r) ;
tmp[pos+r] = 0 ;
pos += ++r ;/**+1 to skip the \n character*/
}
}
int main (int argc, char const *const *argv, char const *const *envp)
{
int r = 0 , pf, rm = 0, m = 0, fd[2], did = 0 ;
ssize_t slen = 0 ;
unsigned int mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ;
char const *dir = 0 , *file = 0 ;
char const *newargv[6+1] ;
char const *newread[6+1] ;
char md[UINT32_FMT] ;
char buf[MAX_ENV+1] ;
char tmp[MAX_ENV+1] ;
PROG = "66-gnwenv" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "hxm:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'h' : info_help() ; return 0 ;
case 'x' : EXACT = 1 ; break ;
case 'm' : if (!uint0_oscan(l.arg, &mode)) dieusage() ; did = 1 ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 3) dieusage() ;
pattern = argv[0] ;
dir = argv[1] ;
if (dir[0] != '/') strerr_dief2x(111,dir," must be an absolute path") ;
file = argv[2] ;
newread[rm++] = "66-getenv" ;
if (EXACT)
newread[rm++] = "-x" ;
newread[rm++] = pattern ;
newread[rm++] = dir ;
newread[rm++] = file ;
newread[rm++] = 0 ;
if (pipe(fd) < 0) strerr_diefu1sys(111,"pipe") ;
pf = fork() ;
if (pf < 0) strerr_diefu1sys(111,"fork") ;
if (!pf)
{
dup2(fd[1],1) ;
pathexec(newread) ;
}
else
{
wait(NULL) ;
slen = read(fd[0],buf,MAX_ENV) ;
if (!slen) return 0 ;
buf[slen] = 0 ;
}
r = get_nbline(buf,slen) ;
string_env(tmp,buf,slen) ;
md[uint32_fmt(md,mode)] = 0 ;
newargv[m++] = "66-writenv" ;
if (did)
{
newargv[m++] = "-m" ;
newargv[m++] = md ;
}
newargv[m++] = dir ;
newargv[m++] = file ;
newargv[m++] = 0 ;
char const *v[r + 1] ;
if (!env_make (v, r ,tmp, slen)) strerr_diefu1sys(111,"make environment") ;
v[r] = 0 ;
xpathexec_fromenv (newargv, v, r) ;
}
File added
/*
* 66-which.c
*
* Copyright (c) 2019 Dyne.org Foundation, Amsterdam
* Copyright (c) 2019 Eric Vidal <eric@obarun.org>
*
* Written by:
* - Danilo Spinella <danyspin97@protonmail.com>
* - Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*
* */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <skalibs/genalloc.h>
#include <skalibs/sgetopt.h>
#include <skalibs/buffer.h>
#include <skalibs/bytestr.h>
#include <skalibs/strerr2.h>
#include <skalibs/config.h>
#include <oblibs/error2.h>
#include <oblibs/string.h>
#define USAGE "66-which [ -h ] [ -a | -q ] commands..."
static inline void info_help (void)
{
static char const *help =
"66-which <options> commands...\n"
"\n"
"options :\n"
" -h: print this help\n"
" -a: print all matching executable in PATH\n"
" -q: quiet, do not print anything to stdout\n"
;
if (buffer_putsflush(buffer_1, help) < 0)
strerr_diefu1sys(111, "write to stdout") ;
}
int check_executable(char const* filepath) {
struct stat sb ;
return (stat(filepath, &sb) == 0 && sb.st_mode & S_IXUSR
&& !S_ISDIR(sb.st_mode)) ? 1 : 0 ;
}
int parse_path(genalloc* folders, char* path) {
char* rp = NULL ;
size_t i, len, s ;
int found ;
stralloc filepath = STRALLOC_ZERO ;
while (path) {
s = str_chr(path, ':') ;
if (!stralloc_copyb(&filepath, path, s)
|| !stralloc_0(&filepath))
strerr_diefu1sys(111, "append stralloc with PATH") ;
rp = realpath(filepath.s, NULL);
if (rp != NULL) {
char const** ss = genalloc_s(char const*, folders);
found = 0;
len = genalloc_len(char const*, folders);
for ( i = 0 ; i < len ; i++) {
if (obstr_equal(ss[i], rp)) {
found = 1 ;
break ;
}
}
if (!found) {
if (!genalloc_append(char const*, folders, &rp))
strerr_diefu1sys(111, "append genalloc") ;
} else {
free(rp);
}
}
if (s == strlen(path)) break ;
path += s + 1;
}
stralloc_free(&filepath) ;
return genalloc_len(char const*, folders) ;
}
int handle_string(char const* name, char const* env_path, genalloc_ref paths,
int quiet, int printall) {
size_t len = genalloc_len(char const*, paths) ;
int found = 0 ;
stralloc filepath = STRALLOC_ZERO ;
char const** ss = genalloc_s(char const*, paths) ;
for (size_t i = 0 ; i < len ; i++) {
if (!stralloc_copys(&filepath, ss[i])
|| !stralloc_cats(&filepath, "/")
|| !stralloc_cats(&filepath, name)
|| !stralloc_0(&filepath))
retstralloc(111, "filepath");
if (check_executable(filepath.s)) {
if (!quiet && (buffer_puts(buffer_1small, filepath.s) < 0
|| buffer_put(buffer_1small, "\n", 1) < 0
|| !buffer_flush(buffer_1small)))
strerr_diefu1sys(111, "write to stdout") ;
found = 1;
if (!printall) break ;
}
}
if (found == 0 && !quiet)
strerr_warnw5x("no ",name," in (",env_path,")") ;
stralloc_free(&filepath);
return found == 1 ? 0 : 111;
}
int handle_path(char const* path, int quiet) {
char* rp = realpath(path, NULL) ;
if (rp != NULL && check_executable(rp)) {
if (!quiet && (buffer_puts(buffer_1small, rp) < 0
|| buffer_put(buffer_1small, "\n", 1) < 0
|| !buffer_flush(buffer_1small)))
strerr_diefu1sys(111, "write to stdout") ;
return 0 ;
}
size_t len = strlen(path) ;
char base[len+1] ;
char dir[len+1] ;
if (!basename(base, path))
strerr_diefu1sys(111, "get basename") ;
if (!dirname(dir, path))
strerr_diefu1sys(111, "get dirname") ;
if (!quiet) strerr_warnw5x("no ",base," in (",dir,")") ;
return 111 ;
}
int main (int argc, char const *const *argv)
{
int printall = 0 ;
int quiet = 0 ;
char* path = 0 ;
int ret = 0;
genalloc paths = GENALLOC_ZERO ; // char const *
PROG = "66-which" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "haq", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'h': info_help() ; return 0 ;
case 'a': printall = 1 ; break ;
case 'q': quiet = 1 ; break ;
default : strerr_dieusage(110, USAGE) ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (printall && quiet) strerr_dieusage(110, USAGE) ;
path = getenv("PATH") ;
if (!path) path = SKALIBS_DEFAULTPATH ;
if (argc < 0) strerr_dieusage(110, USAGE) ;
if (!parse_path(&paths, path))
strerr_diefu1sys(111, "PATH is empty or contains non valid values") ;
for ( ; *argv ; argv++) {
if ((*argv)[0] == '/'
|| (*argv)[0] == '~' || ((*argv)[0] == '~' && (*argv)[1] == '/')
|| (*argv)[0] == '.' || ((*argv)[0] == '.' && (*argv)[1] == '/')
|| ((*argv)[0] == '.' || ((*argv)[1] == '.' && (*argv)[2] == '/')))
ret = handle_path(*argv, quiet);
else
ret = handle_string(*argv, path, &paths, quiet, printall);
}
genalloc_free(char const *, &paths) ;
return ret ;
}
File added
/*
* 66-writenv.c
*
* Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>//fsync,close
#include <errno.h>
#include <skalibs/types.h>
#include <skalibs/bytestr.h>
#include <skalibs/buffer.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#define MAX_ENV 4095
#define USAGE "66-writenv [ - h ] [ -m mode ] dir file"
#define dieusage() strerr_dieusage(100, USAGE)
static inline void info_help (void)
{
static char const *help =
"66-writenv <options> dir file\n"
"\n"
"options :\n"
" -h: print this help\n"
" -m: create dir with given mode\n"
;
if (buffer_putsflush(buffer_1, help) < 0)
strerr_diefu1sys(111, "write to stdout") ;
}
int main (int argc, char const *const *argv, char const *const *envp)
{
unsigned int mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ;
size_t dirlen, filen ;
buffer b ;
int fd ;
char const *dir = 0 , *file = 0 ;
char buf[MAX_ENV+1] ;
PROG = "66-writenv" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "hm:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'h' : info_help() ; return 0 ;
case 'm' : if (!uint0_oscan(l.arg, &mode)) dieusage() ; break ;
default : dieusage() ;
}
}
argc -= l.ind ; argv += l.ind ;
}
if (argc < 2) dieusage() ;
dir = argv[0] ;
if (dir[0] != '/') strerr_dief2x(111,dir," must be an absolute path") ;
file = argv[1] ;
if (mkdir(dir, mode) < 0)
{
struct stat st ;
if (errno != EEXIST) strerr_diefu2sys(111, "mkdir ", dir) ;
if (stat(dir, &st) < 0)
strerr_diefu2sys(111, "stat ", dir) ;
if (!S_ISDIR(st.st_mode))
{
errno = ENOTDIR ;
strerr_diefu2sys(111, "mkdir ", dir) ;
}
}
dirlen = strlen(dir) ;
filen = strlen(file) ;
char fn[dirlen + 1 + filen + 1] ;
memcpy(fn,dir,dirlen) ;
fn[dirlen] = '/' ;
memcpy(fn + dirlen + 1, file ,filen) ;
fn[dirlen + 1 + filen] = 0 ;
fd = open_trunc(fn) ;
if (fd < 0) strerr_diefu2sys(111,"open trunc: ",fn) ;
buffer_init(&b,&buffer_write,fd,buf,MAX_ENV) ;
for (; *envp ; envp++)
{
if ((buffer_put(&b, *envp,strlen(*envp)) < 0) ||
(buffer_put(&b, "\n",1) < 0)) { close(fd) ; strerr_diefu1sys(111,"write buffer") ; }
}
if(!buffer_flush(&b)){ close(fd) ; strerr_diefu1sys(111,"flush") ; }
if (fsync(fd) < 0){ close(fd) ; strerr_diefu1sys(111,"sync") ; }
close(fd) ;
return 0 ;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment