1 /* $OpenBSD: rcsutil.c,v 1.46 2017/08/29 16:47:33 otto Exp $ */
3 * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
4 * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6 * Copyright (c) 2006 Ray Lai <ray@openbsd.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/queue.h>
42 * Split the contents of a file into a list of lines.
45 rcs_splitlines(u_char *data, size_t len)
48 struct rcs_lines *lines;
52 lines = calloc(1, sizeof(*lines));
55 TAILQ_INIT(&(lines->l_lines));
57 lp = calloc(1, sizeof(*lp));
62 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
65 for (i = 0; i < len; i++) {
66 if (*p == '\n' || (i == len - 1)) {
68 lp = malloc(sizeof(*lp));
75 lp->l_lineno = ++(lines->l_nblines);
76 TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
86 rcs_freelines(struct rcs_lines *lines)
90 while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
91 TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
99 rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
100 int (*p)(struct rcs_lines *, struct rcs_lines *))
102 struct rcs_lines *dlines, *plines;
107 dlines = rcs_splitlines(data, dlen);
108 plines = rcs_splitlines(patch, plen);
110 if (p(dlines, plines) < 0) {
111 rcs_freelines(dlines);
112 rcs_freelines(plines);
116 res = buf_alloc(1024);
117 TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
118 if (lp->l_line == NULL)
120 buf_append(&newlen, res, lp->l_line, lp->l_len);
123 rcs_freelines(dlines);
124 rcs_freelines(plines);