Apply ASLR to attr
[platform/upstream/attr.git] / install-sh
1 #! /bin/sh
2 #
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # This script emulates bsd install and also recognises
20 # two environment variables, with the following semantics :-
21 #
22 # $DIST_MANIFEST - if set, the name of the file to append manifest
23 #                  information in the following format:
24 #                  File     :  f mode owner group src target
25 #                  Directory:  d mode owner group target
26 #                  Symlink  :  l linkval target
27 #
28 # $DIST_ROOT     - if set, prepend to target
29 #
30 # The sematics of all combinations of these two variables
31 # are as follows:
32 #
33 # $DIST_MANIFEST?  $DIST_ROOT? |   Copy?  Append Manifest?
34 # -----------------------------+--------------------------
35 #       not set       not set  |    yes        no
36 #       not set       set      |    yes        no
37 #       set           not set  |    no         yes
38 #       set           set      |    yes        yes
39 #
40 _usage() {
41     echo "Usage: $prog [-o owner] [-g group] [-m mode] -d directory"
42     echo "or     $prog [-D] [-o owner] [-g group] [-m mode] file directory/file"
43     echo "or     $prog [-o owner] [-g group] [-m mode] file [file ...] directory"
44     echo "or     $prog -S file target  (creates \"target\" symlink)"
45     echo "or     $prog -T lt_arg [-o owner] [-g group] [-m mode] libtool.lai directory"
46     echo ""
47     echo "The \$DIST_MANIFEST and \$DIST_ROOT environment variables affect the"
48     echo "behaviour of this command - see comments in the script."
49     echo "The -D flag is only available for the second usage, and causes"
50     echo "the target directory to be created before installing the file."
51     echo ""
52     exit 1
53 }
54
55 _chown ()
56 {
57     _st=255
58     if [ $# -eq 3 ] ; then
59         chown $1:$2 $3
60         _st=$?
61         if [ $_st -ne 0 ] ; then
62             if [ $REAL_UID != '0' ] ; then
63                 if [ ! -f $DIST_ROOT/.chown.quiet ] ; then
64                     echo '==============================================='
65                     echo Ownership of files under ${DIST_ROOT:-/}
66                     echo cannot be changed
67                     echo '==============================================='
68                     if [ -n "$DIST_ROOT" ] ; then
69                         touch $DIST_ROOT/.chown.quiet
70                     fi
71                 fi
72                _st=0
73             fi
74         fi
75     fi
76
77     return $_st
78 }
79
80
81 _manifest ()
82 {
83     echo $* | sed -e 's/\/\//\//g' >>${DIST_MANIFEST:-/dev/null}
84 }
85
86 prog=`basename $0`
87 HERE=`pwd`
88 dflag=false
89 Dflag=false
90 Sflag=false
91 Tflag=false
92 DIRMODE=755
93 FILEMODE=644
94 OWNER=`id -u`
95 GROUP=`id -g`
96 REAL_UID=$OWNER
97
98 # default is to install and don't append manifest
99 INSTALL=true
100 MANIFEST=:
101
102 : ${DIST_ROOT:=${DESTDIR}}
103
104 [ -n "$DIST_MANIFEST" -a -z "$DIST_ROOT" ] && INSTALL=false
105 [ -n "$DIST_MANIFEST" ] && MANIFEST="_manifest"
106
107 [ $# -eq 0 ] && _usage
108
109 if $INSTALL
110 then
111     CP=cp; LN=ln; MKDIR=mkdir; CHMOD=chmod; CHOWN=_chown
112 else
113     CP=true; LN=true; MKDIR=true; CHMOD=true; CHOWN=true
114 fi
115
116 [ -n "$DIST_ROOT" -a $REAL_UID -ne 0 ] && CHOWN=true
117
118 while getopts "Dcm:d:S:o:g:T:" c $*
119 do
120    case $c in
121    c)
122         ;;
123    g)
124         GROUP=$OPTARG
125         ;;
126    o)
127         OWNER=$OPTARG
128         ;;
129    m)
130         DIRMODE=`expr $OPTARG`
131         FILEMODE=$DIRMODE
132         ;;
133    D)
134         Dflag=true
135         ;;
136    S)
137         symlink=$OPTARG
138         Sflag=true
139         ;;
140    d)
141         dir=$DIST_ROOT/$OPTARG
142         dflag=true
143         ;;
144    T)
145         lt_install=$OPTARG
146         Tflag=true
147         ;;
148    *)
149         _usage
150         ;;
151    esac
152 done
153
154 shift `expr $OPTIND - 1`
155
156 status=0
157 if $dflag
158 then
159     #
160     # first usage
161     #
162     $MKDIR -p $dir
163     status=$?
164     if [ $status -eq 0 ]
165     then
166         $CHMOD $DIRMODE $dir
167         status=$?
168     fi
169     if [ $status -eq 0 ]
170     then
171         $CHOWN $OWNER $GROUP $dir
172         status=$?
173     fi
174     $MANIFEST d $DIRMODE $OWNER $GROUP ${dir#$DIST_ROOT}
175 elif $Sflag
176 then
177     #
178     # fourth usage (symlink)
179     #
180     if [ $# -ne 1 ]
181     then
182         _usage
183     else
184         target=$DIST_ROOT/$1
185     fi
186     $LN -s -f $symlink $target
187     status=$?
188     $MANIFEST l $symlink ${target#$DIST_ROOT}
189 elif $Tflag
190 then
191     #
192     # -T (install libs built by libtool)
193     #
194     if [ $# -ne 2 ]
195     then
196         _usage
197     else
198         libtool_lai=$1
199         # source the libtool variables
200         if [ ! -f $libtool_lai ]
201         then
202                 echo "$prog: Unable to find libtool library file $libtool_lai"
203                 exit 2
204         fi
205         . ./$libtool_lai
206         target=$DIST_ROOT/$2
207     fi
208     case $lt_install in
209     so_dot_version)
210         # Loop until we find libfoo.so.x.y.z, then break out.
211         for solib in $library_names
212         do
213                 # does it have enough parts?  libfoo.so.x.y.z == 5
214                 cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
215                 if [ $cnt -eq 5 ]
216                 then
217                         install_name=$target/$solib
218                         $CP $solib $install_name
219                         status=$?
220                         $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$solib ${install_name#$DIST_ROOT}
221                         break
222                 fi
223         done
224         ;;
225
226     so_*)
227         case $lt_install in
228         so_dot_current)
229                 # ln -s libfoo.so.x.y.z to libfoo.so.x
230                 from_parts=5  # libfoo.so.x.y.z
231                 to_parts=3    # libfoo.so.x
232                 ;;
233         so_base)
234                 # ln -s libfoo.so.x to libfoo.so
235                 from_parts=3  # libfoo.so.x
236                 to_parts=2    # libfoo.so
237                 ;;
238         *)
239                 echo "$prog: -T $lt_install invalid"
240                 exit 2
241                 ;;
242         esac
243
244         # Loop until we find the names, then break out.
245         for solib in $library_names
246         do
247                 # does it have enough parts?
248                 cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
249                 if [ $cnt -eq $from_parts ]
250                 then
251                         from_name=$solib
252                 elif [ $cnt -eq $to_parts ]
253                 then
254                         to_name=$solib
255                 fi
256
257                 if [ -n "$from_name" ] && [ -n "$to_name" ]
258                 then
259                         install_name=$target/$to_name
260                         $LN -s -f $from_name $install_name
261                         status=$?
262                         $MANIFEST l $from_name ${install_name#$DIST_ROOT}
263                         break
264                 fi
265         done
266         ;;
267     old_lib)
268         install_name=$target/$old_library
269         $CP $old_library $install_name
270         status=$?
271         $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$old_library ${install_name#$DIST_ROOT}
272         ;;
273     *)
274         echo "$prog: -T $lt_install invalid"
275         exit 2
276         ;;
277     esac
278
279     case $lt_install in
280     old_lib|so_dot_version)
281         if [ $status -eq 0 ]
282         then
283                 $CHMOD $FILEMODE $install_name
284                 $CHOWN $OWNER $GROUP $install_name
285         fi
286         ;;
287     esac
288
289 else
290     list=""
291     dir=""
292     if [ $# -eq 2 ]
293     then
294         #
295         # second usage
296         #
297         f=$1
298         dir=$DIST_ROOT/$2
299         if $Dflag
300         then
301             mkdir -p `dirname $dir`
302         fi
303         $CP $f $dir
304         status=$?
305         if [ $status -eq 0 ]
306         then
307             if [ -f $dir/$f ]
308             then
309                 $CHMOD $FILEMODE $dir/$f
310                 status=$?
311                 if [ $status -eq 0 ]
312                 then
313                     $CHOWN $OWNER $GROUP $dir/$f
314                     status=$?
315                 fi
316                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
317             else
318                 $CHMOD $FILEMODE $dir
319                 status=$?
320                 if [ $status -eq 0 ]
321                 then
322                     $CHOWN $OWNER $GROUP $dir
323                     status=$?
324                 fi
325                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$dir ${dir#$DIST_ROOT}
326             fi
327         fi
328     else
329         #
330         # third usage
331         #
332         n=1
333         while [ $# -gt 0 ]
334         do
335             if [ $# -gt 1 ]
336             then
337                 list="$list $1"
338             else
339                 dir=$DIST_ROOT/$1
340             fi
341             shift
342         done
343
344         # echo DIR=$dir list=\"$list\"
345         for f in $list
346         do
347             $CP $f $dir
348             status=$?
349             if [ $status -eq 0 ]
350             then
351                 $CHMOD $FILEMODE $dir/$f
352                 status=$?
353                 if [ $status -eq 0 ]
354                 then
355                     $CHOWN $OWNER $GROUP $dir/$f
356                     status=$?
357                 fi
358                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
359             fi
360             [ $status -ne 0 ] && break
361         done
362     fi
363 fi
364
365 exit $status