1 /*
2  * Copyright (c) 2013 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
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
11  * file.
12  *
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.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <sysexits.h>
32 
33 #include <sys/stat.h>
34 #include <sys/fcntl.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 
38 void usage(void);
39 
40 int
main(int argc,char * argv[])41 main(int argc, char * argv[])
42 {
43 	struct stat sb;
44 	char *newcontent = NULL;
45 	size_t newcontentlength = 0;
46 	char *oldcontent = NULL;
47 	int ret;
48 	int dstfd;
49 	const char *dst = NULL;
50 	ssize_t readsize, writesize;
51 	int i;
52 
53 	if (argc < 2) {
54 		usage();
55 	}
56 
57 	dst = argv[1];
58 
59 	for (i = 2; i < argc; i++) {
60 		newcontentlength += strlen(argv[i]) + 1 /* space or newline */;
61 	}
62 	newcontentlength += 1; /* NUL */
63 
64 	newcontent = malloc(newcontentlength);
65 	if (newcontent == NULL) {
66 		err(EX_UNAVAILABLE, "malloc() failed");
67 	}
68 
69 	newcontent[0] = '\0';
70 
71 	for (i = 2; i < argc; i++) {
72 		strlcat(newcontent, argv[i], newcontentlength);
73 		if (i < argc - 1) {
74 			strlcat(newcontent, " ", newcontentlength);
75 		} else {
76 			strlcat(newcontent, "\n", newcontentlength);
77 		}
78 	}
79 
80 	dstfd = open(dst, O_RDWR | O_CREAT | O_APPEND, DEFFILEMODE);
81 	if (dstfd < 0) {
82 		err(EX_NOINPUT, "open(%s)", dst);
83 	}
84 
85 	ret = fstat(dstfd, &sb);
86 	if (ret < 0) {
87 		err(EX_NOINPUT, "fstat(%s)", dst);
88 	}
89 
90 	if (!S_ISREG(sb.st_mode)) {
91 		err(EX_USAGE, "%s is not a regular file", dst);
92 	}
93 
94 	if (sb.st_size != newcontentlength) {
95 		/* obvious new content must be different than old */
96 		goto replace;
97 	}
98 
99 	oldcontent = malloc(newcontentlength);
100 	if (oldcontent == NULL) {
101 		err(EX_UNAVAILABLE, "malloc(%lu) failed", newcontentlength);
102 	}
103 
104 	readsize = read(dstfd, oldcontent, newcontentlength);
105 	if (readsize == -1) {
106 		err(EX_UNAVAILABLE, "read() failed");
107 	} else if (readsize != newcontentlength) {
108 		errx(EX_UNAVAILABLE, "short read of file");
109 	}
110 
111 	if (0 == memcmp(oldcontent, newcontent, newcontentlength)) {
112 		/* binary comparison succeeded, just exit */
113 		free(oldcontent);
114 		ret = close(dstfd);
115 		if (ret < 0) {
116 			err(EX_UNAVAILABLE, "close() failed");
117 		}
118 
119 		exit(0);
120 	}
121 
122 replace:
123 	ret = ftruncate(dstfd, 0);
124 	if (ret < 0) {
125 		err(EX_UNAVAILABLE, "ftruncate() failed");
126 	}
127 
128 	writesize = write(dstfd, newcontent, newcontentlength);
129 	if (writesize == -1) {
130 		err(EX_UNAVAILABLE, "write() failed");
131 	} else if (writesize != newcontentlength) {
132 		errx(EX_UNAVAILABLE, "short write of file");
133 	}
134 
135 	ret = close(dstfd);
136 	if (ret < 0) {
137 		err(EX_NOINPUT, "close(dst)");
138 	}
139 
140 	return 0;
141 }
142 
143 void
usage(void)144 usage(void)
145 {
146 	fprintf(stderr, "Usage: %s <dst> <new> <contents> <...>\n",
147 	    getprogname());
148 	exit(EX_USAGE);
149 }
150