From owner-freebsd-bugs@FreeBSD.ORG Thu Dec 13 10:00:06 2007 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DBE7516A418 for ; Thu, 13 Dec 2007 10:00:06 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id C1B4613C45A for ; Thu, 13 Dec 2007 10:00:06 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id lBDA06m2086799 for ; Thu, 13 Dec 2007 10:00:06 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id lBDA06bQ086798; Thu, 13 Dec 2007 10:00:06 GMT (envelope-from gnats) Resent-Date: Thu, 13 Dec 2007 10:00:06 GMT Resent-Message-Id: <200712131000.lBDA06bQ086798@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Edwin Groothuis Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AF9D816A46E for ; Thu, 13 Dec 2007 09:59:43 +0000 (UTC) (envelope-from edwin@mavetju.org) Received: from mail5out.barnet.com.au (mail5.barnet.com.au [202.83.178.78]) by mx1.freebsd.org (Postfix) with ESMTP id E90DA13C46E for ; Thu, 13 Dec 2007 09:59:42 +0000 (UTC) (envelope-from edwin@mavetju.org) Received: by mail5out.barnet.com.au (Postfix, from userid 1001) id 68948221882A; Thu, 13 Dec 2007 20:59:41 +1100 (EST) Received: from mail5auth.barnet.com.au (mail5.barnet.com.au [202.83.178.78]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mail5auth.barnet.com.au", Issuer "*.barnet.com.au" (verified OK)) by mail5.barnet.com.au (Postfix) with ESMTP id 183AD21B176E for ; Thu, 13 Dec 2007 20:59:41 +1100 (EST) Received: from k7.mavetju (k7.mavetju.org [10.251.1.18]) by mail5auth.barnet.com.au (Postfix) with ESMTP id 987352218805 for ; Thu, 13 Dec 2007 20:59:39 +1100 (EST) Received: by k7.mavetju (Postfix, from userid 1001) id 43DD912C; Thu, 13 Dec 2007 20:59:39 +1100 (EST) Message-Id: <20071213095939.43DD912C@k7.mavetju> Date: Thu, 13 Dec 2007 20:59:39 +1100 (EST) From: Edwin Groothuis To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: bin/118644: [patch] style(9)ify src/usr.bin/calendar X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Edwin Groothuis List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Dec 2007 10:00:06 -0000 >Number: 118644 >Category: bin >Synopsis: [patch] style(9)ify src/usr.bin/calendar >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu Dec 13 10:00:06 UTC 2007 >Closed-Date: >Last-Modified: >Originator: Edwin Groothuis >Release: FreeBSD 6.2-RELEASE-p4 i386 >Organization: - >Environment: System: FreeBSD k7.mavetju 6.2-RELEASE-p4 FreeBSD 6.2-RELEASE-p4 #0: Thu Apr 26 17:55:55 UTC 2007 root@i386-builder.daemonology.net:/usr/obj/usr/src/sys/SMP i386 On request of grog@, cleanup and style(9)ify src/usr.bin/calendar while we are at it. >Description: >How-To-Repeat: >Fix: Index: calendar.c =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/calendar.c,v retrieving revision 1.19 diff -u -r1.19 calendar.c --- calendar.c 7 May 2007 11:18:30 -0000 1.19 +++ calendar.c 13 Dec 2007 09:57:41 -0000 @@ -113,6 +113,7 @@ default: usage(); } + argc -= optind; argv += optind; @@ -121,7 +122,7 @@ /* use current time */ if (f_time <= 0) - (void)time(&f_time); + (void)time(&f_time); settime(f_time); @@ -143,11 +144,10 @@ void usage(void) { - (void)fprintf(stderr, "%s\n%s\n", + + fprintf(stderr, "%s\n%s\n", "usage: calendar [-a] [-A days] [-B days] [-F friday] " "[-f calendarfile]", " [-t dd[.mm[.year]]] [-W days]"); exit(1); } - - Index: calendar.h =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/calendar.h,v retrieving revision 1.12 diff -u -r1.12 calendar.h --- calendar.h 9 Jun 2007 05:54:13 -0000 1.12 +++ calendar.h 13 Dec 2007 09:57:41 -0000 @@ -77,17 +77,3 @@ char *name; int len; }; - -struct event *event_add(struct event *events, int month, int day, char *date, - int var, char *txt); -void event_continue(struct event *events, char *txt); -void event_print_all(FILE *fp, struct event *events); -/* Stored calendar event */ -struct event { - int month; - int day; - int var; - char *date; - char *text; - struct event *next; -}; Index: day.c =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/day.c,v retrieving revision 1.27 diff -u -r1.27 day.c --- day.c 9 Jun 2007 05:54:13 -0000 1.27 +++ day.c 13 Dec 2007 09:57:41 -0000 @@ -174,36 +174,36 @@ time_t Mktime (char *dp) { - time_t t; - int d, m, y; - struct tm tm; - - (void)time(&t); - tp = localtime(&t); - - tm = tm0; - tm.tm_mday = tp->tm_mday; - tm.tm_mon = tp->tm_mon; - tm.tm_year = tp->tm_year; - - switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) { - case 3: - if (y > 1900) - y -= 1900; - tm.tm_year = y; - /* FALLTHROUGH */ - case 2: - tm.tm_mon = m - 1; - /* FALLTHROUGH */ - case 1: - tm.tm_mday = d; - } + time_t t; + int d, m, y; + struct tm tm; + + (void)time(&t); + tp = localtime(&t); + + tm = tm0; + tm.tm_mday = tp->tm_mday; + tm.tm_mon = tp->tm_mon; + tm.tm_year = tp->tm_year; + + switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) { + case 3: + if (y > 1900) + y -= 1900; + tm.tm_year = y; + /* FALLTHROUGH */ + case 2: + tm.tm_mon = m - 1; + /* FALLTHROUGH */ + case 1: + tm.tm_mday = d; + } #ifdef DEBUG - fprintf(stderr, "Mktime: %d %d %s\n", (int)mktime(&tm), (int)t, - asctime(&tm)); + fprintf(stderr, "Mktime: %d %d %s\n", + (int)mktime(&tm), (int)t, asctime(&tm)); #endif - return(mktime(&tm)); + return(mktime(&tm)); } /* @@ -299,85 +299,87 @@ */ if (flags & F_ISDAY) { #ifdef DEBUG - fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month); + fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month); #endif - *varp = 1; - /* variable weekday, SundayLast, MondayFirst ... */ - if (day < 0 || day >= 10) { - - /* negative offset; last, -4 .. -1 */ - if (day < 0) { - v1 = day/10 - 1; /* offset -4 ... -1 */ - day = 10 + (day % 10); /* day 1 ... 7 */ - - /* day, eg '22nd' */ - v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); - - /* (month length - day) / 7 + 1 */ - if (cumdays[month+1] - cumdays[month] >= v2 - && ((int)((cumdays[month+1] - - cumdays[month] - v2) / 7) + 1) == -v1) - /* bingo ! */ - day = v2; - - /* set to yesterday */ - else { - day = tp->tm_mday - 1; - if (day == 0) - return (0); - } + *varp = 1; + /* variable weekday, SundayLast, MondayFirst ... */ + if (day < 0 || day >= 10) { + + /* negative offset; last, -4 .. -1 */ + if (day < 0) { + v1 = day/10 - 1; /* offset -4 ... -1 */ + day = 10 + (day % 10); /* day 1 ... 7 */ + + /* day, eg '22nd' */ + v2 = tp->tm_mday + + (((day - 1) - tp->tm_wday + 7) % 7); + + /* (month length - day) / 7 + 1 */ + if (cumdays[month+1] - cumdays[month] >= v2 + && ((int)((cumdays[month+1] - + cumdays[month] - v2) / 7) + 1) == -v1) + day = v2; /* bingo ! */ + + /* set to yesterday */ + else { + day = tp->tm_mday - 1; + if (day == 0) + return (0); + } + } + + /* first, second ... +1 ... +5 */ + else { + /* offset: +1 (first Sunday) ... */ + v1 = day/10; + day = day % 10; + + /* day, eg '22th' */ + v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); + + /* Hurrah! matched */ + if ( ((v2 - 1 + 7) / 7) == v1 ) + day = v2; + + else { + /* set to yesterday */ + day = tp->tm_mday - 1; + if (day == 0) + return (0); + } + } } - /* first, second ... +1 ... +5 */ else { - v1 = day/10; /* offset: +1 (first Sunday) ... */ - day = day % 10; - - /* day, eg '22th' */ - v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); - - /* Hurrah! matched */ - if ( ((v2 - 1 + 7) / 7) == v1 ) - day = v2; - - /* set to yesterday */ - else { - day = tp->tm_mday - 1; - if (day == 0) - return (0); - } + /* wired */ + day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); + *varp = 1; } - } - - /* wired */ - else { - day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); - *varp = 1; - } } if (!(flags & F_EASTER)) { - if (day + cumdays[month] > cumdays[month + 1]) { /* off end of month */ - day -= (cumdays[month + 1] - cumdays[month]); /* adjust */ - if (++month > 12) /* next year */ - month = 1; - } - *monthp = month; - *dayp = day; - day = cumdays[month] + day; - } - else { - for (v1 = 0; day > cumdays[v1]; v1++) - ; - *monthp = v1 - 1; - *dayp = day - cumdays[v1 - 1]; - *varp = 1; + if (day + cumdays[month] > cumdays[month + 1]) { + /* off end of month, adjust */ + day -= (cumdays[month + 1] - cumdays[month]); + /* next year */ + if (++month > 12) + month = 1; + } + *monthp = month; + *dayp = day; + day = cumdays[month] + day; + } else { + for (v1 = 0; day > cumdays[v1]; v1++) + ; + *monthp = v1 - 1; + *dayp = day - cumdays[v1 - 1]; + *varp = 1; } #ifdef DEBUG - fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day, - cumdays[month], tp->tm_yday); + fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", + *dayp, day, cumdays[month], tp->tm_yday); #endif /* When days before or days after is specified */ @@ -451,38 +453,34 @@ { int offs; - offs = strlen(s); - /* Sun+1 or Wednesday-2 * ^ ^ */ /* fprintf(stderr, "x: %s %s %d\n", s, s + offs - 2, offs); */ switch(*(s + offs - 2)) { case '-': - return(-(atoi(s + offs - 1))); + return(-(atoi(s + offs - 1))); case '+': - return(atoi(s + offs - 1)); + return(atoi(s + offs - 1)); } - /* * some aliases: last, first, second, third, fourth */ /* last */ if (offs > 4 && !strcasecmp(s + offs - 4, "last")) - return(-1); + return(-1); else if (offs > 5 && !strcasecmp(s + offs - 5, "first")) - return(+1); + return(+1); else if (offs > 6 && !strcasecmp(s + offs - 6, "second")) - return(+2); + return(+2); else if (offs > 5 && !strcasecmp(s + offs - 5, "third")) - return(+3); + return(+3); else if (offs > 6 && !strcasecmp(s + offs - 6, "fourth")) - return(+4); - + return(+4); /* no offset detected */ return(0); Index: io.c =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/io.c,v retrieving revision 1.22 diff -u -r1.22 io.c --- io.c 30 Oct 2007 03:44:09 -0000 1.22 +++ io.c 13 Dec 2007 09:57:42 -0000 @@ -67,9 +67,9 @@ #include "calendar.h" -const char *calendarFile = "calendar"; /* default calendar file */ +const char *calendarFile = "calendar"; /* default calendar file */ const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */ -const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */ +const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */ struct fixs neaster, npaskha; @@ -83,6 +83,23 @@ {"'s Calendar\nPrecedence: bulk\n\n", 30}, }; +/* + * Event sorting related functions: + * - Use event_add() to create a new event + * - Use event_continue() to add more text to the last added event + * - Use event_print_all() to display them in time chronological order + */ +struct event *event_add(struct event *, int, int, char *, int, char *); +void event_continue(struct event *events, char *txt); +void event_print_all(FILE *fp, struct event *events); +struct event { + int month; + int day; + int var; + char *date; + char *text; + struct event *next; +}; void cal(void) @@ -145,7 +162,8 @@ char dbuf[80]; if (d_first < 0) - d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); + d_first = + (*nl_langinfo(D_MD_ORDER) == 'd'); tm.tm_sec = 0; /* unused */ tm.tm_min = 0; /* unused */ tm.tm_hour = 0; /* unused */ @@ -156,25 +174,32 @@ (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); - events = event_add(events, month, day, dbuf, var, p); + events = event_add(events, month, day, dbuf, + var, p); } + } else { + if (printing) + event_continue(events, buf); } - else if (printing) - event_continue(events, buf); } event_print_all(fp, events); closecal(fp); } -/* - * Functions to handle buffered calendar events. - */ struct event * -event_add(struct event *events, int month, int day, char *date, int var, char *txt) +event_add(struct event *events, int month, int day, + char *date, int var, char *txt) { struct event *e; + /* + * Creating a new event: + * - Create a new event + * - Copy the machine readable day and month + * - Copy the human readable and language specific date + * - Copy the text of the event + */ e = (struct event *)calloc(1, sizeof(struct event)); if (e == NULL) errx(1, "event_add: cannot allocate memory"); @@ -197,6 +222,13 @@ { char *text; + /* + * Adding text to the event: + * - Save a copy of the old text (unknown length, so strdup()) + * - Allocate enough space for old text + \n + new text + 0 + * - Store the old text + \n + new text + * - Destroy the saved copy. + */ text = strdup(e->text); if (text == NULL) errx(1, "event_continue: cannot allocate memory"); @@ -217,15 +249,28 @@ event_print_all(FILE *fp, struct event *events) { struct event *e, *e_next; - int daycount = f_dayAfter + f_dayBefore; int daycounter; int day, month; - for (daycounter = 0; daycounter <= daycount; daycounter++) { + /* + * Print all events: + * - We know the number of days to be counted (f_dayAfter + f_dayBefore) + * - We know the current day of the year ("now" - f_dayBefore + counter) + * - We know the number of days in the year (yrdays, set in settime()) + * - So we know the date on which the current daycounter is on the + * calendar in days and months. + * - Go through the list of events, and print all matching dates + */ + for (daycounter = 0; daycounter <= f_dayAfter + f_dayBefore; + daycounter++) { day = tp->tm_yday - f_dayBefore + daycounter; if (day < 0) day += yrdays; if (day >= yrdays) day -= yrdays; + /* + * When we know the day of the year, we can determine the day + * of the month and the month. + */ month = 1; while (month <= 12) { if (day <= cumdays[month]) @@ -239,10 +284,14 @@ fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day); #endif + /* + * Go through all events and print the text of the matching + * dates + */ for (e = events; e != NULL; e = e_next ) { e_next = e->next; - if (month != e->month || day != e->day) + if (month != e->month || day != e->day) continue; (void)fprintf(fp, "%s%c%s\n", e->date, @@ -286,25 +335,25 @@ /* Day */ else if ((val = getday(start)) != 0) { - *flags |= F_ISDAY; + *flags |= F_ISDAY; - /* variable weekday */ - if ((var = getdayvar(start)) != 0) { - if (var <=5 && var >= -4) - val += var * 10; + /* variable weekday */ + if ((var = getdayvar(start)) != 0) { + if (var <=5 && var >= -4) + val += var * 10; #ifdef DEBUG - printf("var: %d\n", var); + printf("var: %d\n", var); #endif - } + } } /* Easter */ else if ((val = geteaster(start, tp->tm_year + 1900)) != 0) - *flags |= F_EASTER; + *flags |= F_EASTER; /* Paskha */ else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0) - *flags |= F_EASTER; + *flags |= F_EASTER; /* undefined rest */ else { @@ -330,12 +379,12 @@ /* open up calendar file as stdin */ if (!freopen(calendarFile, "r", stdin)) { if (doall) { - if (chdir(calendarHomes[0]) != 0) - return (NULL); - if (stat(calendarNoMail, &sbuf) == 0) - return (NULL); - if (!freopen(calendarFile, "r", stdin)) - return (NULL); + if (chdir(calendarHomes[0]) != 0) + return (NULL); + if (stat(calendarNoMail, &sbuf) == 0) + return (NULL); + if (!freopen(calendarFile, "r", stdin)) + return (NULL); } else { char *home = getenv("HOME"); if (home == NULL || *home == '\0') @@ -343,13 +392,14 @@ chdir(home); for (found = i = 0; i < sizeof(calendarHomes) / sizeof(calendarHomes[0]); i++) - if (chdir(calendarHomes[i]) == 0 && - freopen(calendarFile, "r", stdin)) { - found = 1; - break; - } + if (chdir(calendarHomes[i]) == 0 && + freopen(calendarFile, "r", stdin)) { + found = 1; + break; + } if (!found) - errx(1, "no calendar file: ``%s''", calendarFile); + errx(1, + "no calendar file: ``%s''", calendarFile); } } if (pipe(pdes) < 0) Index: ostern.c =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/ostern.c,v retrieving revision 1.12 diff -u -r1.12 ostern.c --- ostern.c 7 May 2007 11:18:30 -0000 1.12 +++ ostern.c 13 Dec 2007 09:57:42 -0000 @@ -46,25 +46,25 @@ int easter(int year) /* 0 ... abcd, NOT since 1900 */ { - int G, /* Golden number - 1 */ - C, /* Century */ - H, /* 23 - epact % 30 */ - I, /* days from 21 March to Paschal full moon */ - J, /* weekday of full moon */ - L; /* days from 21 March to Sunday on of before full moon */ - - G = year % 19; - C = year / 100; - H = (C - C/4 - (8*C+13)/25 + 19*G + 15) % 30; - I = H - (H/28)*(1 - (H/28)*(29/(H + 1))*((21 - G)/11)); - J = (year + year/4 + I + 2 - C + C/4) % 7; - - L = I - J; - - if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) - return 31 + 29 + 21 + L + 7; - else - return 31 + 28 + 21 + L + 7; + int G, /* Golden number - 1 */ + C, /* Century */ + H, /* 23 - epact % 30 */ + I, /* days from 21 March to Paschal full moon */ + J, /* weekday of full moon */ + L; /* days from 21 March to Sunday on of before full moon */ + + G = year % 19; + C = year / 100; + H = (C - C/4 - (8*C+13)/25 + 19*G + 15) % 30; + I = H - (H/28)*(1 - (H/28)*(29/(H + 1))*((21 - G)/11)); + J = (year + year/4 + I + 2 - C + C/4) % 7; + + L = I - J; + + if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) + return 31 + 29 + 21 + L + 7; + else + return 31 + 28 + 21 + L + 7; } /* return year day for Easter or easter depending days @@ -81,13 +81,13 @@ #define EASTERNAMELEN (sizeof(EASTER) - 1) if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0) - s += EASTERNAMELEN; + s += EASTERNAMELEN; else if ( neaster.name != NULL && strncasecmp(s, neaster.name, neaster.len) == 0 ) - s += neaster.len; + s += neaster.len; else - return(0); + return(0); #if DEBUG printf("%s %d %d\n", s, year, EASTERNAMELEN); @@ -100,11 +100,11 @@ case '-': case '+': - offset = atoi(s); - break; + offset = atoi(s); + break; default: - offset = 0; + offset = 0; } return (easter(year) + offset); Index: paskha.c =================================================================== RCS file: /home/ncvs/src/usr.bin/calendar/paskha.c,v retrieving revision 1.8 diff -u -r1.8 paskha.c --- paskha.c 7 May 2007 11:18:30 -0000 1.8 +++ paskha.c 13 Dec 2007 09:57:42 -0000 @@ -65,14 +65,13 @@ int offset; if (strncasecmp(s, PASKHA, PASKHALEN) == 0) - s += PASKHALEN; + s += PASKHALEN; else if ( npaskha.name != NULL && strncasecmp(s, npaskha.name, npaskha.len) == 0 ) - s += npaskha.len; + s += npaskha.len; else - return 0; - + return 0; /* Paskha+1 or Paskha-2 * ^ ^ */ @@ -81,12 +80,12 @@ case '-': case '+': - offset = atoi(s); - break; + offset = atoi(s); + break; default: - offset = 0; - break; + offset = 0; + break; } return (paskha(year) + offset + 13/* new style */); >Release-Note: >Audit-Trail: >Unformatted: