comparison toys/patch.c @ 550:b2194045c40e

Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
author Rob Landley <rob@landley.net>
date Mon, 19 Mar 2012 19:19:21 -0500
parents cd70270dbc51
children 1e8b9acdafeb
comparison
equal deleted inserted replaced
549:1f5bd8c93093 550:b2194045c40e
73 73
74 static void do_line(void *data) 74 static void do_line(void *data)
75 { 75 {
76 struct double_list *dlist = (struct double_list *)data; 76 struct double_list *dlist = (struct double_list *)data;
77 77
78 if (TT.state>1 && *dlist->data != TT.state) 78 if (TT.state>1 && *dlist->data != TT.state) {
79 fdprintf(TT.state == 2 ? 2 : TT.fileout, 79 char *s = dlist->data+(TT.state>3 ? 1 : 0);
80 "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); 80 int i = TT.state == 2 ? 2 : TT.fileout;
81 81
82 if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data); 82 xwrite(i, s, strlen(s));
83 xwrite(i, "\n", 1);
84 }
85
86 if (PATCH_DEBUG) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
83 87
84 free(dlist->data); 88 free(dlist->data);
85 free(data); 89 free(data);
86 } 90 }
87 91
94 static void fail_hunk(void) 98 static void fail_hunk(void)
95 { 99 {
96 if (!TT.current_hunk) return; 100 if (!TT.current_hunk) return;
97 TT.current_hunk->prev->next = 0; 101 TT.current_hunk->prev->next = 0;
98 102
99 fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline); 103 fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n",
104 TT.hunknum, TT.oldline, TT.newline);
100 toys.exitval = 1; 105 toys.exitval = 1;
101 106
102 // If we got to this point, we've seeked to the end. Discard changes to 107 // If we got to this point, we've seeked to the end. Discard changes to
103 // this file and advance to next file. 108 // this file and advance to next file.
104 109
126 131
127 // Match EOF if there aren't as many ending context lines as beginning 132 // Match EOF if there aren't as many ending context lines as beginning
128 for (plist = TT.current_hunk; plist; plist = plist->next) { 133 for (plist = TT.current_hunk; plist; plist = plist->next) {
129 if (plist->data[0]==' ') matcheof++; 134 if (plist->data[0]==' ') matcheof++;
130 else matcheof = 0; 135 else matcheof = 0;
131 if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data); 136 if (PATCH_DEBUG) fprintf(stderr, "HUNK:%s\n", plist->data);
132 } 137 }
133 matcheof = matcheof < TT.context; 138 matcheof = matcheof < TT.context;
134 139
135 if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); 140 if (PATCH_DEBUG) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
136 141
137 // Loop through input data searching for this hunk. Match all context 142 // Loop through input data searching for this hunk. Match all context
138 // lines and all lines to be removed until we've found the end of a 143 // lines and all lines to be removed until we've found the end of a
139 // complete hunk. 144 // complete hunk.
140 plist = TT.current_hunk; 145 plist = TT.current_hunk;
153 plist = plist->next; 158 plist = plist->next;
154 } 159 }
155 160
156 // Is this EOF? 161 // Is this EOF?
157 if (!data) { 162 if (!data) {
158 if (PATCH_DEBUG) fdprintf(2, "INEOF\n"); 163 if (PATCH_DEBUG) fprintf(stderr, "INEOF\n");
159 164
160 // Does this hunk need to match EOF? 165 // Does this hunk need to match EOF?
161 if (!plist && matcheof) break; 166 if (!plist && matcheof) break;
162 167
163 if (backwarn) 168 if (backwarn)
164 fdprintf(2,"Possibly reversed hunk %d at %ld\n", 169 fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
165 TT.hunknum, TT.linenum); 170 TT.hunknum, TT.linenum);
166 171
167 // File ended before we found a place for this hunk. 172 // File ended before we found a place for this hunk.
168 fail_hunk(); 173 fail_hunk();
169 goto done; 174 goto done;
170 } else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data); 175 } else if (PATCH_DEBUG) fprintf(stderr, "IN: %s\n", data);
171 check = dlist_add(&buf, data); 176 check = dlist_add(&buf, data);
172 177
173 // Compare this line with next expected line of hunk. 178 // Compare this line with next expected line of hunk.
174 // todo: teach the strcmp() to ignore whitespace. 179 // todo: teach the strcmp() to ignore whitespace.
175 180
184 if (!plist || strcmp(check->data, plist->data+1)) { 189 if (!plist || strcmp(check->data, plist->data+1)) {
185 // Match failed. Write out first line of buffered data and 190 // Match failed. Write out first line of buffered data and
186 // recheck remaining buffered data for a new match. 191 // recheck remaining buffered data for a new match.
187 192
188 if (PATCH_DEBUG) 193 if (PATCH_DEBUG)
189 fdprintf(2, "NOT: %s\n", plist->data); 194 fprintf(stderr, "NOT: %s\n", plist->data);
190 195
191 TT.state = 3; 196 TT.state = 3;
192 check = llist_pop(&buf); 197 check = llist_pop(&buf);
193 check->prev->next = buf; 198 check->prev->next = buf;
194 buf->prev = check->prev; 199 buf->prev = check->prev;
202 break; 207 break;
203 } 208 }
204 check = buf; 209 check = buf;
205 } else { 210 } else {
206 if (PATCH_DEBUG) 211 if (PATCH_DEBUG)
207 fdprintf(2, "MAYBE: %s\n", plist->data); 212 fprintf(stderr, "MAYBE: %s\n", plist->data);
208 // This line matches. Advance plist, detect successful match. 213 // This line matches. Advance plist, detect successful match.
209 plist = plist->next; 214 plist = plist->next;
210 if (!plist && !matcheof) goto out; 215 if (!plist && !matcheof) goto out;
211 check = check->next; 216 check = check->next;
212 if (check == buf) break; 217 if (check == buf) break;
255 // Other versions of patch accept damaged patches, 260 // Other versions of patch accept damaged patches,
256 // so we need to also. 261 // so we need to also.
257 if (strip || !patchlinenum++) { 262 if (strip || !patchlinenum++) {
258 int len = strlen(patchline); 263 int len = strlen(patchline);
259 if (patchline[len-1] == '\r') { 264 if (patchline[len-1] == '\r') {
260 if (!strip) fdprintf(2, "Removing DOS newlines\n"); 265 if (!strip) fprintf(stderr, "Removing DOS newlines\n");
261 strip = 1; 266 strip = 1;
262 patchline[len-1]=0; 267 patchline[len-1]=0;
263 } 268 }
264 } 269 }
265 if (!*patchline) { 270 if (!*patchline) {