Skip to content
Open
2 changes: 1 addition & 1 deletion etc/login.defs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin
# If defined, either a TZ environment parameter spec or the
# fully-rooted pathname of a file containing such a spec.
#
#ENV_TZ TZ=CST6CDT
#ENV_TZ TZ=UTC
#ENV_TZ /etc/tzname

#
Expand Down
68 changes: 33 additions & 35 deletions lib/tz.c
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@
/*
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
* SPDX-FileCopyrightText: 1991 - 1994, Chip Rosenthal
* SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
* SPDX-FileCopyrightText: 2007 - 2010, Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 1991-1994, Julianne Frances Haugh
// SPDX-FileCopyrightText: 1991-1994, Chip Rosenthal
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
// SPDX-FileCopyrightText: 2003-2005, Tomasz Kłoczko
// SPDX-FileCopyrightText: 2007-2010, Nicolas François
// SPDX-FileCopyrightText: 2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#ifndef USE_PAM

#ident "$Id$"

#include <limits.h>
#include <stdio.h>
#include <string.h>

#include "defines.h"
#include "getdef.h"
#include "io/fgets/fgets.h"
#include "prototypes.h"
#include "string/strtok/stpsep.h"


#define DEFAULT_TZ "TZ=UTC"


/*
* tz - return local timezone name
*
* tz() determines the name of the local timezone by reading the
* contents of the file named by ``fname''.
* contents of the file named by 'path'.
*/
/*@observer@*/const char *tz (const char *fname)
/*@observer@*/
const char *
tz(const char *path)
{
FILE *fp = NULL;
const char *result;
static char tzbuf[BUFSIZ];

fp = fopen (fname, "r");
if ( (NULL == fp)
|| (fgets_a(tzbuf, fp) == NULL)) {
result = "TZ=CST6CDT";
} else {
stpsep(tzbuf, "\n");
result = tzbuf;
}

if (NULL != fp) {
(void) fclose (fp);
}

return result;
FILE *fp;
static char buf[LINE_MAX + 1];

fp = fopen(path, "r");
if (fp == NULL)
return DEFAULT_TZ;

if (fgets_a(buf, fp) == NULL)
goto def;
if (stpsep(buf, "\n") == NULL)
goto def;

fclose(fp);
return buf;
def:
fclose(fp);
return DEFAULT_TZ;
}
#else /* !USE_PAM */
extern int ISO_C_forbids_an_empty_translation_unit;
#endif /* !USE_PAM */

4 changes: 2 additions & 2 deletions man/login.defs.d/ENV_TZ.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
If set, it will be used to define the TZ environment variable when
a user login. The value can be the name of a timezone preceded by
<replaceable>TZ=</replaceable> (for example
<replaceable>TZ=CST6CDT</replaceable>), or the full path to the file
<replaceable>TZ=UTC</replaceable>), or the full path to the file
containing the timezone specification (for example
<filename>/etc/tzname</filename>).
</para>
<!-- TODO: it can in fact be used to set any other variable-->
<para>
If a full path is specified but the file does not exist or cannot be
read, the default is to use <replaceable>TZ=CST6CDT</replaceable>.
read, the default is to use <replaceable>TZ=UTC</replaceable>.
</para>
</listitem>
</varlistentry>
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin
# If defined, either a TZ environment parameter spec or the
# fully-rooted pathname of a file containing such a spec.
#
#ENV_TZ TZ=CST6CDT
#ENV_TZ TZ=UTC
#ENV_TZ /etc/tzname

#
Expand Down
2 changes: 1 addition & 1 deletion tests/system/etc/login.defs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ HUSHLOGIN_FILE .hushlogin
# If defined, either a TZ environment parameter spec or the
# fully-rooted pathname of a file containing such a spec.
#
#ENV_TZ TZ=CST6CDT
#ENV_TZ TZ=UTC
#ENV_TZ /etc/tzname

#
Expand Down
Loading