Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / sup / source / read_line.c
blob9e3777eda3464a2cb08c0681b78511e31f834641
1 /* $NetBSD: read_line.c,v 1.8 2003/10/16 06:26:06 itojun Exp $ */
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #if defined(lint) && defined(__RCSID)
31 __RCSID("$NetBSD: read_line.c,v 1.8 2003/10/16 06:26:06 itojun Exp $");
32 #endif
34 #include <sys/param.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
40 #include "supcdefs.h"
41 #include "supextern.h"
43 /* read_line():
44 * Read a line from a file, parsing continuations ending in \
45 * and eliminating trailing newlines.
46 * Returns a pointer to an internal buffer that is reused upon
47 * next invocation.
49 * NOTE: if HAS_FPARSELN is not defined, delim and flags are currently unused.
51 char *
52 read_line(FILE * fp, size_t * size, size_t * lineno, const char *delim,
53 int flags)
55 static char *buf;
56 #ifdef HAS_FPARSELN
58 if (buf != NULL)
59 free(buf);
60 return (buf = fparseln(fp, size, lineno, delim, flags));
61 #else
62 char *n;
63 #ifndef HAS_FGETLN
64 char sbuf[1024];
65 #endif
66 static int buflen;
68 size_t s, len;
69 char *ptr;
70 int cnt;
72 len = 0;
73 cnt = 1;
74 while (cnt) {
75 if (lineno != NULL)
76 (*lineno)++;
77 #ifdef HAS_FGETLN
78 if ((ptr = fgetln(fp, &s)) == NULL) {
79 if (size != NULL)
80 *size = len;
81 if (len == 0)
82 return NULL;
83 else
84 return buf;
86 #else
87 if ((ptr = fgets(sbuf, sizeof(sbuf) - 1, fp)) == NULL) {
88 if (len == 0)
89 return NULL;
90 else
91 return buf;
92 } else {
93 char *l;
94 if ((l = strchr(sbuf, '\n')) == NULL) {
95 if (sbuf[sizeof(sbuf) - 3] != '\\') {
96 s = sizeof(sbuf);
97 sbuf[sizeof(sbuf) - 2] = '\\';
98 sbuf[sizeof(sbuf) - 1] = '\0';
99 } else
100 s = sizeof(sbuf) - 1;
101 } else {
102 s = l - sbuf;
105 #endif
106 if (ptr[s - 1] == '\n') /* the newline may be missing at EOF */
107 s--; /* forget newline */
108 if (!s)
109 cnt = 0;
110 else {
111 if ((cnt = (ptr[s - 1] == '\\')) != 0)
112 s--; /* forget \\ */
115 if (len + s + 1 > buflen) {
116 n = realloc(buf, len + s + 1);
117 if (n == NULL)
118 err(1, "can't realloc");
119 buf = n;
120 buflen = len + s + 1;
122 memcpy(buf + len, ptr, s);
123 len += s;
124 buf[len] = '\0';
126 if (size != NULL)
127 *size = len;
128 return buf;
129 #endif /* HAS_FPARSELN */