diff mbox

[RFA] Add libiberty/argv.c:countargv

Message ID 20110920182334.658F32461A8@ruffy.mtv.corp.google.com
State New
Headers show

Commit Message

Doug Evans Sept. 20, 2011, 6:23 p.m. UTC
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.

Comments

Doug Evans Sept. 26, 2011, 4 p.m. UTC | #1
On Tue, Sep 20, 2011 at 11:23 AM, Doug Evans <dje@google.com> wrote:
> 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.

Ping.
Ian Lance Taylor Sept. 28, 2011, 6:22 p.m. UTC | #2
On Tue, Sep 20, 2011 at 11:23 AM, Doug Evans <dje@google.com> wrote:
>
> 2011-09-20  Doug Evans  <dje@google.com>
>
>        include/
>        * libiberty.h (countargv): Declare.
>
>        libiberty/
>        * argv.c (countargv): New function.


> +  for (argc = 0; argv[argc] != NULL; argc++);

Please write the semicolon on the next line.  I think it is too easy
to get confused when written that way.

  for (argc = 0; argv[argc] != NULL; argc++)
    ;

This is OK with that change.

Thanks.

Ian
diff mbox

Patch

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. */