| | |
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 27 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 |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






