1df930be7Sderaadt /*
2df930be7Sderaadt * Copyright (c) 1994 Christos Zoulas
3df930be7Sderaadt * All rights reserved.
4df930be7Sderaadt *
5df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
6df930be7Sderaadt * modification, are permitted provided that the following conditions
7df930be7Sderaadt * are met:
8df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
9df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
10df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
11df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
12df930be7Sderaadt * documentation and/or other materials provided with the distribution.
13df930be7Sderaadt * 3. All advertising materials mentioning features or use of this software
14df930be7Sderaadt * must display the following acknowledgement:
15df930be7Sderaadt * This product includes software developed by Christos Zoulas.
16df930be7Sderaadt * 4. The name of the author may not be used to endorse or promote products
17df930be7Sderaadt * derived from this software without specific prior written permission.
18df930be7Sderaadt *
19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20df930be7Sderaadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21df930be7Sderaadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23df930be7Sderaadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df930be7Sderaadt * SUCH DAMAGE.
30df930be7Sderaadt */
31df930be7Sderaadt
32df930be7Sderaadt #include <err.h>
33df930be7Sderaadt #include <stdlib.h>
34df930be7Sderaadt #include <string.h>
35df930be7Sderaadt
36df930be7Sderaadt #include "util.h"
37df930be7Sderaadt
38df930be7Sderaadt /* emalloc():
39df930be7Sderaadt * Error checked malloc
40df930be7Sderaadt */
41df930be7Sderaadt void *
emalloc(size_t s)42f56bb1bdSderaadt emalloc(size_t s)
43df930be7Sderaadt {
44df930be7Sderaadt void *ptr = malloc(s);
45df930be7Sderaadt if (ptr == NULL)
46*cbb3c767Smmcc err(1, NULL);
47df930be7Sderaadt return ptr;
48df930be7Sderaadt }
49df930be7Sderaadt
50df930be7Sderaadt
51df930be7Sderaadt /* erealloc():
52df930be7Sderaadt * Error checked realloc
53df930be7Sderaadt */
54df930be7Sderaadt void *
erealloc(void * p,size_t s)55f56bb1bdSderaadt erealloc(void *p, size_t s)
56df930be7Sderaadt {
57df930be7Sderaadt void *ptr = realloc(p, s);
58df930be7Sderaadt if (ptr == NULL)
59*cbb3c767Smmcc err(1, NULL);
60df930be7Sderaadt return ptr;
61df930be7Sderaadt }
62df930be7Sderaadt
63df930be7Sderaadt
64f9bbbf45Sfgsch /* get_line():
65df930be7Sderaadt * Read a line from a file parsing continuations ending in \
66df930be7Sderaadt * and eliminating trailing newlines.
67df930be7Sderaadt */
68df930be7Sderaadt char *
get_line(FILE * fp,size_t * size)69f9bbbf45Sfgsch get_line(FILE *fp, size_t *size)
70df930be7Sderaadt {
71df930be7Sderaadt size_t s, len = 0;
725d4c798fSderaadt char *buf = NULL, *ptr;
73df930be7Sderaadt int cnt = 1;
74df930be7Sderaadt
75df930be7Sderaadt while (cnt) {
76df930be7Sderaadt if ((ptr = fgetln(fp, &s)) == NULL) {
77df930be7Sderaadt *size = len;
78df930be7Sderaadt return buf;
79df930be7Sderaadt }
80df930be7Sderaadt if (ptr[s - 1] == '\n') /* the newline may be missing at EOF */
81df930be7Sderaadt s--; /* forget newline */
82e89e0eedSderaadt if (s && (cnt = (ptr[s - 1] == '\\'))) /* check for \\ */
83df930be7Sderaadt s--; /* forget \\ */
84df930be7Sderaadt
85df930be7Sderaadt buf = erealloc(buf, len + s + 1);
86df930be7Sderaadt memcpy(buf + len, ptr, s);
87df930be7Sderaadt len += s;
88df930be7Sderaadt buf[len] = '\0';
89df930be7Sderaadt }
90df930be7Sderaadt *size = len;
91df930be7Sderaadt return buf;
92df930be7Sderaadt }
93