]> err.no Git - util-linux/commitdiff
cal: fix first day of the week calculation on BE systems
authorKarel Zak <kzak@redhat.com>
Thu, 11 Feb 2010 15:29:05 +0000 (16:29 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 11 Feb 2010 15:29:05 +0000 (16:29 +0100)
This reverts commit dcb54fafb128ab41772ae217afc6a7612e2cc446,
"cal: remove gcc-ism from nl_langinfo() call".

The code:

int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);

does not work on big-endian machines. The original solution based on
union is better.

Note that the "type punning" is not gcc-ism any more, it's allowed
by C99 (6.5.2.3).

Reported-by: Joseph Jezak <josejx@gentoo.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/cal.c

index 5444ceafcf3d66ef5d48cee2ac9e4a143ff4f65d..5eb14b5574113175398448cdf06b9adf34919138 100644 (file)
@@ -312,7 +312,11 @@ main(int argc, char **argv) {
         POSIX:  19971201 + 7 -1 = 0
         */
        {
-               int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
+               int wfd;
+               union { unsigned int word; char *string; } val;
+               val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
+
+               wfd = val.word;
                wfd = day_in_week(wfd % 100, (wfd / 100) % 100, wfd / (100 * 100));
                weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
        }