]>
git.saurik.com Git - apple/xnu.git/blob - SETUP/installfile/installfile.c
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>
41 int main(int argc
, char * argv
[])
50 const char *src
= NULL
;
51 const char *dst
= NULL
;
52 char dsttmpname
[MAXPATHLEN
];
54 while ((ch
= getopt(argc
, argv
, "cSm:")) != -1) {
58 /* ignored for compatibility */
62 mset
= setmode(optarg
);
64 errx(EX_USAGE
, "Unrecognized mode %s", optarg
);
66 mode
= getmode(mset
, 0);
85 srcfd
= open(src
, O_RDONLY
| O_SYMLINK
, 0);
87 err(EX_NOINPUT
, "open(%s)", src
);
89 ret
= fstat(srcfd
, &sb
);
91 err(EX_NOINPUT
, "fstat(%s)", src
);
93 if (!S_ISREG(sb
.st_mode
))
94 err(EX_USAGE
, "%s is not a regular file", src
);
96 snprintf(dsttmpname
, sizeof(dsttmpname
), "%s.XXXXXX", dst
);
98 dstfd
= mkstemp(dsttmpname
);
100 err(EX_UNAVAILABLE
, "mkstemp(%s)", dsttmpname
);
102 ret
= fcopyfile(srcfd
, dstfd
, NULL
,
105 err(EX_UNAVAILABLE
, "fcopyfile(%s, %s)", src
, dsttmpname
);
107 ret
= futimes(dstfd
, NULL
);
109 err(EX_UNAVAILABLE
, "futimes(%s)", dsttmpname
);
112 ret
= fchmod(dstfd
, mode
);
114 err(EX_NOINPUT
, "fchmod(%s, %ho)", dsttmpname
, mode
);
117 ret
= rename(dsttmpname
, dst
);
119 err(EX_NOINPUT
, "rename(%s, %s)", dsttmpname
, dst
);
123 err(EX_NOINPUT
, "close(dst)");
127 err(EX_NOINPUT
, "close(src)");
134 fprintf(stderr
, "Usage: %s [-c] [-S] [-m <mode>] <src> <dst>\n",