| 1 |
flameeyes |
1.1 |
# Copyright 1999-2005 Gentoo Foundation |
| 2 |
|
|
# Distributed under the terms of the GNU General Public License v2 |
| 3 |
uberlord |
1.9 |
# $Header: /var/cvsroot/gentoo-x86/eclass/portability.eclass,v 1.8 2006/03/08 20:08:13 flameeyes Exp $ |
| 4 |
flameeyes |
1.1 |
# |
| 5 |
|
|
# Author: Diego Pettenò <flameeyes@gentoo.org> |
| 6 |
|
|
# |
| 7 |
|
|
# This eclass is created to avoid using non-portable GNUisms inside ebuilds |
| 8 |
|
|
# |
| 9 |
|
|
# NB: If you add anything, please comment it! |
| 10 |
|
|
|
| 11 |
|
|
# treecopy orig1 orig2 orig3 .... dest |
| 12 |
|
|
# |
| 13 |
|
|
# mimic cp --parents copy, but working on BSD userland as well |
| 14 |
|
|
treecopy() { |
| 15 |
|
|
dest=${!#} |
| 16 |
|
|
files_count=$# |
| 17 |
|
|
|
| 18 |
|
|
while(( $# > 1 )); do |
| 19 |
|
|
dirstruct=$(dirname "$1") |
| 20 |
|
|
mkdir -p "${dest}/${dirstruct}" |
| 21 |
|
|
cp -pPR "$1" "${dest}/${dirstruct}" |
| 22 |
|
|
|
| 23 |
|
|
shift |
| 24 |
|
|
done |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
# seq min max |
| 28 |
|
|
# |
| 29 |
|
|
# compatibility function that mimes seq command if not available |
| 30 |
|
|
seq() { |
| 31 |
vapier |
1.3 |
local p=$(type -P seq) |
| 32 |
flameeyes |
1.1 |
|
| 33 |
|
|
case $# in |
| 34 |
vapier |
1.3 |
1) min=1 max=$1 step=1 ;; |
| 35 |
|
|
2) min=$1 max=$2 step=1 ;; |
| 36 |
|
|
3) min=$1 max=$3 step=$2 ;; |
| 37 |
|
|
*) die "seq called with wrong number of arguments" ;; |
| 38 |
flameeyes |
1.1 |
esac |
| 39 |
|
|
|
| 40 |
vapier |
1.3 |
if [[ -z ${p} ]] ; then |
| 41 |
flameeyes |
1.1 |
local reps |
| 42 |
|
|
# BSD userland |
| 43 |
|
|
if [[ ${step} != 0 ]]; then |
| 44 |
|
|
reps=$(( ($max-$min) / $step +1 )) |
| 45 |
|
|
else |
| 46 |
|
|
reps=0 |
| 47 |
|
|
fi |
| 48 |
|
|
|
| 49 |
|
|
jot $reps $min $max $step |
| 50 |
|
|
else |
| 51 |
|
|
"${p}" $min $step $max |
| 52 |
|
|
fi |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
# Gets the linker flag to link to dlopen() function |
| 56 |
|
|
dlopen_lib() { |
| 57 |
|
|
if [[ ${ELIBC} != *BSD ]]; then |
| 58 |
|
|
echo "-ldl" |
| 59 |
|
|
fi |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
flameeyes |
1.2 |
# Gets the home directory for the specified user |
| 63 |
|
|
# it's a wrap around egetent as the position of the home directory in the line |
| 64 |
|
|
# varies depending on the os used. |
| 65 |
|
|
# |
| 66 |
|
|
# To use that, inherit eutils, not portability! |
| 67 |
|
|
egethome() { |
| 68 |
|
|
ent=$(egetent passwd $1) |
| 69 |
|
|
|
| 70 |
flameeyes |
1.5 |
case ${CHOST} in |
| 71 |
|
|
*-darwin*|*-freebsd*|*-dragonfly*) |
| 72 |
|
|
# Darwin, OSX, FreeBSD and DragonFly use position 9 to store homedir |
| 73 |
flameeyes |
1.8 |
echo ${ent} | cut -d: -f9 |
| 74 |
flameeyes |
1.6 |
;; |
| 75 |
flameeyes |
1.5 |
*) |
| 76 |
|
|
# Linux, NetBSD and OpenBSD use position 6 instead |
| 77 |
flameeyes |
1.8 |
echo ${ent} | cut -d: -f6 |
| 78 |
flameeyes |
1.6 |
;; |
| 79 |
flameeyes |
1.5 |
esac |
| 80 |
flameeyes |
1.2 |
} |
| 81 |
flameeyes |
1.4 |
|
| 82 |
flameeyes |
1.7 |
# Gets the shell for the specified user |
| 83 |
|
|
# it's a wrap around egetent as the position of the home directory in the line |
| 84 |
|
|
# varies depending on the os used. |
| 85 |
|
|
# |
| 86 |
|
|
# To use that, inherit eutils, not portability! |
| 87 |
|
|
egetshell() { |
| 88 |
|
|
ent=$(egetent passwd "$1") |
| 89 |
|
|
|
| 90 |
|
|
case ${CHOST} in |
| 91 |
|
|
*-darwin*|*-freebsd*|*-dragonfly*) |
| 92 |
|
|
# Darwin, OSX, FreeBSD and DragonFly use position 9 to store homedir |
| 93 |
flameeyes |
1.8 |
echo ${ent} | cut -d: -f10 |
| 94 |
flameeyes |
1.7 |
;; |
| 95 |
|
|
*) |
| 96 |
|
|
# Linux, NetBSD and OpenBSD use position 6 instead |
| 97 |
flameeyes |
1.8 |
echo ${ent} cut -d: -f7 |
| 98 |
flameeyes |
1.7 |
;; |
| 99 |
|
|
esac |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
|
# Returns true if specified user has a shell that precludes logins |
| 103 |
|
|
# on whichever operating system. |
| 104 |
|
|
is-login-disabled() { |
| 105 |
|
|
shell=$(egetshell "$1") |
| 106 |
|
|
|
| 107 |
|
|
case ${shell} in |
| 108 |
|
|
/bin/false|/usr/bin/false|/sbin/nologin|/usr/sbin/nologin) |
| 109 |
|
|
return 0 ;; |
| 110 |
|
|
*) |
| 111 |
|
|
return 1 ;; |
| 112 |
|
|
esac |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
flameeyes |
1.4 |
# Gets the name of the BSD-ish make command (pmake from NetBSD) |
| 116 |
|
|
# |
| 117 |
|
|
# This will return make (provided by system packages) for BSD userlands, |
| 118 |
|
|
# or bsdmake for Darwin userlands and pmake for the rest of userlands, |
| 119 |
|
|
# both of which are provided by sys-devel/pmake package. |
| 120 |
|
|
# |
| 121 |
|
|
# Note: the bsdmake for Darwin userland is with compatibility with MacOSX |
| 122 |
|
|
# default name. |
| 123 |
|
|
get_bmake() { |
| 124 |
|
|
if [[ ${USERLAND} == *BSD ]]; then |
| 125 |
|
|
echo make |
| 126 |
|
|
elif [[ ${USERLAND} == "Darwin" ]]; then |
| 127 |
|
|
echo bsdmake |
| 128 |
|
|
else |
| 129 |
|
|
echo pmake |
| 130 |
|
|
fi |
| 131 |
|
|
} |
| 132 |
uberlord |
1.9 |
|
| 133 |
|
|
# Portable method of getting mount names and points. |
| 134 |
|
|
# Returns as "point node fs" |
| 135 |
|
|
# Remember to convert 040 back to a space. |
| 136 |
|
|
get_mounts() { |
| 137 |
|
|
local point= node= fs= foo= |
| 138 |
|
|
|
| 139 |
|
|
# Linux has /proc/mounts which should always exist |
| 140 |
|
|
if [[ $(uname -s) == "Linux" ]] ; then |
| 141 |
|
|
while read node point fs foo ; do |
| 142 |
|
|
echo "${point} ${node} ${fs}" |
| 143 |
|
|
done < /proc/mounts |
| 144 |
|
|
return |
| 145 |
|
|
fi |
| 146 |
|
|
|
| 147 |
|
|
# OK, pray we have a -p option that outputs mounts in fstab format |
| 148 |
|
|
# using tabs as the seperator. |
| 149 |
|
|
# Then pray that there are no tabs in the either. |
| 150 |
|
|
# Currently only FreeBSD supports this and the other BSDs will |
| 151 |
|
|
# have to be patched. |
| 152 |
|
|
# Athough the BSD's may support /proc, they do NOT put \040 in place |
| 153 |
|
|
# of the spaces and we should not force a /proc either. |
| 154 |
|
|
local IFS=$'\t' |
| 155 |
|
|
LC_ALL=C mount -p | while read node point fs foo ; do |
| 156 |
|
|
echo "${point// /\040} ${node// /\040} ${fs%% *}" |
| 157 |
|
|
done |
| 158 |
|
|
} |