| | |
Which function builds argv ?
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; 28 Days Ago at 2:35 am.
0
#2 28 Days Ago
•
•
•
•
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 28 Days Ago
I'm no assembly guru, but you might be able to find something were you to decompile it.
1
#7 28 Days Ago
•
•
•
•
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.
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






