2 * Copyright (c) 2012 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: openpam_readlinev.c 648 2013-03-05 17:54:27Z des $
40 #include <security/pam_appl.h>
42 #include "openpam_impl.h"
44 #define MIN_WORDV_SIZE 32
49 * Read a line from a file and split it into words.
53 openpam_readlinev(FILE *f, int *lineno, int *lenp)
55 char *word, **wordv, **tmp;
56 size_t wordlen, wordvsize;
57 int ch, serrno, wordvlen;
59 wordvsize = MIN_WORDV_SIZE;
61 if ((wordv = malloc(wordvsize * sizeof *wordv)) == NULL) {
62 openpam_log(PAM_LOG_ERROR, "malloc(): %m");
66 wordv[wordvlen] = NULL;
67 while ((word = openpam_readword(f, lineno, &wordlen)) != NULL) {
68 if ((unsigned int)wordvlen + 1 >= wordvsize) {
69 /* need to expand the array */
71 tmp = realloc(wordv, wordvsize * sizeof *wordv);
73 openpam_log(PAM_LOG_ERROR, "malloc(): %m");
80 wordv[wordvlen++] = word;
81 wordv[wordvlen] = NULL;
84 /* I/O error or out of memory */
87 free(wordv[wordvlen]);
92 /* assert(!ferror(f)) */
94 /* assert(ch == EOF || ch == '\n') */
95 if (ch == EOF && wordvlen == 0) {
99 if (ch == '\n' && lineno != NULL)
107 * The =openpam_readlinev function reads a line from a file, splits it
108 * into words according to the rules described in the =openpam_readword
109 * manual page, and returns a list of those words.
111 * If =lineno is not =NULL, the integer variable it points to is
112 * incremented every time a newline character is read.
113 * This includes quoted or escaped newline characters and the newline
114 * character at the end of the line.
116 * If =lenp is not =NULL, the number of words on the line is stored in the
117 * variable to which it points.
121 * If successful, the =openpam_readlinev function returns a pointer to a
122 * dynamically allocated array of pointers to individual dynamically
123 * allocated NUL-terminated strings, each containing a single word, in the
124 * order in which they were encountered on the line.
125 * The array is terminated by a =NULL pointer.
127 * The caller is responsible for freeing both the array and the individual
128 * strings by passing each of them to =!free.
130 * If the end of the line was reached before any words were read,
131 * =openpam_readlinev returns a pointer to a dynamically allocated array
132 * containing a single =NULL pointer.
134 * The =openpam_readlinev function can fail and return =NULL for one of
137 * - The end of the file was reached before any words were read; :errno is
138 * zero, =!ferror returns zero, and =!feof returns a non-zero value.
140 * - The end of the file was reached while a quote or backslash escape
141 * was in effect; :errno is set to =EINVAL, =!ferror returns zero, and
142 * =!feof returns a non-zero value.
144 * - An error occurred while reading from the file; :errno is non-zero,
145 * =!ferror returns a non-zero value and =!feof returns zero.
147 * - A =!malloc or =!realloc call failed; :errno is set to =ENOMEM,
148 * =!ferror returns a non-zero value, and =!feof may or may not return