This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFA] Add libiberty/argv.c:countargv


Hi.

Part of the abstraction of an argv is a count of the number elements.
This patch adds countargv which I have use for in gdb.

Ok to check in?

2011-09-20  Doug Evans  <dje@google.com>

	include/
	* libiberty.h (countargv): Declare.

	libiberty/
	* argv.c (countargv): New function.

Index: include/libiberty.h
===================================================================
RCS file: /cvs/src/src/include/libiberty.h,v
retrieving revision 1.64
diff -u -p -r1.64 libiberty.h
--- include/libiberty.h	22 Jul 2011 14:37:51 -0000	1.64
+++ include/libiberty.h	20 Sep 2011 18:18:07 -0000
@@ -91,6 +91,10 @@ extern void expandargv PARAMS ((int *, c
 
 extern int writeargv PARAMS ((char **, FILE *));
 
+/* Return the number of elements in argv.  */
+
+extern int countargv (char**);
+
 /* Return the last component of a path name.  Note that we can't use a
    prototype here because the parameter is declared inconsistently
    across different systems, sometimes as "char *" and sometimes as
Index: libiberty/argv.c
===================================================================
RCS file: /cvs/src/src/libiberty/argv.c,v
retrieving revision 1.22
diff -u -p -r1.22 argv.c
--- libiberty/argv.c	13 Aug 2010 11:36:10 -0000	1.22
+++ libiberty/argv.c	20 Sep 2011 18:21:28 -0000
@@ -492,6 +492,28 @@ expandargv (int *argcp, char ***argvp)
     }
 }
 
+/*
+
+@deftypefn Extension int countargv (char **@var{argv})
+
+Return the number of elements in @var{argv}.
+Returns zero if @var{argv} is NULL.
+
+@end deftypefn
+
+*/
+
+int
+countargv (char **argv)
+{
+  int argc;
+
+  if (argv == NULL)
+    return 0;
+  for (argc = 0; argv[argc] != NULL; argc++);
+  return argc;
+}
+
 #ifdef MAIN
 
 /* Simple little test driver. */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]