---
name: "s2argv"
section: "3"
platform: "linux"
package: "libexecs-dev"
version: "1.4-2+b2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/s2argv"
---

# s2argv(3)

convert a command string in an argv array

## 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)](/execv/3.md),  [**execvp**(3)](/execvp/3.md) or [**execvpe**(3)](/execvpe/3.md). 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 function is NULL by default and all variables get replaced with an empty string. Programmers can use their own custom function instead).

**s2argv\_free** frees the memory that was allocated by **s2argv**.

**s2argvlen** returns the sum of the **s2argc** for all the commands, including the trailing NULLs that separate them.

**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)](/execv/3.md), [**execvp**(3)](/execvp/3.md) or [**execvpe**(3)](/execvpe/3.md). 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)](/exec/3.md)

## BUGS

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

## AUTHOR

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