Which function builds argv ?

Thread Solved

Join Date: Jul 2008
Posts: 894
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 210
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark

Which function builds argv ?

 
0
  #1
28 Days Ago
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; 28 Days Ago at 2:35 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 335
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 48
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
28 Days Ago
Originally Posted by Gribouillis View Post
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"
Originally Posted by Gribouillis View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #3
28 Days Ago
I'm no assembly guru, but you might be able to find something were you to decompile it.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 894
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 210
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark
 
0
  #4
28 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 335
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 48
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #5
28 Days Ago
yes.
That is iterpreted and processed by the shell which sets the argv and forward to the executable binary file.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #6
28 Days Ago
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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 894
Reputation: Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about Gribouillis has a spectacular aura about 
Solved Threads: 210
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Practically a Posting Shark
 
1
  #7
28 Days Ago
Originally Posted by Tom Gunn View Post
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 !
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC