/[baselayout]/trunk/sh/functions.sh
Gentoo

Contents of /trunk/sh/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2689 - (show annotations) (download) (as text)
Sat May 5 21:22:46 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-sh
File size: 3077 byte(s)
Better fix
1 # Copyright 1999-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Please keep this useable by every shell in portage
5
6 RC_GOT_FUNCTIONS="yes"
7
8 eindent() {
9 RC_EINDENT=$((${RC_EINDENT:-0} + 2))
10 [ "${RC_EINDENT}" -gt 40 ] && RC_EINDENT=40
11 export RC_EINDENT
12 }
13
14 eoutdent() {
15 RC_EINDENT=$((${RC_EINDENT:-0} - 2))
16 [ "${RC_EINDENT}" -lt 0 ] && RC_EINDENT=0
17 return 0
18 }
19
20 # void esyslog(char* priority, char* tag, char* message)
21 #
22 # use the system logger to log a message
23 #
24 esyslog() {
25 local pri= tag=
26
27 if [ -x /usr/bin/logger ] ; then
28 pri="$1"
29 tag="$2"
30
31 shift 2
32 [ -z "$*" ] && return 0
33
34 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
35 fi
36
37 return 0
38 }
39
40 # Safer way to list the contents of a directory,
41 # as it do not have the "empty dir bug".
42 #
43 # char *dolisting(param)
44 #
45 # print a list of the directory contents
46 #
47 # NOTE: quote the params if they contain globs.
48 # also, error checking is not that extensive ...
49 #
50 dolisting() {
51 local x= y= mylist= mypath="$*"
52
53 # Here we use file globbing instead of ls to save on forking
54 for x in ${mypath} ; do
55 [ ! -e "${x}" ] && continue
56
57 if [ -L "${x}" -o -f "${x}" ] ; then
58 mylist="${mylist} "${x}
59 elif [ -d "${x}" ] ; then
60 [ "${x%/}" != "${x}" ] && x=${x%/}
61
62 for y in "${x}"/* ; do
63 [ -e "${y}" ] && mylist="${mylist} ${y}"
64 done
65 fi
66 done
67
68 echo "${mylist# *}"
69 }
70
71 # bool is_older_than(reference, files/dirs to check)
72 #
73 # return 0 if any of the files/dirs are newer than
74 # the reference file
75 #
76 # EXAMPLE: if is_older_than a.out *.o ; then ...
77 is_older_than() {
78 local x= ref="$1"
79 shift
80
81 for x in "$@" ; do
82 [ -e "${x}" ] || continue
83 # We need to check the mtime if it's a directory too as the
84 # contents may have changed.
85 [ "${x}" -nt "${ref}" ] && return 0
86 [ -d "${x}" ] && is_older_than "${ref}" "${x}"/* && return 0
87 done
88
89 return 1
90 }
91
92 uniqify() {
93 local result=
94 while [ -n "$1" ] ; do
95 case " ${result} " in
96 *" $1 "*) ;;
97 *) result="${result} $1" ;;
98 esac
99 shift
100 done
101 echo "${result# *}"
102 }
103
104 KV_to_int() {
105 [ -z $1 ] && return 1
106
107 local x=${1%%-*}
108 local KV_MAJOR=${x%%.*}
109 x=${x#*.}
110 local KV_MINOR=${x%%.*}
111 x=${x#*.}
112 local KV_MICRO=${x%%.*}
113 local KV_int=$((${KV_MAJOR} * 65536 + ${KV_MINOR} * 256 + ${KV_MICRO} ))
114
115 # We make version 2.2.0 the minimum version we will handle as
116 # a sanity check ... if its less, we fail ...
117 [ "${KV_int}" -lt 131584 ] && return 1
118
119 echo "${KV_int}"
120 }
121
122 # Setup a basic $PATH. Just add system default to existing.
123 # This should solve both /sbin and /usr/sbin not present when
124 # doing 'su -c foo', or for something like: PATH= rcscript start
125 case "${PATH}" in
126 /lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin) ;;
127 /lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:*) ;;
128 *) export PATH="/lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" ;;
129 esac
130
131 for arg in "$@" ; do
132 case "${arg}" in
133 --nocolor|--nocolour)
134 export RC_NOCOLOR="yes"
135 ;;
136 esac
137 done
138
139 if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then
140 eval $(eval_ecolors)
141 fi
142
143 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20