summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2008-11-24 14:11:15 +0100
committerJim Meyering <meyering@redhat.com>2008-12-01 17:40:59 +0100
commitca738e4414741dd5154304dcf3ac04d64207fe68 (patch)
tree213cedecfd7ff11d9b1b420e667b1a49b53cf29a
parent21eb87e6eafee9111ac2da32cfbfb9ee34a4b465 (diff)
downloadcoreutils-ca738e4414741dd5154304dcf3ac04d64207fe68.tar.xz
argv-iter: new module
* gl/lib/argv-iter.h: New file. * gl/lib/argv-iter.c: New file. * gl/modules/argv-iter: New file. With a suggestion for improved memory management by Pádraig Brady.
-rw-r--r--gl/lib/argv-iter.c117
-rw-r--r--gl/lib/argv-iter.h47
-rw-r--r--gl/modules/argv-iter24
3 files changed, 188 insertions, 0 deletions
diff --git a/gl/lib/argv-iter.c b/gl/lib/argv-iter.c
new file mode 100644
index 000000000..4c79b109f
--- /dev/null
+++ b/gl/lib/argv-iter.c
@@ -0,0 +1,117 @@
+/* Iterate over arguments from argv or --files0-from=FILE
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Jim Meyering. */
+
+#include <config.h>
+#include "argv-iter.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct argv_iterator
+{
+ /* Test FP to determine whether in read-mode or argv-mode. */
+ /* file-mode: fp records position */
+ FILE *fp;
+ size_t item_idx;
+ char *tok;
+ size_t buf_len;
+
+ /* argv-mode: record just argv and current pointer */
+ char **arg_list;
+ char **p;
+};
+
+struct argv_iterator *
+argv_iter_init_argv (char **argv)
+{
+ struct argv_iterator *ai = malloc (sizeof *ai);
+ if (!ai)
+ return NULL;
+ ai->fp = NULL;
+ ai->arg_list = argv;
+ ai->p = argv;
+ return ai;
+}
+
+/* Initialize to read from the stream, FP.
+ The input is expected to contain a list of NUL-delimited tokens. */
+struct argv_iterator *
+argv_iter_init_stream (FILE *fp)
+{
+ struct argv_iterator *ai = malloc (sizeof *ai);
+ if (!ai)
+ return NULL;
+ ai->fp = fp;
+ ai->tok = NULL;
+ ai->buf_len = 0;
+
+ ai->item_idx = 0;
+ ai->arg_list = NULL;
+ return ai;
+}
+
+char *
+argv_iter (struct argv_iterator *ai, enum argv_iter_err *err)
+{
+ if (ai->fp)
+ {
+ ssize_t len = getdelim (&ai->tok, &ai->buf_len, '\0', ai->fp);
+ if (len < 0)
+ {
+ *err = feof (ai->fp) ? AI_ERR_EOF : AI_ERR_READ;
+ return NULL;
+ }
+
+ *err = AI_ERR_OK;
+ ai->item_idx++;
+ return ai->tok;
+ }
+ else
+ {
+ if (*(ai->p) == NULL)
+ {
+ *err = AI_ERR_EOF;
+ return NULL;
+ }
+ else
+ {
+ *err = AI_ERR_OK;
+ return *(ai->p++);
+ }
+ }
+}
+
+size_t
+argv_iter_n_args (struct argv_iterator const *ai)
+{
+ return ai->fp ? ai->item_idx : ai->p - ai->arg_list;
+}
+
+void
+argv_iter_free (struct argv_iterator *ai)
+{
+ if (ai->fp)
+ free (ai->tok);
+ free (ai);
+}
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/gl/lib/argv-iter.h b/gl/lib/argv-iter.h
new file mode 100644
index 000000000..06a397c9e
--- /dev/null
+++ b/gl/lib/argv-iter.h
@@ -0,0 +1,47 @@
+/* Iterate over arguments from argv or --files0-from=FILE
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdbool.h>
+
+struct argv_iterator;
+enum argv_iter_err;
+
+#undef _ATTRIBUTE_NONNULL_
+#if __GNUC__ == 3 && __GNUC_MINOR__ >= 3 || 3 < __GNUC__
+# define _ATTRIBUTE_NONNULL_(m,...) __attribute__ ((__nonnull__ (m)))
+#else
+# define _ATTRIBUTE_NONNULL_(m,...)
+#endif
+
+enum argv_iter_err
+{
+ AI_ERR_OK = 1,
+ AI_ERR_EOF,
+ AI_ERR_MEM,
+ AI_ERR_READ
+};
+
+struct argv_iterator *argv_iter_init_argv (char **argv)
+ _ATTRIBUTE_NONNULL_ (1);
+struct argv_iterator *argv_iter_init_stream (FILE *fp)
+ _ATTRIBUTE_NONNULL_ (1);
+char *argv_iter (struct argv_iterator *, enum argv_iter_err *)
+ _ATTRIBUTE_NONNULL_ (1, 2);
+size_t argv_iter_n_args (struct argv_iterator const *)
+ _ATTRIBUTE_NONNULL_ (1);
+void argv_iter_free (struct argv_iterator *)
+ _ATTRIBUTE_NONNULL_ (1);
diff --git a/gl/modules/argv-iter b/gl/modules/argv-iter
new file mode 100644
index 000000000..e9fc63391
--- /dev/null
+++ b/gl/modules/argv-iter
@@ -0,0 +1,24 @@
+Description:
+iterate through argv or a --files0-from=-specified file
+
+Files:
+lib/argv-iter.c
+lib/argv-iter.h
+
+Depends-on:
+getdelim
+stdbool
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += argv-iter.c argv-iter.h
+
+Include:
+"argv-iter.h"
+
+License
+GPL
+
+Maintainer:
+Jim Meyering