Unix Technical Forum

cal diff

This is a discussion on cal diff within the mailing.openbsd.tech forums, part of the OpenBSD category; --> drop in parsemonth from freebsd. use it to parse month, so "cal june 2005" works as expected. if only ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > mailing.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-18-2008, 08:55 AM
Ted Unangst
 
Posts: n/a
Default cal diff

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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:10 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com