Argument vector conversion


Detailed Description

The command line arguments are the whitespace-separated tokens given in the shell command used to invoke the program; thus, in `cat foo bar', the arguments are `foo' and `bar'. For the command `cat foo bar', argc is 3 and argv has three elements, "cat", "foo" and "bar".

The argv_from_str() and argv_to_str() functions convert an argument vector (as in main()) from and to a string (as seen in the shell), respectively.


Functions

int argv_from_str (char *str, char **const argv, int argc)
 convert string to argument vector
char * argv_to_str (int argc, const char **const argv)
 convert argument vector to string


Function Documentation

int argv_from_str ( char *  str,
char **const  argv,
int  argc 
)

convert string to argument vector

Parameters:
[in] str space seperated string to convert
[out] argv argument vector to fill
[in] argc size of argv
Returns:
number of arguments that (would) have been converted
Note:
the caller has to allocate memory for argv

this function modifies its first argument, use with caution

Definition at line 23 of file argv_from_str.c.

References char_isspace.

Referenced by exec_fork(), exec_fork_background(), exec_fork_pipe(), and exec_replace().

00024 {
00025         int i, argc = 0;
00026         
00027         for (i = 0; i < max_argc; i++)
00028                 argv[i] = NULL;
00029         
00030         if (!str)
00031                 return 0;
00032         
00033         while (*str) {
00034                 while (char_isspace(*str))
00035                         *(str++) = '\0';
00036                 
00037                 if (*str && argc < max_argc - 1) {
00038                         argv[argc] = str;
00039                         argc++;
00040                         
00041                         while (*str && !char_isspace(*str))
00042                                 str++;
00043                 }
00044                 
00045                 else
00046                         break;
00047         }
00048         
00049         return argc;
00050 }

char* argv_to_str ( int  argc,
const char **const  argv 
)

convert argument vector to string

Parameters:
[in] argc size of argv
[in] argv argument vector to convert
Returns:
space seperated string (obtained by malloc(3))
Note:
The caller should free obtained memory using free(3)
See also:
malloc(3)

free(3)

Definition at line 22 of file argv_to_str.c.

References stralloc_t::len, stralloc_t::s, str_dupn(), stralloc_catm(), stralloc_free(), and stralloc_init().

00023 {
00024         int i;
00025         size_t len;
00026         char *str;
00027         stralloc_t buf;
00028         
00029         stralloc_init(&buf);
00030         
00031         for (i = 0; i < argc; i++)
00032                 stralloc_catm(&buf, argv[i], " ", 0);
00033         
00034         if (buf.len < 1)
00035                 len = 0;
00036         else
00037                 len = buf.len - 1;
00038         
00039         str = str_dupn(buf.s, len);
00040         stralloc_free(&buf);
00041         return str;
00042 }


Generated on Sun Dec 3 17:46:16 2006 for lucid by  doxygen 1.5.1