FloriOn
FloriOn

Reputation: 287

How to use in-line expansion with execvp

I am interested in, how can one accomplish in-line linux expansion with execvp in C. I tried doing so with a backslashed apostrophe inside the one that indicates, that the following is a string, but failed. The code, that I run is as it follows:

static const char *datecmd[] = { "xsetroot", "-name", "$(date +'%T')", NULL };
execvp(((char **)arg->v)[0], (char **)arg->v);

Upvotes: 1

Views: 179

Answers (2)

It looks like you are interested in globbing, see glob(7). Then you could also use wordexp(3) to expand your thing, and later call execvp(3) on its result.

BTW, for the particular expansion of date +%T you should read time(7) and simply use a usual combination of time(2), localtime(3), strftime(3). You don't need to run any date process (and you might avoid any globbing)

Upvotes: 1

Politank-Z
Politank-Z

Reputation: 3721

In-line expansion is a function of the shell, so you would need to run your command from inside of a shell, e.g.:

static const char *datecmd[] = { "bash", "-c",
                                 "xsetroot -name $(date +'%T')", NULL };
execvp(((char **)arg->v)[0], (char **)arg->v);

Upvotes: 3

Related Questions