I am using running one binary eg. 'ls -la' in my c code with execl() function and reading return status of child process. execl("/bin/ls", "ls", "-la", NULL); and receiving child status by waitpid(pid, &status, 0 ); function. But i need my out put in different time zone like TZ=UTC. So like to use 'ls -la' as 'TZ=UTC ls -la'. So is it possible to pass time zone argument with execl() function. Or is there any way to get the output in specific time zone. Looking for c code.
Asked
Active
Viewed 222 times
0
-
"Looking for c code." is nice as the tag is C, yet rather than post what you have tried, you described it. Better and more clear to post compilable code you have attempted. It adds value to the details of what you are attempting. – chux - Reinstate Monica May 29 '18 at 18:17
-
Use `execle` and provide an environment with `TZ` set to `UTC`. – jwdonahue May 29 '18 at 18:57
1 Answers
1
For this simple case, the easiest solution is to use setenv
to modify the value of the TZ
environment variable just before calling execl
. (Presumably you fork()
before the call to execl
; the setenv
should be called after the fork()
only in the child process, so that it doesn't modify the parent's environment.)
You could create an entire environment list and then pass it to execle
. But that's a lot of work for little benefit. You might want to do that if you were calling a setuid executable, or if you wanted to add a lot of environment variables, or if your C library doesn't have setenv
. But in normal cases, the single setenv
call is just fine.

rici
- 234,347
- 28
- 237
- 341