]>
git.saurik.com Git - apple/xnu.git/blob - SETUP/installfile/installfile.c
8e1c9142fa56eb7a1e5003f369c46a7212b55570
2 * Copyright (c) 2012 Apple, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
33 #include <sys/fcntl.h>
34 #include <sys/param.h>
42 main(int argc
, char * argv
[])
51 const char *src
= NULL
;
52 const char *dst
= NULL
;
53 char dsttmpname
[MAXPATHLEN
];
55 while ((ch
= getopt(argc
, argv
, "cSm:")) != -1) {
59 /* ignored for compatibility */
63 mset
= setmode(optarg
);
65 errx(EX_USAGE
, "Unrecognized mode %s", optarg
);
68 mode
= getmode(mset
, 0);
87 srcfd
= open(src
, O_RDONLY
, 0);
89 err(EX_NOINPUT
, "open(%s)", src
);
92 ret
= fstat(srcfd
, &sb
);
94 err(EX_NOINPUT
, "fstat(%s)", src
);
97 if (!S_ISREG(sb
.st_mode
)) {
98 err(EX_USAGE
, "%s is not a regular file", src
);
101 snprintf(dsttmpname
, sizeof(dsttmpname
), "%s.XXXXXX", dst
);
103 dstfd
= mkstemp(dsttmpname
);
105 err(EX_UNAVAILABLE
, "mkstemp(%s)", dsttmpname
);
108 ret
= fcopyfile(srcfd
, dstfd
, NULL
,
111 err(EX_UNAVAILABLE
, "fcopyfile(%s, %s)", src
, dsttmpname
);
114 ret
= futimes(dstfd
, NULL
);
116 err(EX_UNAVAILABLE
, "futimes(%s)", dsttmpname
);
120 ret
= fchmod(dstfd
, mode
);
122 err(EX_NOINPUT
, "fchmod(%s, %ho)", dsttmpname
, mode
);
126 ret
= rename(dsttmpname
, dst
);
128 err(EX_NOINPUT
, "rename(%s, %s)", dsttmpname
, dst
);
133 err(EX_NOINPUT
, "close(dst)");
138 err(EX_NOINPUT
, "close(src)");
147 fprintf(stderr
, "Usage: %s [-c] [-S] [-m <mode>] <src> <dst>\n",