vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| drop in parsemonth from freebsd. use it to parse month, so "cal june 2005" works as expected. if only one arg is given, see if it could be a month (without interfering if it's a year). there's plenty more cleanup to do, but this at least makes it a bit more convenient to use. Index: cal.c ================================================== ================= RCS file: /cvs/src/usr.bin/cal/cal.c,v retrieving revision 1.11 diff -u -r1.11 cal.c --- cal.c 2005/04/13 18:52:59 1.11 +++ cal.c 2005/06/28 03:00:53 @@ -159,12 +159,22 @@ month = 0; switch(argc) { case 2: - if ((month = atoi(*argv++)) < 1 || month > 12) - errx(1, "illegal month value: use 1-12"); + month = parsemonth(*argv++); + if (!month) + errx(1, "Unable to parse month"); /* FALLTHROUGH */ case 1: - if ((year = atoi(*argv)) < 1 || year > 9999) - errx(1, "illegal year value: use 1-9999"); + if (argc == 1 && !isdigit(*argv[0])) { + month = parsemonth(*argv); + if (!month) + errx(1, "illegal year value: use 1-9999"); + (void)time(&now); + local_time = localtime(&now); + year = local_time->tm_year + TM_YEAR_BASE; + } else { + if ((year = atoi(*argv)) < 1 || year > 9999) + errx(1, "illegal year value: use 1-9999"); + } break; case 0: (void)time(&now); @@ -416,4 +426,21 @@ (void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n"); exit(1); +} + +int +parsemonth(const char *s) +{ + int v; + char *cp; + struct tm tm; + + v = (int)strtol(s, &cp, 10); + if (cp != s) + return (v); + if (strptime(s, "%B", &tm) != NULL) + return (tm.tm_mon + 1); + if (strptime(s, "%b", &tm) != NULL) + return (tm.tm_mon + 1); + return (0); } -- And that's why I'm the best man and you're just a ring boy. |