S2ARGV(3)

πŸ“¦
version 1.4 Β· 2 versions
, in debian trixie, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– 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 πŸ’œ

exec(3)


πŸ“ 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