xref: /xnu-11215.61.5/SETUP/replacecontents/replacecontents.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  * Copyright (c) 2013 Apple Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions  *
6*4f1223e8SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*4f1223e8SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*4f1223e8SApple OSS Distributions  * file.
12*4f1223e8SApple OSS Distributions  *
13*4f1223e8SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*4f1223e8SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*4f1223e8SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*4f1223e8SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*4f1223e8SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*4f1223e8SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*4f1223e8SApple OSS Distributions  * limitations under the License.
20*4f1223e8SApple OSS Distributions  *
21*4f1223e8SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*4f1223e8SApple OSS Distributions  */
23*4f1223e8SApple OSS Distributions 
24*4f1223e8SApple OSS Distributions #include <stdio.h>
25*4f1223e8SApple OSS Distributions #include <stdlib.h>
26*4f1223e8SApple OSS Distributions #include <unistd.h>
27*4f1223e8SApple OSS Distributions #include <string.h>
28*4f1223e8SApple OSS Distributions #include <stdbool.h>
29*4f1223e8SApple OSS Distributions #include <errno.h>
30*4f1223e8SApple OSS Distributions #include <err.h>
31*4f1223e8SApple OSS Distributions #include <sysexits.h>
32*4f1223e8SApple OSS Distributions 
33*4f1223e8SApple OSS Distributions #include <sys/stat.h>
34*4f1223e8SApple OSS Distributions #include <sys/fcntl.h>
35*4f1223e8SApple OSS Distributions #include <sys/param.h>
36*4f1223e8SApple OSS Distributions #include <sys/time.h>
37*4f1223e8SApple OSS Distributions 
38*4f1223e8SApple OSS Distributions void usage(void);
39*4f1223e8SApple OSS Distributions 
40*4f1223e8SApple OSS Distributions int
main(int argc,char * argv[])41*4f1223e8SApple OSS Distributions main(int argc, char * argv[])
42*4f1223e8SApple OSS Distributions {
43*4f1223e8SApple OSS Distributions 	struct stat sb;
44*4f1223e8SApple OSS Distributions 	char *newcontent = NULL;
45*4f1223e8SApple OSS Distributions 	size_t newcontentlength = 0;
46*4f1223e8SApple OSS Distributions 	char *oldcontent = NULL;
47*4f1223e8SApple OSS Distributions 	int ret;
48*4f1223e8SApple OSS Distributions 	int dstfd;
49*4f1223e8SApple OSS Distributions 	const char *dst = NULL;
50*4f1223e8SApple OSS Distributions 	ssize_t readsize, writesize;
51*4f1223e8SApple OSS Distributions 	int i;
52*4f1223e8SApple OSS Distributions 
53*4f1223e8SApple OSS Distributions 	if (argc < 2) {
54*4f1223e8SApple OSS Distributions 		usage();
55*4f1223e8SApple OSS Distributions 	}
56*4f1223e8SApple OSS Distributions 
57*4f1223e8SApple OSS Distributions 	dst = argv[1];
58*4f1223e8SApple OSS Distributions 
59*4f1223e8SApple OSS Distributions 	for (i = 2; i < argc; i++) {
60*4f1223e8SApple OSS Distributions 		newcontentlength += strlen(argv[i]) + 1 /* space or newline */;
61*4f1223e8SApple OSS Distributions 	}
62*4f1223e8SApple OSS Distributions 	newcontentlength += 1; /* NUL */
63*4f1223e8SApple OSS Distributions 
64*4f1223e8SApple OSS Distributions 	newcontent = malloc(newcontentlength);
65*4f1223e8SApple OSS Distributions 	if (newcontent == NULL) {
66*4f1223e8SApple OSS Distributions 		err(EX_UNAVAILABLE, "malloc() failed");
67*4f1223e8SApple OSS Distributions 	}
68*4f1223e8SApple OSS Distributions 
69*4f1223e8SApple OSS Distributions 	newcontent[0] = '\0';
70*4f1223e8SApple OSS Distributions 
71*4f1223e8SApple OSS Distributions 	for (i = 2; i < argc; i++) {
72*4f1223e8SApple OSS Distributions 		strlcat(newcontent, argv[i], newcontentlength);
73*4f1223e8SApple OSS Distributions 		if (i < argc - 1) {
74*4f1223e8SApple OSS Distributions 			strlcat(newcontent, " ", newcontentlength);
75*4f1223e8SApple OSS Distributions 		} else {
76*4f1223e8SApple OSS Distributions 			strlcat(newcontent, "\n", newcontentlength);
77*4f1223e8SApple OSS Distributions 		}
78*4f1223e8SApple OSS Distributions 	}
79*4f1223e8SApple OSS Distributions 
80*4f1223e8SApple OSS Distributions 	dstfd = open(dst, O_RDWR | O_CREAT | O_APPEND, DEFFILEMODE);
81*4f1223e8SApple OSS Distributions 	if (dstfd < 0) {
82*4f1223e8SApple OSS Distributions 		err(EX_NOINPUT, "open(%s)", dst);
83*4f1223e8SApple OSS Distributions 	}
84*4f1223e8SApple OSS Distributions 
85*4f1223e8SApple OSS Distributions 	ret = fstat(dstfd, &sb);
86*4f1223e8SApple OSS Distributions 	if (ret < 0) {
87*4f1223e8SApple OSS Distributions 		err(EX_NOINPUT, "fstat(%s)", dst);
88*4f1223e8SApple OSS Distributions 	}
89*4f1223e8SApple OSS Distributions 
90*4f1223e8SApple OSS Distributions 	if (!S_ISREG(sb.st_mode)) {
91*4f1223e8SApple OSS Distributions 		err(EX_USAGE, "%s is not a regular file", dst);
92*4f1223e8SApple OSS Distributions 	}
93*4f1223e8SApple OSS Distributions 
94*4f1223e8SApple OSS Distributions 	if (sb.st_size != newcontentlength) {
95*4f1223e8SApple OSS Distributions 		/* obvious new content must be different than old */
96*4f1223e8SApple OSS Distributions 		goto replace;
97*4f1223e8SApple OSS Distributions 	}
98*4f1223e8SApple OSS Distributions 
99*4f1223e8SApple OSS Distributions 	oldcontent = malloc(newcontentlength);
100*4f1223e8SApple OSS Distributions 	if (oldcontent == NULL) {
101*4f1223e8SApple OSS Distributions 		err(EX_UNAVAILABLE, "malloc(%lu) failed", newcontentlength);
102*4f1223e8SApple OSS Distributions 	}
103*4f1223e8SApple OSS Distributions 
104*4f1223e8SApple OSS Distributions 	readsize = read(dstfd, oldcontent, newcontentlength);
105*4f1223e8SApple OSS Distributions 	if (readsize == -1) {
106*4f1223e8SApple OSS Distributions 		err(EX_UNAVAILABLE, "read() failed");
107*4f1223e8SApple OSS Distributions 	} else if (readsize != newcontentlength) {
108*4f1223e8SApple OSS Distributions 		errx(EX_UNAVAILABLE, "short read of file");
109*4f1223e8SApple OSS Distributions 	}
110*4f1223e8SApple OSS Distributions 
111*4f1223e8SApple OSS Distributions 	if (0 == memcmp(oldcontent, newcontent, newcontentlength)) {
112*4f1223e8SApple OSS Distributions 		/* binary comparison succeeded, just exit */
113*4f1223e8SApple OSS Distributions 		free(oldcontent);
114*4f1223e8SApple OSS Distributions 		ret = close(dstfd);
115*4f1223e8SApple OSS Distributions 		if (ret < 0) {
116*4f1223e8SApple OSS Distributions 			err(EX_UNAVAILABLE, "close() failed");
117*4f1223e8SApple OSS Distributions 		}
118*4f1223e8SApple OSS Distributions 
119*4f1223e8SApple OSS Distributions 		exit(0);
120*4f1223e8SApple OSS Distributions 	}
121*4f1223e8SApple OSS Distributions 
122*4f1223e8SApple OSS Distributions replace:
123*4f1223e8SApple OSS Distributions 	ret = ftruncate(dstfd, 0);
124*4f1223e8SApple OSS Distributions 	if (ret < 0) {
125*4f1223e8SApple OSS Distributions 		err(EX_UNAVAILABLE, "ftruncate() failed");
126*4f1223e8SApple OSS Distributions 	}
127*4f1223e8SApple OSS Distributions 
128*4f1223e8SApple OSS Distributions 	writesize = write(dstfd, newcontent, newcontentlength);
129*4f1223e8SApple OSS Distributions 	if (writesize == -1) {
130*4f1223e8SApple OSS Distributions 		err(EX_UNAVAILABLE, "write() failed");
131*4f1223e8SApple OSS Distributions 	} else if (writesize != newcontentlength) {
132*4f1223e8SApple OSS Distributions 		errx(EX_UNAVAILABLE, "short write of file");
133*4f1223e8SApple OSS Distributions 	}
134*4f1223e8SApple OSS Distributions 
135*4f1223e8SApple OSS Distributions 	ret = close(dstfd);
136*4f1223e8SApple OSS Distributions 	if (ret < 0) {
137*4f1223e8SApple OSS Distributions 		err(EX_NOINPUT, "close(dst)");
138*4f1223e8SApple OSS Distributions 	}
139*4f1223e8SApple OSS Distributions 
140*4f1223e8SApple OSS Distributions 	return 0;
141*4f1223e8SApple OSS Distributions }
142*4f1223e8SApple OSS Distributions 
143*4f1223e8SApple OSS Distributions void
usage(void)144*4f1223e8SApple OSS Distributions usage(void)
145*4f1223e8SApple OSS Distributions {
146*4f1223e8SApple OSS Distributions 	fprintf(stderr, "Usage: %s <dst> <new> <contents> <...>\n",
147*4f1223e8SApple OSS Distributions 	    getprogname());
148*4f1223e8SApple OSS Distributions 	exit(EX_USAGE);
149*4f1223e8SApple OSS Distributions }
150