Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.process

License:
Boost License 1.0.

Authors:
Walter Bright, Andrei Alexandrescu, Vladimir Panteleev

Source:
std/process.d

int system(string command);
Execute command in a command shell.

Returns:
If command is null, returns nonzero if the command interpreter is found, and zero otherwise. If command is not null, returns -1 on error, or the exit status of command (which may in turn signal an error in command's execution).

Note:
On Unix systems, the homonym C function (which is accessible to D programs as std.c.system) returns a code in the same format as waitpid, waitpid, meaning that C programs must use the WEXITSTATUS macro to extract the actual exit code from the system call. D's system automatically extracts the exit status.

int execv(in string pathname, in string[] argv);
int execve(in string pathname, in string[] argv, in string[] envp);
int execvp(in string pathname, in string[] argv);
int execvpe(in string pathname, in string[] argv, in string[] envp);
Replace the current process by executing a command, pathname, with the arguments in argv. Typically, the first element of argv is the command being executed, i.e. argv[0] == pathname. The 'p' versions of exec search the PATH environment variable for pathname. The 'e' versions additionally take the new process' environment variables as an array of strings of the form key=value.

Does not return on success (the current process will have been replaced). Returns -1 on failure with no indication of the underlying error.

alias core.sys.posix.unistd.getpid getpid;
Returns the process ID of the calling process, which is guaranteed to be unique on the system. This call is always successful.

Example:
 writefln("Current process id: %s", getpid());

string shell(string cmd);
Runs cmd in a shell and returns its standard output. If the process could not be started or exits with an error code, throws an exception.

Example:
   auto tempFilename = chomp(shell("mcookie"));
   auto f = enforce(fopen(tempFilename), "w");
   scope(exit)
   {
       fclose(f) == 0 || assert(false);
       system(escapeShellCommand("rm", tempFilename));
   }
   ... use f ...

string getenv(in char[] name);
Gets the value of environment variable name as a string. Calls std.c.stdlib.getenv internally.

void setenv(in char[] name, in char[] value, bool overwrite);
Sets the value of environment variable name to value. If the value was written, or the variable was already present and overwrite is false, returns normally. Otherwise, it throws an exception. Calls std.c.stdlib.setenv internally.

void unsetenv(in char[] name);
Removes variable name from the environment. Calls std.c.stdlib.unsetenv internally.

alias Environment environment;
Manipulates environment variables using an associative-array-like interface.

Examples:
    // Return variable, or throw an exception if it doesn't exist.
    auto path = environment["PATH"];

    // Add/replace variable.
    environment["foo"] = "bar";

    // Remove variable.
    environment.remove("foo");

    // Return variable, or null if it doesn't exist.
    auto foo = environment.get("foo");

    // Return variable, or a default value if it doesn't exist.
    auto foo = environment.get("foo", "default foo value");

    // Return an associative array of type string[string] containing
    // all the environment variables.
    auto aa = environment.toAA();

void browse(string url);
Start up the browser and set it to viewing the page at url.

pure nothrow string escapeWindowsArgument(in char[] arg);
Quote an argument in a manner conforming to the behavior of CommandLineToArgvW.

string escapeShellCommand(in char[][] args...);
Escape an argv-style argument array to be used with the system or shell functions.

Example:
string url = "http://dlang.org/";
system(escapeShellCommand("wget", url, "-O", "dlang-index.html"));

Concatenate multiple escapeShellCommand and escapeShellFileName results to use shell redirection or piping operators.

Example:
system(
    escapeShellCommand("curl", "http://dlang.org/download.html") ~
    "|" ~
    escapeShellCommand("grep", "-o", `http://\S*\.zip`) ~
    ">" ~
    escapeShellFileName("D download links.txt"));

pure nothrow string escapeShellFileName(in char[] fn);
Escape a filename to be used for shell redirection with the system or shell functions.