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