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>
38 #include "got_compat.h"
44 * Split the contents of a file into a list of lines.
47 rcs_splitlines(u_char
*data
, size_t len
)
50 struct rcs_lines
*lines
;
54 lines
= calloc(1, sizeof(*lines
));
57 TAILQ_INIT(&(lines
->l_lines
));
59 lp
= calloc(1, sizeof(*lp
));
64 TAILQ_INSERT_TAIL(&(lines
->l_lines
), lp
, l_list
);
67 for (i
= 0; i
< len
; i
++) {
68 if (*p
== '\n' || (i
== len
- 1)) {
70 lp
= malloc(sizeof(*lp
));
77 lp
->l_lineno
= ++(lines
->l_nblines
);
78 TAILQ_INSERT_TAIL(&(lines
->l_lines
), lp
, l_list
);
88 rcs_freelines(struct rcs_lines
*lines
)
92 while ((lp
= TAILQ_FIRST(&(lines
->l_lines
))) != NULL
) {
93 TAILQ_REMOVE(&(lines
->l_lines
), lp
, l_list
);
101 rcs_patchfile(u_char
*data
, size_t dlen
, u_char
*patch
, size_t plen
,
102 int (*p
)(struct rcs_lines
*, struct rcs_lines
*))
104 const struct got_error
*err
= NULL
;
105 struct rcs_lines
*dlines
, *plines
;
110 dlines
= rcs_splitlines(data
, dlen
);
111 plines
= rcs_splitlines(patch
, plen
);
113 if (p(dlines
, plines
) < 0) {
114 rcs_freelines(dlines
);
115 rcs_freelines(plines
);
119 err
= buf_alloc(&res
, 1024);
122 TAILQ_FOREACH(lp
, &dlines
->l_lines
, l_list
) {
123 if (lp
->l_line
== NULL
)
125 buf_append(&newlen
, res
, lp
->l_line
, lp
->l_len
);
128 rcs_freelines(dlines
);
129 rcs_freelines(plines
);