| | |
Which function builds argv ?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Suppose that I have a program named myprog, and I type this in a shell
The function main in myprog will receive a char** argv containing the following strings
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 ?
C Syntax (Toggle Plain Text)
$ myprog -z hello -m "this is a message"
C Syntax (Toggle Plain Text)
"-z", "hello", "-m", "this is a message"
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.
0
#2 Oct 29th, 2009
•
•
•
•
The function main in myprog will receive a char** argv containing the following strings
C Syntax (Toggle Plain Text)
"-z", "hello", "-m", "this is a message"
char** argv will contain
C Syntax (Toggle Plain Text)
"myprog", "-z", "hello", "-m", "this is a message"
0
#3 Oct 29th, 2009
I'm no assembly guru, but you might be able to find something were you to decompile it.
1
#7 Oct 29th, 2009
•
•
•
•
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.
C Syntax (Toggle Plain Text)
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 ! ![]() |
Similar Threads
- Array Problem (PHP)
- Code Help - Restructure. (C++)
- Multi-level dynamic navigation with php (PHP)
- Compiling error help please (C++)
- Pointer Help (C++)
- Passing argc and argv to function (C++)
- Export Obejct from DLL (C++)
- matlab help : function, global, and ode45 (Legacy and Other Languages)
- can anyone please brief me on argc and argv in C. (C)
Other Threads in the C Forum
- Previous Thread: gedit plugin: code completion (gtk)
- Next Thread: C, do while loop.
Views: 372 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net array arrays asterisks binarysearch calculate changingto char cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue mysql number opendocumentformat opensource overwrite owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






