~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to misc-utils/cal.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
#include <sys/types.h>
59
59
 
60
60
#include <ctype.h>
 
61
#include <getopt.h>
61
62
#include <stdio.h>
62
63
#include <stdlib.h>
63
64
#include <string.h>
64
65
#include <time.h>
65
66
#include <unistd.h>
66
 
#include <err.h>
67
67
#include <errno.h>
68
68
 
69
69
#include "c.h"
70
70
#include "nls.h"
71
71
#include "mbsalign.h"
 
72
#include "strutils.h"
72
73
 
73
74
#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
74
75
 
91
92
}
92
93
 
93
94
static const char *
94
 
my_tgetstr(char *s, char *ss) {
 
95
my_tgetstr(char *s __attribute__ ((__unused__)), char *ss) {
95
96
    const char* ret = tigetstr(ss);
96
97
    if (!ret || ret==(char*)-1)
97
 
        return "";
 
98
        return "";
98
99
    else
99
 
        return ret;
 
100
        return ret;
100
101
}
101
102
 
102
103
#elif defined(HAVE_LIBTERMCAP)
118
119
}
119
120
 
120
121
static const char *
121
 
my_tgetstr(char *s, char *ss) {
 
122
my_tgetstr(char *s, char *ss __attribute__ ((__unused__))) {
122
123
    const char* ret = tgetstr(s, &strbuf);
123
124
    if (!ret)
124
 
        return "";
 
125
        return "";
125
126
    else
126
 
        return ret;
 
127
        return ret;
127
128
}
128
129
 
129
130
#else /* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */
252
253
void monthly(int, int, int);
253
254
void monthly3(int, int, int);
254
255
void trim_trailing_spaces(char *);
255
 
void usage(void);
 
256
static void __attribute__ ((__noreturn__)) usage(FILE * out);
256
257
void headers_init(void);
257
258
 
258
259
int
262
263
        int ch, day, month, year, yflag;
263
264
        int num_months = NUM_MONTHS;
264
265
 
 
266
        static const struct option longopts[] = {
 
267
                {"one", no_argument, NULL, '1'},
 
268
                {"three", no_argument, NULL, '3'},
 
269
                {"sunday", no_argument, NULL, 's'},
 
270
                {"monday", no_argument, NULL, 'm'},
 
271
                {"julian", no_argument, NULL, 'j'},
 
272
                {"year", no_argument, NULL, 'y'},
 
273
                {"version", no_argument, NULL, 'V'},
 
274
                {"help", no_argument, NULL, 'h'},
 
275
                {NULL, 0, NULL, 0}
 
276
        };
 
277
 
265
278
        setlocale(LC_ALL, "");
266
279
        bindtextdomain(PACKAGE, LOCALEDIR);
267
280
        textdomain(PACKAGE);
290
303
         * This is needed to support first_weekday=2 and first_workday=1 for
291
304
         * the rare case where working days span across 2 weeks.
292
305
         * This shell script shows the combinations and calculations involved:
293
 
 
294
 
         for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
295
 
           printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
296
 
           date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
297
 
         done
298
 
 
299
 
         en_US:  19971130 + 1 -1 = 0  #0 = sunday
300
 
         ru_RU:  19971130 + 2 -1 = 1
301
 
         fr_FR:  19971201 + 1 -1 = 1
302
 
         csb_PL: 19971201 + 2 -1 = 2
303
 
         POSIX:  19971201 + 7 -1 = 0
 
306
         *
 
307
         * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
 
308
         *   printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
 
309
         *   date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
 
310
         * done
 
311
         *
 
312
         * en_US:  19971130 + 1 -1 = 0  #0 = sunday
 
313
         * ru_RU:  19971130 + 2 -1 = 1
 
314
         * fr_FR:  19971201 + 1 -1 = 1
 
315
         * csb_PL: 19971201 + 2 -1 = 2
 
316
         * POSIX:  19971201 + 7 -1 = 0
304
317
         */
305
318
        {
306
319
                int wfd;
314
327
#endif
315
328
 
316
329
        yflag = 0;
317
 
        while ((ch = getopt(argc, argv, "13mjsyV")) != -1)
 
330
        while ((ch = getopt_long(argc, argv, "13mjsyVh", longopts, NULL)) != -1)
318
331
                switch(ch) {
319
332
                case '1':
320
333
                        num_months = 1;         /* default */
338
351
                        printf(_("%s from %s\n"), program_invocation_short_name,
339
352
                               PACKAGE_STRING);
340
353
                        return EXIT_SUCCESS;
 
354
                case 'h':
 
355
                        usage(stdout);
341
356
                case '?':
342
357
                default:
343
 
                        usage();
 
358
                        usage(stderr);
344
359
                }
345
360
        argc -= optind;
346
361
        argv += optind;
351
366
        day = month = year = 0;
352
367
        switch(argc) {
353
368
        case 3:
354
 
                if ((day = atoi(*argv++)) < 1 || day > 31)
355
 
                        errx(1, _("illegal day value: use 1-%d"), 31);
 
369
                day = strtol_or_err(*argv++, _("illegal day value"));
 
370
                if (day < 1 || 31 < day)
 
371
                        errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), 31);
356
372
                /* FALLTHROUGH */
357
373
        case 2:
358
 
                if ((month = atoi(*argv++)) < 1 || month > 12)
359
 
                        errx(1, _("illegal month value: use 1-12"));
 
374
                month = strtol_or_err(*argv++, _("illegal month value: use 1-12"));
 
375
                if (month < 1 || 12 < month)
 
376
                        errx(EXIT_FAILURE, _("illegal month value: use 1-12"));
360
377
                /* FALLTHROUGH */
361
378
        case 1:
362
 
                if ((year = atoi(*argv)) < 1 || year > 9999)
363
 
                        errx(1, _("illegal year value: use 1-9999"));
 
379
                year = strtol_or_err(*argv++, _("illegal year value: use 1-9999"));
 
380
                if (year < 1 || 9999 < year)
 
381
                        errx(EXIT_FAILURE, _("illegal year value: use 1-9999"));
364
382
                if (day) {
365
383
                        int dm = days_in_month[leap_year(year)][month];
366
384
                        if (day > dm)
367
 
                                errx(1, _("illegal day value: use 1-%d"), dm);
 
385
                                errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
368
386
                        day = day_in_year(day, month, year);
369
387
                } else if ((local_time->tm_year + 1900) == year) {
370
388
                        day = local_time->tm_yday + 1;
378
396
                month = local_time->tm_mon + 1;
379
397
                break;
380
398
        default:
381
 
                usage();
 
399
                usage(stderr);
382
400
        }
383
401
        headers_init();
384
402
 
399
417
 
400
418
void headers_init(void)
401
419
{
402
 
  int i, wd;
403
 
  char *cur_dh = day_headings, *cur_j_dh = j_day_headings;
404
 
 
405
 
  strcpy(day_headings,"");
406
 
  strcpy(j_day_headings,"");
407
 
 
408
 
  for(i = 0 ; i < 7 ; i++ ) {
409
 
     ssize_t space_left;
410
 
     wd = (i + weekstart) % 7;
411
 
 
412
 
     if (i)
413
 
        strcat(cur_dh++, " ");
414
 
     space_left = sizeof(day_headings) - (cur_dh - day_headings);
415
 
     if(space_left <= 2)
416
 
        break;
417
 
     cur_dh += center_str(nl_langinfo(ABDAY_1+wd), cur_dh, space_left, 2);
418
 
 
419
 
     if (i)
420
 
        strcat(cur_j_dh++, " ");
421
 
     space_left = sizeof(j_day_headings) - (cur_j_dh - j_day_headings);
422
 
     if(space_left <= 3)
423
 
        break;
424
 
     cur_j_dh += center_str(nl_langinfo(ABDAY_1+wd), cur_j_dh, space_left, 3);
425
 
  }
426
 
 
427
 
  for (i = 0; i < 12; i++)
428
 
     full_month[i] = nl_langinfo(MON_1+i);
 
420
        int i, wd;
 
421
        char *cur_dh = day_headings, *cur_j_dh = j_day_headings;
 
422
 
 
423
        strcpy(day_headings, "");
 
424
        strcpy(j_day_headings, "");
 
425
 
 
426
        for (i = 0; i < 7; i++) {
 
427
                ssize_t space_left;
 
428
                wd = (i + weekstart) % 7;
 
429
 
 
430
                if (i)
 
431
                        strcat(cur_dh++, " ");
 
432
                space_left =
 
433
                    sizeof(day_headings) - (cur_dh - day_headings);
 
434
                if (space_left <= 2)
 
435
                        break;
 
436
                cur_dh +=
 
437
                    center_str(nl_langinfo(ABDAY_1 + wd), cur_dh,
 
438
                               space_left, 2);
 
439
 
 
440
                if (i)
 
441
                        strcat(cur_j_dh++, " ");
 
442
                space_left =
 
443
                    sizeof(j_day_headings) - (cur_j_dh - j_day_headings);
 
444
                if (space_left <= 3)
 
445
                        break;
 
446
                cur_j_dh +=
 
447
                    center_str(nl_langinfo(ABDAY_1 + wd), cur_j_dh,
 
448
                               space_left, 3);
 
449
        }
 
450
 
 
451
        for (i = 0; i < 12; i++)
 
452
                full_month[i] = nl_langinfo(MON_1 + i);
429
453
}
430
454
 
431
455
void
506
530
        do_monthly(day, month,      year,      &out_curm);
507
531
        do_monthly(day, next_month, next_year, &out_next);
508
532
 
509
 
        width = (julian ? J_WEEK_LEN : WEEK_LEN) -1;
 
533
        width = (julian ? J_WEEK_LEN : WEEK_LEN) -1;
510
534
        for (i = 0; i < 2; i++)
511
535
                printf("%s  %s  %s\n", out_prev.s[i], out_curm.s[i], out_next.s[i]);
512
536
        for (i = 2; i < FMT_ST_LINES; i++) {
514
538
                w1 = w2 = w3 = width;
515
539
 
516
540
#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
517
 
                /* adjust width to allow for non printable characters */
518
 
                w1 += (out_prev.s[i] == Hrow ? Slen : 0);
519
 
                w2 += (out_curm.s[i] == Hrow ? Slen : 0);
520
 
                w3 += (out_next.s[i] == Hrow ? Slen : 0);
 
541
                /* adjust width to allow for non printable characters */
 
542
                w1 += (out_prev.s[i] == Hrow ? Slen : 0);
 
543
                w2 += (out_curm.s[i] == Hrow ? Slen : 0);
 
544
                w3 += (out_next.s[i] == Hrow ? Slen : 0);
521
545
#endif
522
546
                snprintf(lineout, sizeof(lineout), "%-*s  %-*s  %-*s\n",
523
547
                       w1, out_prev.s[i],
757
781
                printf("%*s", separate, "");
758
782
}
759
783
 
760
 
void
761
 
usage()
 
784
static void __attribute__ ((__noreturn__)) usage(FILE * out)
762
785
{
763
 
 
764
 
        fprintf(stderr, _("usage: %s [-13smjyV] [[[day] month] year]\n"),
 
786
        fputs(_("\nUsage:\n"), out);
 
787
        fprintf(out,
 
788
              _(" %s [options] [[[day] month] year]\n"),
765
789
                        program_invocation_short_name);
766
 
        exit(EXIT_FAILURE);
 
790
 
 
791
        fputs(_("\nOptions:\n"), out);
 
792
        fputs(_(" -1, --one        show only current month (default)\n"
 
793
                " -3, --three      show previous, current and next month\n"
 
794
                " -s, --sunday     Sunday as first day of week\n"
 
795
                " -m, --monday     Monday as first day of week\n"
 
796
                " -j, --julian     output Julian dates\n"
 
797
                " -y, --year       show whole current year\n"
 
798
                " -V, --version    display version information and exit\n"
 
799
                " -h, --help       display this help text and exit\n\n"), out);
 
800
 
 
801
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
767
802
}