2 * wiggle - apply rejected patches
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Email: <neilb@suse.de>
28 * Files are read in whole and stored in a
29 * struct stream {char*, len}
32 * loading the file "-" reads from stdin which might require
33 * reading into several buffers
37 #include <sys/types.h>
43 static void join_streams(struct stream list[], int cnt)
45 /* join all the streams in the list (upto body=NULL)
46 * into one by re-allocing list[0].body and copying
52 for (i = 0; i < cnt ; i++)
55 c = realloc(list[0].body, len+1);
62 for (i = 1; i < cnt; i++) {
63 memcpy(c, list[i].body, list[i].len);
70 static struct stream load_regular(int fd)
77 s.body = malloc(s.len+1);
79 if (read(fd, s.body, s.len) != s.len) {
89 static struct stream load_other(int fd)
92 struct stream list[10];
96 list[i].body = malloc(8192);
99 list[i].len = read(fd, list[i].body, 8192);
102 if (list[i].len == 0)
106 join_streams(list, i);
110 join_streams(list, i);
114 struct stream load_file(char *name)
122 if (strcmp(name, "-") == 0)
125 fd = open(name, O_RDONLY);
130 if (fstat(fd, &stb) == 0) {
132 if (S_ISREG(stb.st_mode))
133 s = load_regular(fd);