Skip to content

Commit

Permalink
Bug fixing in env and date
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Holzschuch committed Apr 14, 2023
1 parent e7ae429 commit 70a2d50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions shell_cmds/date/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ static void usage(void);

static const char *rfc2822_format = "%a, %d %b %Y %T %z";

// iOS: store the previous value of TZ, and restore it at the end
#undef setenv
#undef unsetenv
#undef getenv
char* local_TZ;
char* global_TZ;

int
date_main(int argc, char *argv[])
{
Expand All @@ -111,6 +118,13 @@ date_main(int argc, char *argv[])
jflag = nflag = Rflag = 0;
optind = 1; opterr = 1; optreset = 1;
set_timezone = 0;
// iOS: strftime uses TZ from the real environment, not from ours:
global_TZ = getenv("TZ"); // so we can restore it later
local_TZ = ios_getenv("TZ");
if (local_TZ)
setenv("TZ", local_TZ, 1);
else
unsetenv("TZ");
while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1)
switch((char)ch) {
case 'd': /* daylight savings time */
Expand Down Expand Up @@ -220,6 +234,10 @@ date_main(int argc, char *argv[])

(void)strftime(buf, sizeof(buf), format, &lt);
(void)fprintf(thread_stdout, "%s\n", buf);
if (global_TZ)
setenv("TZ", global_TZ, 1); // restore global value of TZ
else
unsetenv("TZ");
if (fflush(thread_stdout)) {
err(1, "stdout");
}
Expand Down Expand Up @@ -369,5 +387,9 @@ usage(void)
"[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]" :
" "
"[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");
if (global_TZ)
setenv("TZ", global_TZ, 1); // restore global value of TZ
else
unsetenv("TZ");
exit(1);
}
2 changes: 2 additions & 0 deletions shell_cmds/env/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ env_main(int argc, char **argv)
err(errno == ENOENT ? 127 : 126, "%s", *argv);
#else
// execvp is not supposed to return, but on iOS it does.
int pid = ios_fork();
int childerr = execvp(*argv, argv);
waitpid(pid, &childerr, 0);
ios_exit(childerr);
#endif
}
Expand Down

0 comments on commit 70a2d50

Please sign in to comment.