944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 800
  • C RSS
Oct 29th, 2009
0

Which function builds argv ?

Expand Post »
Suppose that I have a program named myprog, and I type this in a shell
  1. $ myprog -z hello -m "this is a message"
The function main in myprog will receive a char** argv containing the following strings
  1. "-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 ?
Last edited by Gribouillis; Oct 29th, 2009 at 2:35 am.
Similar Threads
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 29th, 2009
0
Re: Which function builds argv ?
The function main in myprog will receive a char** argv containing the following strings
  1. "-z", "hello", "-m", "this is a message"
One small correction.
char** argv will contain
  1. "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.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 29th, 2009
0
Re: Which function builds argv ?
I'm no assembly guru, but you might be able to find something were you to decompile it.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 29th, 2009
0
Re: Which function builds argv ?
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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 29th, 2009
0
Re: Which function builds argv ?
yes.
That is iterpreted and processed by the shell which sets the argv and forward to the executable binary file.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Oct 29th, 2009
1
Re: Which function builds argv ?
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 29th, 2009
1
Re: Which function builds argv ?
Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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
  1. char **buildargv (const char *input)
  2. {
  3. char *arg;
  4. char *copybuf;
  5. int squote = 0;
  6. int dquote = 0;
  7. int bsquote = 0;
  8. int argc = 0;
  9. int maxargc = 0;
  10. char **argv = NULL;
  11. char **nargv;
  12.  
  13. if (input != NULL)
  14. {
  15. copybuf = (char *) alloca (strlen (input) + 1);
  16. /* Is a do{}while to always execute the loop once. Always return an
  17. argv, even for null strings. See NOTES above, test case below. */
  18. do
  19. {
  20. /* Pick off argv[argc] */
  21. while (ISBLANK (*input))
  22. {
  23. input++;
  24. }
  25. if ((maxargc == 0) || (argc >= (maxargc - 1)))
  26. {
  27. /* argv needs initialization, or expansion */
  28. if (argv == NULL)
  29. {
  30. maxargc = INITIAL_MAXARGC;
  31. nargv = (char **) malloc (maxargc * sizeof (char *));
  32. }
  33. else
  34. {
  35. maxargc *= 2;
  36. nargv = (char **) realloc (argv, maxargc * sizeof (char *));
  37. }
  38. if (nargv == NULL)
  39. {
  40. if (argv != NULL)
  41. {
  42. freeargv (argv);
  43. argv = NULL;
  44. }
  45. break;
  46. }
  47. argv = nargv;
  48. argv[argc] = NULL;
  49. }
  50. /* Begin scanning arg */
  51. arg = copybuf;
  52. while (*input != EOS)
  53. {
  54. if (ISSPACE (*input) && !squote && !dquote && !bsquote)
  55. {
  56. break;
  57. }
  58. else
  59. {
  60. if (bsquote)
  61. {
  62. bsquote = 0;
  63. *arg++ = *input;
  64. }
  65. else if (*input == '\\')
  66. {
  67. bsquote = 1;
  68. }
  69. else if (squote)
  70. {
  71. if (*input == '\'')
  72. {
  73. squote = 0;
  74. }
  75. else
  76. {
  77. *arg++ = *input;
  78. }
  79. }
  80. else if (dquote)
  81. {
  82. if (*input == '"')
  83. {
  84. dquote = 0;
  85. }
  86. else
  87. {
  88. *arg++ = *input;
  89. }
  90. }
  91. else
  92. {
  93. if (*input == '\'')
  94. {
  95. squote = 1;
  96. }
  97. else if (*input == '"')
  98. {
  99. dquote = 1;
  100. }
  101. else
  102. {
  103. *arg++ = *input;
  104. }
  105. }
  106. input++;
  107. }
  108. }
  109. *arg = EOS;
  110. argv[argc] = strdup (copybuf);
  111. if (argv[argc] == NULL)
  112. {
  113. freeargv (argv);
  114. argv = NULL;
  115. break;
  116. }
  117. argc++;
  118. argv[argc] = NULL;
  119.  
  120. while (ISSPACE (*input))
  121. {
  122. input++;
  123. }
  124. }
  125. while (*input != EOS);
  126. }
  127. return (argv);
  128. }

It's part of the gnu libiberty library . Thanks again to all of you !
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: gedit plugin: code completion (gtk)
Next Thread in C Forum Timeline: C, do while loop.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC