Consistently use "superuser" instead of "super user"
[pgsql.git] / src / common / pg_get_line.c
bloba80d196156d6fc30dab234c682af4be5db15059c
1 /*-------------------------------------------------------------------------
3 * pg_get_line.c
4 * fgets() with an expansible result buffer
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/common/pg_get_line.c
13 *-------------------------------------------------------------------------
15 #ifndef FRONTEND
16 #include "postgres.h"
17 #else
18 #include "postgres_fe.h"
19 #endif
21 #include "common/string.h"
22 #include "lib/stringinfo.h"
26 * pg_get_line()
28 * This is meant to be equivalent to fgets(), except that instead of
29 * reading into a caller-supplied, fixed-size buffer, it reads into
30 * a palloc'd (in frontend, really malloc'd) string, which is resized
31 * as needed to handle indefinitely long input lines. The caller is
32 * responsible for pfree'ing the result string when appropriate.
34 * As with fgets(), returns NULL if there is a read error or if no
35 * characters are available before EOF. The caller can distinguish
36 * these cases by checking ferror(stream).
38 * Since this is meant to be equivalent to fgets(), the trailing newline
39 * (if any) is not stripped. Callers may wish to apply pg_strip_crlf().
41 * Note that while I/O errors are reflected back to the caller to be
42 * dealt with, an OOM condition for the palloc'd buffer will not be;
43 * there'll be an ereport(ERROR) or exit(1) inside stringinfo.c.
45 * Also note that the palloc'd buffer is usually a lot longer than
46 * strictly necessary, so it may be inadvisable to use this function
47 * to collect lots of long-lived data. A less memory-hungry option
48 * is to use pg_get_line_buf() or pg_get_line_append() in a loop,
49 * then pstrdup() each line.
51 char *
52 pg_get_line(FILE *stream)
54 StringInfoData buf;
56 initStringInfo(&buf);
58 if (!pg_get_line_append(stream, &buf))
60 /* ensure that free() doesn't mess up errno */
61 int save_errno = errno;
63 pfree(buf.data);
64 errno = save_errno;
65 return NULL;
68 return buf.data;
72 * pg_get_line_buf()
74 * This has similar behavior to pg_get_line(), and thence to fgets(),
75 * except that the collected data is returned in a caller-supplied
76 * StringInfo buffer. This is a convenient API for code that just
77 * wants to read and process one line at a time, without any artificial
78 * limit on line length.
80 * Returns true if a line was successfully collected (including the
81 * case of a non-newline-terminated line at EOF). Returns false if
82 * there was an I/O error or no data was available before EOF.
83 * (Check ferror(stream) to distinguish these cases.)
85 * In the false-result case, buf is reset to empty.
87 bool
88 pg_get_line_buf(FILE *stream, StringInfo buf)
90 /* We just need to drop any data from the previous call */
91 resetStringInfo(buf);
92 return pg_get_line_append(stream, buf);
96 * pg_get_line_append()
98 * This has similar behavior to pg_get_line(), and thence to fgets(),
99 * except that the collected data is appended to whatever is in *buf.
100 * This is useful in preference to pg_get_line_buf() if the caller wants
101 * to merge some lines together, e.g. to implement backslash continuation.
103 * Returns true if a line was successfully collected (including the
104 * case of a non-newline-terminated line at EOF). Returns false if
105 * there was an I/O error or no data was available before EOF.
106 * (Check ferror(stream) to distinguish these cases.)
108 * In the false-result case, the contents of *buf are logically unmodified,
109 * though it's possible that the buffer has been resized.
111 bool
112 pg_get_line_append(FILE *stream, StringInfo buf)
114 int orig_len = buf->len;
116 /* Read some data, appending it to whatever we already have */
117 while (fgets(buf->data + buf->len, buf->maxlen - buf->len, stream) != NULL)
119 buf->len += strlen(buf->data + buf->len);
121 /* Done if we have collected a newline */
122 if (buf->len > orig_len && buf->data[buf->len - 1] == '\n')
123 return true;
125 /* Make some more room in the buffer, and loop to read more data */
126 enlargeStringInfo(buf, 128);
129 /* Check for I/O errors and EOF */
130 if (ferror(stream) || buf->len == orig_len)
132 /* Discard any data we collected before detecting error */
133 buf->len = orig_len;
134 buf->data[orig_len] = '\0';
135 return false;
138 /* No newline at EOF, but we did collect some data */
139 return true;