S2ARGV(3)

NAME
s2argv - convert a command string in an argv array

SYNOPSIS
#include <unistd.h>
#include <stdlib.h>
#include <execs.h>

char **s2argv(const char *args);
void s2argv_free(char **argv);

size_t s2argvlen(char **argv);

size_t s2argc(char **argv);

typedef char * (* s2argv_getvar_t) (const char *name);
extern s2argv_getvar_t s2argv_getvar;

These functions are provided by libexecs and libeexecs. Link with -lexecs or -leexecs.

DESCRIPTION
s2argv convert a command string in an argv array for execv(3), execvp(3) or execvpe(3). Single or double quotes can be used to delimitate command arguments including spaces and a non quoted backslash (\) is the escape character to protect the next char.
s2argv can parse several commands separated by semicolons (;). The argv of each command is terminated by a NULL element, one further NULL element tags the end of the array returned by s2argv.
s2argv supports variables as arguments. When an argument of a command is a dollar sign followed by a name (e.g. $USER) s2argv puts the output of the s2argv_getvar function instead. (It is possible for example to assign s2argv_getvar=getenv. For security reasons, the default value is getvar_null which always returns an empty string. Programmers can use their own custom function instead).

s2argv_free frees the memory that was allocated by s2argv.

s2argvlen returns the length of the array returned by s2argv.

s2argc returns the number of arguemnts of the (first) command returned by s2argv. (The beginning of the next argv is argv+s2argc(argv)+1).

RETURN VALUE
s2argv returns a dynamically allocated argv, ready to be used as an argument to execv(3), execvp(3) or execvpe(3). The return value of s2argv should be freed by s2argv_free in case the exec command does not succeed.

EXAMPLE
The following program demonstrates the use of s2argv:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <execs.h>
#define BUFLEN 1024
int main(int argc, char *argv)
{
	char buf[BUFLEN];
	printf("type in a command and its arguments, e.g. 'ls -l'\n");
	if (fgets(buf, BUFLEN, stdin) != NULL) {
		char **argv=s2argv(buf);
		execvp(argv[0], argv);
		s2argv_free(argv);
		printf("exec error\n");
	}
}

SEE ALSO
exec(3)

BUGS
Bug reports should be addressed to <info@virtualsquare.org>

AUTHOR
Renzo Davoli <renzo@cs.unibo.it>