S2ARGV(3)
π S2ARGV(3) π
okay bestie, welcome to the s2argv page from the Linux Programmer's Manual β yeah, we're doing C today and we're gonna slay it π«Ά
β¨ NAME β¨
s2argv β turn a whole command string into an argv array, like magic but make it C π
πΈ 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, so link with -lexecs or -leexecs. no cap. π₯
π¦ DESCRIPTION π¦
s2argv takes a command string and converts it into an argv array, the exact kind of thing execv(3), execvp(3) or execvpe(3) are begging for. iconic behaviour honestly.
single quotes or double quotes can be used to delimit command arguments that have spaces in them, and a non-quoted backslash (\) is the escape character that protects the next char. so you can hide your lil secrets in there π
s2argv can also parse several commands separated by semicolons (;). the argv of each command is terminated by a NULL element, and one further NULL element tags the end of the array that s2argv returns. two layers of NULL, she contains multitudes β¨
s2argv supports variables as arguments. when an argument of a command is a dollar sign followed by a name (e.g. $USER) then s2argv puts the output of the s2argv_getvar function in there instead. (you could, for example, assign s2argv_getvar=getenv. but β and this is the important part β 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. clean up after yourself, bestie, it's giving self-care πΈ
s2argvlen returns the sum of the s2argc for all the commands, including the trailing NULLs that separate them. adding it all up, one girl doing math π
s2argc returns the number of arguments of the (first) command returned by s2argv. (and the beginning of the next argv is argv+s2argc(argv)+1 β iykyk, do not forget that +1, it is load-bearing π)
π RETURN VALUE π
s2argv returns a dynamically allocated argv, ready to be used as an argument to execv(3), execvp(3) or execvpe(3). and listen β the return value of s2argv should be freed by s2argv_free in case the exec command does not succeed. if it succeeds you're somebody else's process now and you don't gotta worry about it π but if it fails, free it. periodt.
π 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");
}
}
main character energy fr fr π
π SEE ALSO π
π BUGS π
bug reports should be addressed to <info@virtualsquare.org> β they're literally waiting for your email bestie, tell them everything π
π AUTHOR π
Renzo Davoli <renzo@cs.unibo.it> β the icon, the legend, the moment β
and that's the whole page, babe π this was VirtualSquare, dated 2014-05-27 β yeah she's an elder but she still eats, and honestly? she ate. go compile something and text me when it works π§β¨
π¦ where this page comes from
- debian trixie: libexecs-dev 1.4-2+b2 Β· LGPL-2.1-or-later
- ubuntu noble: libexecs-dev 1.4-2build1 Β· LGPL-2.1-or-later
- ubuntu questing: libexecs-dev 1.4-2build1
- ubuntu resolute: libexecs-dev 1.4-2build2 Β· LGPL-2.1-or-later
- ubuntu stonking: libexecs-dev 1.4-2build2 Β· LGPL-2.1-or-later
- Copyright (C) 2014 Renzo Davoli. University of Bologna. <renzo@cs.unibo.it>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms