table of contents
system_safe(3) | Linux Programmer's Manual | system_safe(3) |
NAME¶
system_safe, system_execs, system_nosh - execute a command with its arguments from a string without using a shell
SYNOPSIS¶
#include <stdlib.h>
#include <execs.h>
int system_safe(const char *command);
int system_execsp(const char *command);
int system_execsa(const char *command);
int system_execs(const char *path, char const
*command);
int system_execsrp(const char *command, int
redir[3]);
int system_execsra(const char *command, int
redir[3]);
int system_execsr(const char *path, char const
*command, int redir[3]);
int system_nosh(const char *command);
int system_execsqp(const char *command);
int system_execsqa(const char *command);
int system_execsqrp(const char *command, int
redir[3]);
int system_execsqra(const char *command, int
redir[3]);
These functions are provided by libexecs. Link with -lexecs.
DESCRIPTION¶
system_safe is a safe replacement for system(3)
provided by the libc. system_safe parses the command string and runs
the command directly, without using a shell. The command must be specified
as an absolute pathname. system_safe does not support variables as
argument.
Command arguments in args are delimited by space characters (blank,
tabs or new lines). 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.
system_execsa is like system_safe supporting also 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)
In system_execsp the executable file is sought using the PATH
environment variable as explained for execlp(3).
system_execs requires the path of the executable to be specified as its
first parameter so it does not use the PATH environment variable.
system_execsr, system_execsrp and system_execsra works as
their couterparts without the 'r', but they permit the redirection of
standard input, output and error streams. Their last parameter is an array
of three integers. The standard input of the command will be redirected to
redir[0] if it is positive, the standard output to redir[1] if
it is not negative and different from 1, the standard error to
redir[2] if it is not negative and different from 2.
system_execsra does not use the PATH variable, argv[0] must be
specified as a full pathname.
system_nosh, system_execsqp, system_execsqa,
system_execsqrp and system_execsqra can run sequences of
commands separated by semicolons (;). The first command returning a
non-zero exit status breaks the sequence.
system_nosh is an almost drop in replacement for system(3)
provided by the libc. (system_execsqp and system_nosh are
synonyms).
RETURN VALUE¶
These functions have the same return values of system(3). When running a sequence of commands, it returns the "wait status" of the first command returning a non-zero value. If the return value is zero it means that all the commands of the sequence succeeded.
EXAMPLE¶
The following program shows the usage of system_nosh:
#include <stdio.h> #include <unistd.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"); while (fgets(buf, BUFLEN, stdin) != NULL) { printf("Command: '%s' \n",buf); system_nosh(buf); printf("Command done\n"); } }
SEE ALSO¶
system(3),execs(3),s2argv(3)
BUGS¶
Bug reports should be addressed to <info@virtualsquare.org>
AUTHOR¶
Renzo Davoli <renzo@cs.unibo.it>
2014-05-27 | VirtualSquare |