summaryrefslogtreecommitdiff
path: root/usr.bin/sudo/install-sh
blob: 577779b17da3c7c24fc9887a06b956e16424f199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#! /bin/sh

## (From INN-1.4, written by Rich Salz)
##  $Revision: 1.3 $
##  A script to install files and directories.

PROGNAME=`basename $0`

##  Paths to programs.  CHOWN, STRIP and WHOAMI are checked below.
CHOWN=chown
CHGRP=chgrp
CHMOD=chmod
CP=cp
LN=ln
MKDIR=mkdir
MV=mv
RM=rm
STRIP=strip
WHOAMI=whoami

##  Some systems don't support -x, so we have to use -f.
if [ ${CHOWN} = chown ] ; then
    if [ -f /sbin/chown ] ; then
	CHOWN=/sbin/chown
    elif [ -f /etc/chown ] ; then
	CHOWN=/etc/chown
    elif [ -f /usr/sbin/chown ] ; then
	CHOWN=/usr/sbin/chown
    elif [ -f /usr/etc/chown ] ; then
	CHOWN=/usr/etc/chown
    fi
fi

if [ ${WHOAMI} = whoami ] ; then
    if [ -f /usr/ucb/whoami ] ; then
	WHOAMI=/usr/ucb/whoami
    elif [ -f /usr/bin/whoami ] ; then
	WHOAMI=/usr/bin/whoami
    fi
fi

if [ ${STRIP} = strip ] ; then
    if [ -f /usr/ccs/bin/strip ] ; then
	STRIP=/usr/ccs/bin/strip
    elif [ -f /usr/bin/strip ] ; then
	STRIP=/usr/bin/strip
    fi
fi

##  Defaults.
CHOWNIT=false
CHGROUPIT=false
CHMODIT=false
STRIPIT=false
BACKIT=false
TOUCHIT=true
SAVESRC=false
ROOT=unknown

##  Process JCL.
MORETODO=true
while ${MORETODO} ; do
    case X"$1" in
    X-b)
	BACKIT=true
	BACKUP="$2"
	shift
	;;
    X-b*)
	BACKIT=true
	BACKUP=`expr "$1" : '-b\(.*\)'`
	;;
    X-c)
	SAVESRC=true
	;;
    X-g)
	GROUP="$2"
	CHGROUPIT=true
	shift
	;;
    X-g*)
	GROUP=`expr "$1" : '-g\(.*\)'`
	CHGROUPIT=true
	;;
    X-G)
	case ${ROOT} in
	unknown)
	    case `${WHOAMI}` in
	    root)
		ROOT=true
		;;
	    *)
		ROOT=false
		;;
	    esac
	    ;;
	esac
	GROUP="$2"
	shift
	${ROOT} && CHGROUPIT=true
	;;
    X-G*)
	case ${ROOT} in
	unknown)
	    case `${WHOAMI}` in
	    root)
		ROOT=true
		;;
	    *)
		ROOT=false
		;;
	    esac
	    ;;
	esac
	if ${ROOT} ; then
	    GROUP=`expr "$1" : '-g\(.*\)'`
	    CHGROUPIT=true
	fi
	;;
    X-m)
	MODE="$2"
	CHMODIT=true
	shift
	;;
    X-m*)
	MODE=`expr "$1" : '-m\(.*\)'`
	CHMODIT=true
	;;
    X-n)
	TOUCHIT=false
	;;
    X-o)
	OWNER="$2"
	CHOWNIT=true
	shift
	;;
    X-o*)
	OWNER=`expr "$1" : '-o\(.*\)'`
	CHOWNIT=true
	;;
    X-O)
	case ${ROOT} in
	unknown)
	    case `${WHOAMI}` in
	    root)
		ROOT=true
		;;
	    *)
		ROOT=false
		;;
	    esac
	    ;;
	esac
	OWNER="$2"
	shift
	${ROOT} && CHOWNIT=true
	;;
    X-O*)
	case ${ROOT} in
	unknown)
	    case `${WHOAMI}` in
	    root)
		ROOT=true
		;;
	    *)
		ROOT=false
		;;
	    esac
	    ;;
	esac
	if ${ROOT} ; then
	    OWNER=`expr "$1" : '-o\(.*\)'`
	    CHOWNIT=true
	fi
	;;
    X-s)
	STRIPIT=true
	;;
    X--)
	shift
	MORETODO=false
	;;
    X-*)
	echo "${PROGNAME}:  Unknown flag $1" 1>&2
	exit 1
	;;
    *)
	MORETODO=false
	;;
    esac
    ${MORETODO} && shift
done

##  Process arguments.
if [ $# -ne 2 ] ; then
    echo "Usage:  ${PROGNAME} [flags] source destination"
    exit 1
fi

##  Making a directory?
if [ X"$1" = X. ] ; then
    DEST="$2"
    if [ ! -d "${DEST}" ] ; then
	${MKDIR} "${DEST}" || exit 1
    fi
    if ${CHOWNIT} ; then
	${CHOWN} "${OWNER}" "${DEST}" || exit 1
    fi
    if ${CHGROUPIT} ; then
	${CHGRP} "${GROUP}" "${DEST}" || exit 1
    fi
    if ${CHMODIT} ; then
	umask 0
	${CHMOD} "${MODE}"  "${DEST}" || exit 1
    fi
    exit 0
fi

##  Get the destination and a temp file in the destination diretory.
if [ -d "$2" ] ; then
    DEST="$2/$1"
    TEMP="$2/$$.tmp"
else
    DEST="$2"
    TEMP="`expr "$2" : '\(.*\)/.*'`/$$.tmp"
fi

##  If not given the same name, we must try to copy.
if [ X"$1" != X"$2" -o $SAVESRC ] ; then
    if cmp -s "$1" "${DEST}" ; then
	##  Files are same; touch or not.
	${TOUCHIT} && touch "${DEST}"
    else
	##  If destination exists and we wish to backup, link to backup.
	if [ -f "${DEST}" ] ; then
	    if ${BACKIT} ; then
		${RM} -f "${DEST}${BACKUP}"
		${LN} "${DEST}" "${DEST}${BACKUP}"
	    fi
	fi
	##  Copy source to the right dir, then move to right spot.
	##  Done in two parts so we can hope for atomicity.
	${RM} -f "${TEMP}" || exit 1
	${CP} "$1" "${TEMP}" || exit 1
	${MV} -f "${TEMP}" "${DEST}" || exit 1
    fi
fi

##  Strip and set the modes.
if ${STRIPIT} ; then
    ${STRIP} "${DEST}" || exit 1
fi
if ${CHOWNIT} ; then
    ${CHOWN} "${OWNER}" "${DEST}" || exit 1
fi
if ${CHGROUPIT} ; then
    ${CHGRP} "${GROUP}" "${DEST}" || exit 1
fi
if ${CHMODIT} ; then
    umask 0
    ${CHMOD} "${MODE}"  "${DEST}" || exit 1
fi
exit 0