Suppose that I have a program named myprog, and I type this in a shell

$ myprog -z hello -m "this is a message"

The function main in myprog will receive a char** argv containing the following strings

"-z", "hello", "-m", "this is a message"

So there must be somewhere in a C library a function which takes the command line string and produces the char** argv.
My question for you, C experts, is where is this function and how is it called ?

The function main in myprog will receive a char** argv containing the following strings

"-z", "hello", "-m", "this is a message"

One small correction.
char** argv will contain

"myprog", "-z", "hello", "-m", "this is a message"

So there must be somewhere in a C library a function which takes the command line string and produces the char** argv.
My question for you, C experts, is where is this function and how is it called ?

I guess this part is done by the OS rather than a compiler.

I'm no assembly guru, but you might be able to find something were you to decompile it.

Thanks for your replies. I think you're right, there are some functions related to this in OS source code, but they're not very useful. It would have been great to find a well known C function for this, but it probably doesn't exist.

yes.
That is iterpreted and processed by the shell which sets the argv and forward to the executable binary file.

where is this function and how is it called ?

It is done by the C runtime. The same code that calls main() collects arguments from the command shell and parses them into an array of char pointers.

It is done by the C runtime. The same code that calls main() collects arguments from the command shell and parses them into an array of char pointers.

I looked in the source code of gcc-4.3.2 and I found this

char **buildargv (const char *input)
{
  char *arg;
  char *copybuf;
  int squote = 0;
  int dquote = 0;
  int bsquote = 0;
  int argc = 0;
  int maxargc = 0;
  char **argv = NULL;
  char **nargv;

  if (input != NULL)
    {
      copybuf = (char *) alloca (strlen (input) + 1);
      /* Is a do{}while to always execute the loop once.  Always return an
	 argv, even for null strings.  See NOTES above, test case below. */
      do
	{
	  /* Pick off argv[argc] */
	  while (ISBLANK (*input))
	    {
	      input++;
	    }
	  if ((maxargc == 0) || (argc >= (maxargc - 1)))
	    {
	      /* argv needs initialization, or expansion */
	      if (argv == NULL)
		{
		  maxargc = INITIAL_MAXARGC;
		  nargv = (char **) malloc (maxargc * sizeof (char *));
		}
	      else
		{
		  maxargc *= 2;
		  nargv = (char **) realloc (argv, maxargc * sizeof (char *));
		}
	      if (nargv == NULL)
		{
		  if (argv != NULL)
		    {
		      freeargv (argv);
		      argv = NULL;
		    }
		  break;
		}
	      argv = nargv;
	      argv[argc] = NULL;
	    }
	  /* Begin scanning arg */
	  arg = copybuf;
	  while (*input != EOS)
	    {
	      if (ISSPACE (*input) && !squote && !dquote && !bsquote)
		{
		  break;
		}
	      else
		{
		  if (bsquote)
		    {
		      bsquote = 0;
		      *arg++ = *input;
		    }
		  else if (*input == '\\')
		    {
		      bsquote = 1;
		    }
		  else if (squote)
		    {
		      if (*input == '\'')
			{
			  squote = 0;
			}
		      else
			{
			  *arg++ = *input;
			}
		    }
		  else if (dquote)
		    {
		      if (*input == '"')
			{
			  dquote = 0;
			}
		      else
			{
			  *arg++ = *input;
			}
		    }
		  else
		    {
		      if (*input == '\'')
			{
			  squote = 1;
			}
		      else if (*input == '"')
			{
			  dquote = 1;
			}
		      else
			{
			  *arg++ = *input;
			}
		    }
		  input++;
		}
	    }
	  *arg = EOS;
	  argv[argc] = strdup (copybuf);
	  if (argv[argc] == NULL)
	    {
	      freeargv (argv);
	      argv = NULL;
	      break;
	    }
	  argc++;
	  argv[argc] = NULL;

	  while (ISSPACE (*input))
	    {
	      input++;
	    }
	}
      while (*input != EOS);
    }
  return (argv);
}

It's part of the gnu libiberty library :) . Thanks again to all of you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.