]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/cal.c
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 /* 1999-02-01 Jean-Francois Bignolles: added option '-m' to display
38 * monday as the first day of the week.
39 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
40 * - added Native Language Support
42 * 2000-09-01 Michael Charles Pruznick <dummy@netwiz.net>
43 * Added "-3" option to print prev/next month with current.
44 * Added over-ridable default NUM_MONTHS and "-1" option to
45 * get traditional output when -3 is the default. I hope that
46 * enough people will like -3 as the default that one day the
47 * product can be shipped that way.
49 * 2001-05-07 Pablo Saratxaga <pablo@mandrakesoft.com>
50 * Fixed the bugs with multi-byte charset (zg: cjk, utf-8)
51 * displaying. made the 'month year' ("%s %d") header translatable
52 * so it can be adapted to conventions used by different languages
53 * added support to read "first_weekday" locale information
54 * still to do: support for 'cal_direction' (will require a major
55 * rewrite of the displaying) and proper handling of RTL scripts
58 #include <sys/types.h>
70 #if defined(HAVE_NCURSES)
74 #elif defined(HAVE_NCURSES_NCURSES_H)
75 #include <ncurses/ncurses.h>
78 #include <term.h> /* include after <curses.h> */
81 my_setupterm(const char *term
, int fildes
, int *errret
) {
82 setupterm((char*)term
, fildes
, errret
);
86 my_putstring(char *s
) {
91 my_tgetstr(char *s
, char *ss
) {
92 const char* ret
= tigetstr(ss
);
93 if (!ret
|| ret
==(char*)-1)
99 #elif defined(HAVE_LIBTERMCAP)
103 char termbuffer
[4096];
105 char *strbuf
= termbuffer
;
108 my_setupterm(const char *term
, int fildes
, int *errret
) {
109 *errret
= tgetent(tcbuffer
, term
);
113 my_putstring(char *s
) {
114 tputs (s
, 1, putchar
);
118 my_tgetstr(char *s
, char *ss
) {
119 const char* ret
= tgetstr(s
, &strbuf
);
126 #else /* ! (HAVE_LIBTERMCAP || HAVE_NCURSES) */
129 my_putstring(char *s
) {
137 const char *Senter
="", *Sexit
="";/* enter and exit standout mode */
138 int Slen
; /* strlen of Senter+Sexit */
139 char *Hrow
; /* pointer to highlighted row in month */
141 #ifdef HAVE_LANGINFO_H
142 # include <langinfo.h>
144 # include <localeinfo.h> /* libc4 only */
147 #include "widechar.h"
149 #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
151 /* allow compile-time define to over-ride default */
156 #if ( NUM_MONTHS != 1 && NUM_MONTHS !=3 )
157 #error NUM_MONTHS must be 1 or 3
160 #define THURSDAY 4 /* for reformation */
161 #define SATURDAY 6 /* 1 Jan 1 was a Saturday */
163 #define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
164 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
166 #define MAXDAYS 43 /* max slots in a month array */
167 #define SPACE -1 /* used in day array */
169 static int days_in_month
[2][13] = {
170 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
171 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
174 int sep1752
[MAXDAYS
] = {
175 SPACE
, SPACE
, 1, 2, 14, 15, 16,
176 17, 18, 19, 20, 21, 22, 23,
177 24, 25, 26, 27, 28, 29, 30,
178 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
179 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
180 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
182 }, j_sep1752
[MAXDAYS
] = {
183 SPACE
, SPACE
, 245, 246, 258, 259, 260,
184 261, 262, 263, 264, 265, 266, 267,
185 268, 269, 270, 271, 272, 273, 274,
186 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
187 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
188 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
190 }, empty
[MAXDAYS
] = {
191 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
192 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
193 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
194 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
195 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
196 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
200 #define DAY_LEN 3 /* 3 spaces per day */
201 #define J_DAY_LEN 4 /* 4 spaces per day */
202 #define WEEK_LEN 21 /* 7 days * 3 characters */
203 #define J_WEEK_LEN 28 /* 7 days * 4 characters */
204 #define HEAD_SEP 2 /* spaces between day headings */
207 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
208 char day_headings
[WEEK_LEN
*6+1];
209 /* week1stday = 1 => " M Tu W Th F S S " */
210 char j_day_headings
[J_WEEK_LEN
*6+1];
211 /* week1stday = 1 => " M Tu W Th F S S " */
212 const char *full_month
[12];
214 /* leap year -- account for gregorian reformation in 1752 */
215 #define leap_year(yr) \
216 ((yr) <= 1752 ? !((yr) % 4) : \
217 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
219 /* number of centuries since 1700, not inclusive */
220 #define centuries_since_1700(yr) \
221 ((yr) > 1700 ? (yr) / 100 - 17 : 0)
223 /* number of centuries since 1700 whose modulo of 400 is 0 */
224 #define quad_centuries_since_1700(yr) \
225 ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
227 /* number of leap years between year 1 and this year, not inclusive */
228 #define leap_years_since_year_1(yr) \
229 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
231 /* 0 => sunday (default), 1 => monday */
235 #define TODAY_FLAG 0x400 /* flag day for highlighting */
237 #define FMT_ST_LINES 8
238 #define FMT_ST_CHARS 300 /* 90 suffices in most locales */
241 char s
[FMT_ST_LINES
][FMT_ST_CHARS
];
244 char * ascii_day(char *, int);
245 void center_str(const char* src
, char* dest
, size_t dest_size
, int width
);
246 void center(const char *, int, int);
247 void day_array(int, int, int, int *);
248 int day_in_week(int, int, int);
249 int day_in_year(int, int, int);
250 void yearly(int, int);
251 void j_yearly(int, int);
252 void do_monthly(int, int, int, struct fmt_st
*);
253 void monthly(int, int, int);
254 void monthly3(int, int, int);
255 void trim_trailing_spaces(char *);
257 void headers_init(void);
258 extern char *__progname
;
261 main(int argc
, char **argv
) {
262 struct tm
*local_time
;
264 int ch
, day
, month
, year
, yflag
;
266 int num_months
= NUM_MONTHS
;
269 if ((p
= strrchr(progname
, '/')) != NULL
)
271 __progname
= progname
;
273 setlocale(LC_ALL
, "");
274 bindtextdomain(PACKAGE
, LOCALEDIR
);
277 #if defined(HAVE_NCURSES) || defined(HAVE_LIBTERMCAP)
278 if ((term
= getenv("TERM"))) {
280 my_setupterm(term
, 1, &ret
);
282 Senter
= my_tgetstr("so","smso");
283 Sexit
= my_tgetstr("se","rmso");
284 Slen
= strlen(Senter
) + strlen(Sexit
);
289 #if 0 /* setting week1stday is against man page */
291 * What *is* the first day of the week? Note that glibc does not
292 * provide any information today, it (almost) always answers Monday.
293 * Sunday is the Jewish and Christian tradition.
294 * Sometimes an answer is built into the language:
295 * German calls Wednesday "Mittwoch", so starts at Sunday.
296 * Portuguese calls Monday "segunda-feira", so starts at Sunday.
297 * Russian calls Friday "pyatnitsa", so starts at Monday.
298 * ISO 8601 decided to start at Monday.
300 * The traditional Unix cal utility starts at Sunday.
301 * We start at Sunday and have an option -m for starting at Monday.
303 * At some future time this may become -s for Sunday, -m for Monday,
304 * no option for glibc-determined locale-dependent version.
306 #ifdef HAVE_LANGINFO_H
307 week1stday
= (int)(nl_langinfo(_NL_TIME_FIRST_WEEKDAY
))[0];
312 while ((ch
= getopt(argc
, argv
, "13mjsyV")) != -1)
315 num_months
= 1; /* default */
321 week1stday
= 0; /* default */
333 printf(_("%s from %s\n"),
334 progname
, PACKAGE_STRING
);
343 day
= month
= year
= 0;
346 if ((month
= atoi(*argv
++)) < 1 || month
> 12)
347 errx(1, _("illegal month value: use 1-12"));
350 if ((year
= atoi(*argv
)) < 1 || year
> 9999)
351 errx(1, _("illegal year value: use 1-9999"));
356 char *e
= getenv("TEST_TIME");
358 if (e
&& isdigit((unsigned char) *e
))
364 local_time
= localtime(&now
);
366 day
= local_time
->tm_yday
+ 1;
367 year
= local_time
->tm_year
+ 1900;
369 month
= local_time
->tm_mon
+ 1;
376 if (month
&& num_months
== 1)
377 monthly(day
, month
, year
);
378 else if (month
&& num_months
== 3)
379 monthly3(day
, month
, year
);
387 #ifndef HAVE_WIDECHAR
388 static char *eos(char *s
) {
395 void headers_init(void)
399 wchar_t day_headings_wc
[22],j_day_headings_wc
[29];
400 char *cur_dh
= day_headings
, *cur_j_dh
= j_day_headings
;
402 wcscpy(day_headings_wc
, L
"");
403 wcscpy(j_day_headings_wc
, L
"");
406 strcpy(day_headings
,"");
407 strcpy(j_day_headings
,"");
409 #ifdef HAVE_LANGINFO_H
410 # define weekday(wd) nl_langinfo(ABDAY_1+wd)
412 # define weekday(wd) _time_info->abbrev_wkday[wd]
415 for(i
= 0 ; i
< 7 ; i
++ ) {
417 wd
= (i
+ week1stday
) % 7;
419 swprintf(day_headings_wc
, SIZE(day_headings_wc
), L
"%1.2s ", weekday(wd
));
420 swprintf(j_day_headings_wc
, SIZE(j_day_headings_wc
), L
"%3.3s ", weekday(wd
));
422 space_left
= sizeof(day_headings
) - (cur_dh
- day_headings
);
425 cur_dh
+= wcstombs(cur_dh
, day_headings_wc
, space_left
);
427 space_left
= sizeof(j_day_headings
)-(cur_j_dh
-j_day_headings
);
430 cur_j_dh
+= wcstombs(cur_j_dh
,j_day_headings_wc
, space_left
);
432 sprintf(eos(day_headings
), "%2.2s ", weekday(wd
));
433 sprintf(eos(j_day_headings
), "%3.3s ", weekday(wd
));
437 trim_trailing_spaces(day_headings
);
438 trim_trailing_spaces(j_day_headings
);
441 for (i
= 0; i
< 12; i
++) {
442 #ifdef HAVE_LANGINFO_H
443 full_month
[i
] = nl_langinfo(MON_1
+i
);
445 full_month
[i
] = _time_info
->full_month
[i
];
451 do_monthly(int day
, int month
, int year
, struct fmt_st
*out
) {
452 int col
, row
, len
, days
[MAXDAYS
];
453 char *p
, lineout
[FMT_ST_CHARS
];
454 int width
= (julian
? J_WEEK_LEN
: WEEK_LEN
) - 1;
456 day_array(day
, month
, year
, days
);
459 * %s is the month name, %d the year number.
460 * you can change the order and/or add something here; eg for
461 * Basque the translation should be: "%2$dko %1$s", and
462 * the Vietnamese should be "%s na(m %d", etc.
464 len
= sprintf(lineout
, _("%s %d"), full_month
[month
- 1], year
);
465 center_str(lineout
, out
->s
[0], SIZE(out
->s
[0]), width
);
467 sprintf(out
->s
[1],"%s",
468 julian
? j_day_headings
: day_headings
);
469 for (row
= 0; row
< 6; row
++) {
471 for (col
= 0, p
= lineout
; col
< 7; col
++) {
472 int xd
= days
[row
* 7 + col
];
473 if (xd
!= SPACE
&& (xd
& TODAY_FLAG
))
475 p
= ascii_day(p
, xd
);
478 trim_trailing_spaces(lineout
);
479 sprintf(out
->s
[row
+2], "%s", lineout
);
481 Hrow
= out
->s
[row
+2];
486 monthly(int day
, int month
, int year
) {
490 do_monthly(day
, month
, year
, &out
);
491 for (i
= 0; i
< FMT_ST_LINES
; i
++) {
492 my_putstring(out
.s
[i
]);
498 monthly3(int day
, int month
, int year
) {
499 char lineout
[FMT_ST_CHARS
];
502 struct fmt_st out_prev
;
503 struct fmt_st out_curm
;
504 struct fmt_st out_next
;
505 int prev_month
, prev_year
;
506 int next_month
, next_year
;
510 prev_year
= year
- 1;
512 prev_month
= month
- 1;
517 next_year
= year
+ 1;
519 next_month
= month
+ 1;
523 do_monthly(day
, prev_month
, prev_year
, &out_prev
);
524 do_monthly(day
, month
, year
, &out_curm
);
525 do_monthly(day
, next_month
, next_year
, &out_next
);
527 width
= (julian
? J_WEEK_LEN
: WEEK_LEN
) -1;
528 for (i
= 0; i
< 2; i
++)
529 printf("%s %s %s\n", out_prev
.s
[i
], out_curm
.s
[i
], out_next
.s
[i
]);
530 for (i
= 2; i
< FMT_ST_LINES
; i
++) {
532 w1
= w2
= w3
= width
;
534 #if defined(HAVE_NCURSES) || defined(HAVE_LIBTERMCAP)
535 /* adjust width to allow for non printable characters */
536 w1
+= (out_prev
.s
[i
] == Hrow
? Slen
: 0);
537 w2
+= (out_curm
.s
[i
] == Hrow
? Slen
: 0);
538 w3
+= (out_next
.s
[i
] == Hrow
? Slen
: 0);
540 snprintf(lineout
, SIZE(lineout
), "%-*s %-*s %-*s\n",
545 my_putstring(lineout
);
550 j_yearly(int day
, int year
) {
551 int col
, *dp
, i
, month
, row
, which_cal
;
552 int days
[12][MAXDAYS
];
553 char *p
, lineout
[80];
555 sprintf(lineout
, "%d", year
);
556 center(lineout
, J_WEEK_LEN
* 2 + J_HEAD_SEP
, 0);
559 for (i
= 0; i
< 12; i
++)
560 day_array(day
, i
+ 1, year
, days
[i
]);
561 memset(lineout
, ' ', sizeof(lineout
) - 1);
562 lineout
[sizeof(lineout
) - 1] = '\0';
563 for (month
= 0; month
< 12; month
+= 2) {
564 center(full_month
[month
], J_WEEK_LEN
, J_HEAD_SEP
);
565 center(full_month
[month
+ 1], J_WEEK_LEN
, 0);
566 printf("\n%s%*s %s\n", j_day_headings
, J_HEAD_SEP
, "",
568 for (row
= 0; row
< 6; row
++) {
570 for (which_cal
= 0; which_cal
< 2; which_cal
++) {
571 dp
= &days
[month
+ which_cal
][row
* 7];
572 for (col
= 0; col
< 7; col
++)
573 p
= ascii_day(p
, *dp
++);
574 p
+= sprintf(p
, " ");
577 trim_trailing_spaces(lineout
);
578 my_putstring(lineout
);
586 yearly(int day
, int year
) {
587 int col
, *dp
, i
, month
, row
, which_cal
;
588 int days
[12][MAXDAYS
];
589 char *p
, lineout
[100];
591 sprintf(lineout
, "%d", year
);
592 center(lineout
, WEEK_LEN
* 3 + HEAD_SEP
* 2, 0);
595 for (i
= 0; i
< 12; i
++)
596 day_array(day
, i
+ 1, year
, days
[i
]);
597 memset(lineout
, ' ', sizeof(lineout
) - 1);
598 lineout
[sizeof(lineout
) - 1] = '\0';
599 for (month
= 0; month
< 12; month
+= 3) {
600 center(full_month
[month
], WEEK_LEN
, HEAD_SEP
);
601 center(full_month
[month
+ 1], WEEK_LEN
, HEAD_SEP
);
602 center(full_month
[month
+ 2], WEEK_LEN
, 0);
603 printf("\n%s%*s %s%*s %s\n", day_headings
, HEAD_SEP
,
604 "", day_headings
, HEAD_SEP
, "", day_headings
);
605 for (row
= 0; row
< 6; row
++) {
607 for (which_cal
= 0; which_cal
< 3; which_cal
++) {
608 dp
= &days
[month
+ which_cal
][row
* 7];
609 for (col
= 0; col
< 7; col
++)
610 p
= ascii_day(p
, *dp
++);
611 p
+= sprintf(p
, " ");
614 trim_trailing_spaces(lineout
);
615 my_putstring(lineout
);
624 * Fill in an array of 42 integers with a calendar. Assume for a moment
625 * that you took the (maximum) 6 rows in a calendar and stretched them
626 * out end to end. You would have 42 numbers or spaces. This routine
627 * builds that array for any month from Jan. 1 through Dec. 9999.
630 day_array(int day
, int month
, int year
, int *days
) {
631 int julday
, daynum
, dw
, dm
;
634 if (month
== 9 && year
== 1752) {
635 d_sep1752
= julian
? j_sep1752
: sep1752
;
636 memcpy(days
, d_sep1752
+ week1stday
, MAXDAYS
* sizeof(int));
639 memcpy(days
, empty
, MAXDAYS
* sizeof(int));
640 dm
= days_in_month
[leap_year(year
)][month
];
641 dw
= (day_in_week(1, month
, year
) - week1stday
+ 7) % 7;
642 julday
= day_in_year(1, month
, year
);
643 daynum
= julian
? julday
: 1;
647 days
[dw
] |= TODAY_FLAG
;
654 * return the 1 based day number within the year
657 day_in_year(int day
, int month
, int year
) {
660 leap
= leap_year(year
);
661 for (i
= 1; i
< month
; i
++)
662 day
+= days_in_month
[leap
][i
];
668 * return the 0 based day number for any date from 1 Jan. 1 to
669 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
670 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
674 day_in_week(int day
, int month
, int year
) {
677 temp
= (long)(year
- 1) * 365 + leap_years_since_year_1(year
- 1)
678 + day_in_year(day
, month
, year
);
679 if (temp
< FIRST_MISSING_DAY
)
680 return ((temp
- 1 + SATURDAY
) % 7);
681 if (temp
>= (FIRST_MISSING_DAY
+ NUMBER_MISSING_DAYS
))
682 return (((temp
- 1 + SATURDAY
) - NUMBER_MISSING_DAYS
) % 7);
687 ascii_day(char *p
, int day
) {
690 static char *aday
[] = {
692 " 1", " 2", " 3", " 4", " 5", " 6", " 7",
693 " 8", " 9", "10", "11", "12", "13", "14",
694 "15", "16", "17", "18", "19", "20", "21",
695 "22", "23", "24", "25", "26", "27", "28",
700 int len
= julian
? J_DAY_LEN
: DAY_LEN
;
704 if (day
& TODAY_FLAG
) {
706 p
+= sprintf(p
, Senter
);
710 if ((val
= day
/ 100)) {
723 *p
++ = day
% 10 + '0';
729 p
+= sprintf(p
, Sexit
);
735 trim_trailing_spaces(s
)
742 while (p
> s
&& isspace(*--p
))
750 * Center string, handling multibyte characters appropriately.
751 * In addition if the string is too large for the width it's truncated.
754 center_str(const char* src
, char* dest
, size_t dest_size
, int width
)
757 wchar_t str_wc
[FMT_ST_CHARS
];
759 char str
[FMT_ST_CHARS
];
760 const char* str_to_print
=src
;
761 int len
, spaces
, wide_char_enabled
=0;
766 if (mbstowcs(str_wc
, src
, FMT_ST_CHARS
) > 0) {
767 wide_char_enabled
= 1;
768 len
= wcswidth(str_wc
, SIZE(str_wc
));
773 if (wide_char_enabled
) {
776 wcstombs(str
, str_wc
, SIZE(str
));
779 strncpy(str
, src
, SIZE(str
));
784 spaces
= width
- len
;
785 spaces
= ( spaces
< 0 ? 0 : spaces
);
787 snprintf(dest
, dest_size
, "%*s%s%*s",
790 spaces
/ 2 + spaces
% 2, "" );
794 center(str
, len
, separate
)
799 char lineout
[FMT_ST_CHARS
];
800 center_str(str
, lineout
, SIZE(lineout
), len
);
801 fputs(lineout
, stdout
);
803 (void)printf("%*s", separate
, "");
810 (void)fprintf(stderr
, _("usage: cal [-13smjyV] [[month] year]\n"));