system_nosh(3) | Linux Programmer's Manual | system_nosh(3) |
system_nosh, system_execs - execute a command with its arguments from a string without using a shell
#include <stdlib.h>
#include <execs.h>
int system_nosh(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]);
These functions are provided by libexecs. Link with -lexecs.
system_nosh is an almost drop in replacement for
system(3) provided by the libc. system_nosh parses the command
string and runs the command directly, without using a shell.
(system_execsp and system_nosh are synonyms).
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. 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_execsa does not use the PATH variable, argv[0] must be specified
as a full pathname.
system_execsrp and system_execsr 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.
All these functions can run sequences of commands separated by semicolons
(;). The first command returning a non-zero exit status breaks the
sequence.
These functions have the same return values of system(3). When running a sequence of commands, it returns the exit 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.
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"); } }
Bug reports should be addressed to <info@virtualsquare.org>
Renzo Davoli <renzo@cs.unibo.it>
2014-05-27 | VirtualSquare |