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