1 /* Iterate over arguments from argv or --files0-from=FILE
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering. */
20 #include "argv-iter.h"
25 /* The attribute __pure__ was added in gcc 2.96. */
26 #undef _GL_ATTRIBUTE_PURE
27 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
28 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
30 # define _GL_ATTRIBUTE_PURE /* empty */
35 /* Test FP to determine whether in read-mode or argv-mode. */
36 /* file-mode: fp records position */
42 /* argv-mode: record just argv and current pointer */
47 struct argv_iterator *
48 argv_iter_init_argv (char **argv)
50 struct argv_iterator *ai = malloc (sizeof *ai);
59 /* Initialize to read from the stream, FP.
60 The input is expected to contain a list of NUL-delimited tokens. */
61 struct argv_iterator *
62 argv_iter_init_stream (FILE *fp)
64 struct argv_iterator *ai = malloc (sizeof *ai);
77 argv_iter (struct argv_iterator *ai, enum argv_iter_err *err)
81 ssize_t len = getdelim (&ai->tok, &ai->buf_len, '\0', ai->fp);
84 *err = feof (ai->fp) ? AI_ERR_EOF : AI_ERR_READ;
107 size_t _GL_ATTRIBUTE_PURE
108 argv_iter_n_args (struct argv_iterator const *ai)
110 return ai->fp ? ai->item_idx : ai->p - ai->arg_list;
114 argv_iter_free (struct argv_iterator *ai)