From cd0cff3392ab64e19edd6ec29b179fa96f4c556c Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 5 Nov 2019 23:23:06 +0800 Subject: [PATCH] calendar(1): Various cleanups * Mostly style cleanups. * Use 'bool' when more appropriate. * Staticize several functions and variables to clean up 'calendar.h'. --- usr.bin/calendar/calendar.c | 48 ++++---- usr.bin/calendar/calendar.h | 22 ++-- usr.bin/calendar/calendars/calendar.australia | 2 +- usr.bin/calendar/calendars/calendar.brazilian | 2 +- usr.bin/calendar/calendars/calendar.dutch | 5 + .../calendar/calendars/calendar.newzealand | 2 +- .../calendar/calendars/calendar.southafrica | 2 +- usr.bin/calendar/dates.c | 33 +++--- usr.bin/calendar/day.c | 12 +- usr.bin/calendar/events.c | 15 +-- usr.bin/calendar/io.c | 104 ++++++++++-------- usr.bin/calendar/locale.c | 7 +- usr.bin/calendar/ostern.c | 5 +- usr.bin/calendar/parsedata.c | 84 +++++++------- usr.bin/calendar/paskha.c | 20 ++-- usr.bin/calendar/pom.c | 45 +++----- usr.bin/calendar/sunpos.c | 35 +++--- 17 files changed, 206 insertions(+), 237 deletions(-) diff --git a/usr.bin/calendar/calendar.c b/usr.bin/calendar/calendar.c index 283861ab9b..0c7b4424f9 100644 --- a/usr.bin/calendar/calendar.c +++ b/usr.bin/calendar/calendar.c @@ -27,21 +27,16 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * @(#)calendar.c 8.3 (Berkeley) 3/25/94 + * $FreeBSD: head/usr.bin/calendar/calendar.c 326025 2017-11-20 19:49:47Z pfg $ */ -#if 0 -#ifndef lint -static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; -#endif -#endif - -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 326025 2017-11-20 19:49:47Z pfg $"); - #include #include #include #include +#include #include #include #include @@ -54,8 +49,8 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 326025 2017-11-20 19:49:47Z #define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */ struct passwd *pw; -int doall = 0; -int debug = 0; +bool doall = false; +bool debug = false; static char *DEBUG = NULL; static time_t f_time = 0; double UTCOffset = UTCOFFSET_NOTSET; @@ -73,23 +68,22 @@ main(int argc, char *argv[]) int ch; struct tm tp1, tp2; - (void)setlocale(LC_ALL, ""); + setlocale(LC_ALL, ""); - while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1) + while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:")) != -1) { switch (ch) { - case '-': /* backward contemptible */ + case '-': /* backward compatible */ case 'a': if (getuid()) { errno = EPERM; err(1, NULL); } - doall = 1; + doall = true; break; case 'W': /* we don't need no steenking Fridays */ Friday = -1; /* FALLTHROUGH */ - case 'A': /* days after current date */ f_dayAfter = atoi(optarg); if (f_dayAfter < 0) @@ -107,7 +101,7 @@ main(int argc, char *argv[]) break; case 'd': /* debug output of current date */ - debug = 1; + debug = true; break; case 'F': /* Change the time: When does weekend start? */ @@ -130,11 +124,10 @@ main(int argc, char *argv[]) UTCOffset = strtod(optarg, NULL); break; - case '?': default: usage(); } - + } argc -= optind; argv += optind; @@ -200,25 +193,26 @@ main(int argc, char *argv[]) exit(0); } - if (doall) + if (doall) { while ((pw = getpwent()) != NULL) { - (void)setegid(pw->pw_gid); - (void)initgroups(pw->pw_name, pw->pw_gid); - (void)seteuid(pw->pw_uid); + setegid(pw->pw_gid); + initgroups(pw->pw_name, pw->pw_gid); + seteuid(pw->pw_uid); if (!chdir(pw->pw_dir)) cal(); - (void)seteuid(0); + seteuid(0); } - else + } else { cal(); - exit(0); + } + + return (0); } static void __dead2 usage(void) { - fprintf(stderr, "%s\n%s\n%s\n", "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]", " [-F friday] [-f calendarfile] [-l longitude]", diff --git a/usr.bin/calendar/calendar.h b/usr.bin/calendar/calendar.h index ddf26ea594..76810a9bc3 100644 --- a/usr.bin/calendar/calendar.h +++ b/usr.bin/calendar/calendar.h @@ -34,6 +34,8 @@ #include #include +#include + #define SECSPERDAY (24 * 60 * 60) #define SECSPERHOUR (60 * 60) #define SECSPERMINUTE (60) @@ -51,7 +53,9 @@ /* Not yet categorized */ extern struct passwd *pw; -extern int doall; +extern bool doall; +extern bool debug; /* show parsing of the input */ +extern int year1, year2; extern time_t t1, t2; extern const char *calendarFile; extern int yrdays; @@ -109,9 +113,6 @@ extern int EastLongitude; */ #define UTCOFFSET_CNY 8.0 -extern int debug; /* show parsing of the input */ -extern int year1, year2; - /* events.c */ /* * Event sorting related functions: @@ -157,7 +158,7 @@ void setnsequences(char *); /* day.c */ extern const struct tm tm0; extern char dayname[]; -void settimes(time_t,int before, int after, int friday, struct tm *tp1, struct tm *tp2); +void settimes(time_t, int before, int after, int friday, struct tm *tp1, struct tm *tp2); time_t Mktime(char *); /* parsedata.c */ @@ -166,23 +167,18 @@ void dodebug(char *type); /* io.c */ void cal(void); -void closecal(FILE *); -FILE *opencalin(void); -FILE *opencalout(void); /* ostern.c / paskha.c */ int paskha(int); int easter(int); -int j2g(int); /* dates.c */ extern int cumdaytab[][14]; extern int monthdaytab[][14]; -extern int debug_remember; void generatedates(struct tm *tp1, struct tm *tp2); void dumpdates(void); -int remember_ymd(int y, int m, int d); -int remember_yd(int y, int d, int *rm, int *rd); +bool remember_ymd(int y, int m, int d); +bool remember_yd(int y, int d, int *rm, int *rd); int first_dayofweek_of_year(int y); int first_dayofweek_of_month(int y, int m); int walkthrough_dates(struct event **e); @@ -191,7 +187,7 @@ void addtodate(struct event *e, int year, int month, int day); /* pom.c */ #define MAXMOONS 18 void pom(int year, double UTCoffset, int *fms, int *nms); -void fpom(int year, double utcoffset, double *ffms, double *fnms); +void fpom(int year, double UTCoffset, double *ffms, double *fnms); /* sunpos.c */ void equinoxsolstice(int year, double UTCoffset, int *equinoxdays, int *solsticedays); diff --git a/usr.bin/calendar/calendars/calendar.australia b/usr.bin/calendar/calendars/calendar.australia index 27c73fbe1c..f7a08d47ac 100644 --- a/usr.bin/calendar/calendars/calendar.australia +++ b/usr.bin/calendar/calendars/calendar.australia @@ -67,4 +67,4 @@ Aug/MonFirst Picnic Day (NT) May/MonFirst Labour Day (QLD) Aug/WedSecond RNA Show Day (Brisbane metro) /* Second Last Wednesday */ -#endif +#endif /* !_calendar_australia_ */ diff --git a/usr.bin/calendar/calendars/calendar.brazilian b/usr.bin/calendar/calendars/calendar.brazilian index 2444336c2a..8503c552f6 100644 --- a/usr.bin/calendar/calendars/calendar.brazilian +++ b/usr.bin/calendar/calendars/calendar.brazilian @@ -10,4 +10,4 @@ #include -#endif +#endif /* !_calendar_brazilian_ */ diff --git a/usr.bin/calendar/calendars/calendar.dutch b/usr.bin/calendar/calendars/calendar.dutch index 68f0dd729c..c4d20f273d 100644 --- a/usr.bin/calendar/calendars/calendar.dutch +++ b/usr.bin/calendar/calendars/calendar.dutch @@ -4,6 +4,9 @@ * Originally submitted by Edwin Groothuis */ +#ifndef _calendar_dutch_ +#define _calendar_dutch_ + Easter=Pasen /* @@ -76,3 +79,5 @@ sep/06 Prins Claus (1925 - 2002) sep/25 Prins Johan Friso (1968 - 2013) okt/11 Prins Constantijn (1969) dec/07 Kroonprinses Catharina-Amalia (2003) + +#endif /* !_calendar_dutch_ */ diff --git a/usr.bin/calendar/calendars/calendar.newzealand b/usr.bin/calendar/calendars/calendar.newzealand index a97a4c659a..5fd5bcc31e 100644 --- a/usr.bin/calendar/calendars/calendar.newzealand +++ b/usr.bin/calendar/calendars/calendar.newzealand @@ -22,4 +22,4 @@ Nov 04 Anniversary Day (Marlborough) Nov 15 Anniversary Day (Canterbury) Dec 02 Anniversary Day (Chatham Islands) -#endif +#endif /* !_calendar_newzealand_ */ diff --git a/usr.bin/calendar/calendars/calendar.southafrica b/usr.bin/calendar/calendars/calendar.southafrica index 107403e1ea..43dc3f69a4 100644 --- a/usr.bin/calendar/calendars/calendar.southafrica +++ b/usr.bin/calendar/calendars/calendar.southafrica @@ -20,4 +20,4 @@ 12/16 Day of Reconciliation in South Africa 12/26 Day of Goodwill in South Africa -#endif +#endif /* !_calendar_southafrica_ */ diff --git a/usr.bin/calendar/dates.c b/usr.bin/calendar/dates.c index 5c428a58ca..01f1862c1c 100644 --- a/usr.bin/calendar/dates.c +++ b/usr.bin/calendar/dates.c @@ -25,11 +25,10 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * $FreeBSD: head/usr.bin/calendar/dates.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/dates.c 326276 2017-11-27 15:37:16Z pfg $"); - +#include #include #include #include @@ -66,7 +65,7 @@ struct cal_day { struct event *events; }; -int debug_remember = 0; +static bool debug_remember = false; static struct cal_year *hyear = NULL; /* 1-based month, 0-based days, cumulative */ @@ -81,7 +80,7 @@ int monthdaytab[][14] = { {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30}, }; -static struct cal_day * find_day(int yy, int mm, int dd); +static struct cal_day *find_day(int yy, int mm, int dd); static void createdate(int y, int m, int d) @@ -118,10 +117,8 @@ createdate(int y, int m, int d) if (pyp != NULL) pyp->nextyear = py; } - if (pyp == NULL) { - /* The very very very first one */ - hyear = py; - } + if (pyp == NULL) + hyear = py; /* The very very very first one */ pmp = NULL; pm = py->months; @@ -258,7 +255,7 @@ dumpdates(void) } } -int +bool remember_ymd(int yy, int mm, int dd) { struct cal_year *y; @@ -283,18 +280,18 @@ remember_ymd(int yy, int mm, int dd) d = m->days; while (d != NULL) { if (d->dayofmonth == dd) - return (1); + return (true); d = d->nextday; continue; } - return (0); + return (false); } - return (0); + return (false); } - return (0); + return (false); } -int +bool remember_yd(int yy, int dd, int *rm, int *rd) { struct cal_year *y; @@ -317,15 +314,15 @@ remember_yd(int yy, int dd, int *rm, int *rd) if (d->julianday == dd) { *rm = m->month; *rd = d->dayofmonth; - return (1); + return (true); } d = d->nextday; } m = m->nextmonth; } - return (0); + return (false); } - return (0); + return (false); } int diff --git a/usr.bin/calendar/day.c b/usr.bin/calendar/day.c index b5f98fcbed..1efabbbe41 100644 --- a/usr.bin/calendar/day.c +++ b/usr.bin/calendar/day.c @@ -27,11 +27,10 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: head/usr.bin/calendar/day.c 326025 2017-11-20 19:49:47Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/day.c 326025 2017-11-20 19:49:47Z pfg $"); - #include #include #include @@ -48,7 +47,8 @@ int year1, year2; void -settimes(time_t now, int before, int after, int friday, struct tm *tp1, struct tm *tp2) +settimes(time_t now, int before, int after, int friday, + struct tm *tp1, struct tm *tp2) { char *oldl, *lbufp; struct tm tp; @@ -72,8 +72,8 @@ settimes(time_t now, int before, int after, int friday, struct tm *tp1, struct t lbufp = setlocale(LC_TIME, NULL); if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL) errx(1, "cannot allocate memory"); - (void)setlocale(LC_TIME, "C"); - (void)setlocale(LC_TIME, (oldl != NULL ? oldl : "")); + setlocale(LC_TIME, "C"); + setlocale(LC_TIME, (oldl != NULL ? oldl : "")); if (oldl != NULL) free(oldl); diff --git a/usr.bin/calendar/events.c b/usr.bin/calendar/events.c index 05c9b55e32..673227e5a3 100644 --- a/usr.bin/calendar/events.c +++ b/usr.bin/calendar/events.c @@ -12,7 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -24,24 +24,21 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * + * + * $FreeBSD: head/usr.bin/calendar/events.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/events.c 326276 2017-11-27 15:37:16Z pfg $"); - #include #include #include #include #include -#include "pathnames.h" #include "calendar.h" struct event * event_add(int year, int month, int day, char *date, int var, char *txt, - char *extra) + char *extra) { struct event *e; @@ -92,8 +89,6 @@ event_continue(struct event *e, char *txt) if (e->text == NULL) errx(1, "event_continue: cannot allocate memory"); free(text); - - return; } void @@ -112,7 +107,7 @@ event_print_all(FILE *fp) * dates */ while (e != NULL) { - (void)fprintf(fp, "%s%c%s%s%s%s\n", e->date, + fprintf(fp, "%s%c%s%s%s%s\n", e->date, e->var ? '*' : ' ', e->text, e->extra != NULL ? " (" : "", e->extra != NULL ? e->extra : "", diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c index 3396a7a968..0620efb9bb 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -27,20 +27,15 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * @(#)calendar.c 8.3 (Berkeley) 3/25/94 + * $FreeBSD: head/usr.bin/calendar/io.c 327117 2017-12-23 21:04:32Z eadler $ */ -#if 0 -#ifndef lint -static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; -#endif -#endif - -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/io.c 327117 2017-12-23 21:04:32Z eadler $"); - #include #include #include + #include #include #include @@ -64,21 +59,28 @@ enum { T_PROCESS, }; -const char *calendarFile = "calendar"; /* default calendar file */ +struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon; +struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice; + +const char *calendarFile = "calendar"; /* default calendar file */ static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */ static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */ static char path[MAXPATHLEN]; -struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon; -struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice; - -static int cal_parse(FILE *in, FILE *out); - static StringList *definitions = NULL; static struct event *events[MAXCOUNT]; static char *extradata[MAXCOUNT]; +static FILE *cal_fopen(const char *file); +static bool cal_parse(FILE *in, FILE *out); +static void closecal(FILE *fp); +static FILE *opencalin(void); +static FILE *opencalout(void); +static int token(char *line, FILE *out, bool *skip); +static void trimlr(char **buf); + + static void trimlr(char **buf) { @@ -179,7 +181,7 @@ token(char *line, FILE *out, bool *skip) } walk[strlen(walk) - 1] = '\0'; - if (cal_parse(cal_fopen(walk), out)) + if (!cal_parse(cal_fopen(walk), out)) return (T_ERR); return (T_OK); @@ -216,19 +218,9 @@ token(char *line, FILE *out, bool *skip) } return (T_PROCESS); - } -#define REPLACE(string, slen, struct_) \ - if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \ - if (struct_.name != NULL) \ - free(struct_.name); \ - if ((struct_.name = strdup(buf + (slen))) == NULL) \ - errx(1, "cannot allocate memory"); \ - struct_.len = strlen(buf + (slen)); \ - continue; \ - } -static int +static bool cal_parse(FILE *in, FILE *out) { char *line = NULL; @@ -255,14 +247,14 @@ cal_parse(FILE *in, FILE *out) tm.tm_wday = 0; if (in == NULL) - return (1); + return (false); while ((linelen = getline(&line, &linecap, in)) > 0) { if (*line == '#') { switch (token(line+1, out, &skip)) { case T_ERR: free(line); - return (1); + return (false); case T_OK: continue; case T_PROCESS: @@ -291,6 +283,17 @@ cal_parse(FILE *in, FILE *out) setnnames(); continue; } + +#define REPLACE(string, slen, struct_) \ + if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \ + if (struct_.name != NULL) \ + free(struct_.name); \ + if ((struct_.name = strdup(buf + (slen))) == NULL) \ + errx(1, "cannot allocate memory"); \ + struct_.len = strlen(buf + (slen)); \ + continue; \ + } + REPLACE("Easter=", 7, neaster); REPLACE("Paskha=", 7, npaskha); REPLACE("ChineseNewYear=", 15, ncny); @@ -300,6 +303,8 @@ cal_parse(FILE *in, FILE *out) REPLACE("SepEquinox=", 11, nsepequinox); REPLACE("JunSolstice=", 12, njunsolstice); REPLACE("DecSolstice=", 12, ndecsolstice); +#undef REPLACE + if (strncmp(buf, "SEQUENCE=", 9) == 0) { setnsequences(buf + 9); continue; @@ -353,7 +358,7 @@ cal_parse(FILE *in, FILE *out) tm.tm_mon = month[i] - 1; tm.tm_mday = day[i]; tm.tm_year = year[i] - 1900; - (void)strftime(dbuf, sizeof(dbuf), + strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); if (debug) fprintf(stderr, "got %s\n", pp); @@ -365,8 +370,7 @@ cal_parse(FILE *in, FILE *out) free(line); fclose(in); - - return (0); + return (true); } void @@ -388,14 +392,14 @@ cal(void) return; } - if (cal_parse(fpin, fpout)) + if (!cal_parse(fpin, fpout)) return; event_print_all(fpout); closecal(fpout); } -FILE * +static FILE * opencalin(void) { struct stat sbuf; @@ -417,7 +421,7 @@ opencalin(void) return (fpin); } -FILE * +static FILE * opencalout(void) { int fd; @@ -433,7 +437,7 @@ opencalout(void) return (fdopen(fd, "w+")); } -void +static void closecal(FILE *fp) { uid_t uid; @@ -450,17 +454,18 @@ closecal(FILE *fp) if (pipe(pdes) < 0) goto done; switch (fork()) { - case -1: /* error */ - (void)close(pdes[0]); - (void)close(pdes[1]); + case -1: + /* error */ + close(pdes[0]); + close(pdes[1]); goto done; case 0: /* child -- set stdin to pipe output */ if (pdes[0] != STDIN_FILENO) { - (void)dup2(pdes[0], STDIN_FILENO); - (void)close(pdes[0]); + dup2(pdes[0], STDIN_FILENO); + close(pdes[0]); } - (void)close(pdes[1]); + close(pdes[1]); uid = geteuid(); if (setuid(getuid()) < 0) { warnx("setuid failed"); @@ -480,7 +485,7 @@ closecal(FILE *fp) _exit(1); } /* parent -- write to pipe input */ - (void)close(pdes[0]); + close(pdes[0]); write(pdes[1], "From: \"Reminder Service\" <", 26); write(pdes[1], pw->pw_name, strlen(pw->pw_name)); @@ -491,9 +496,12 @@ closecal(FILE *fp) write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30); while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) - (void)write(pdes[1], buf, nread); - (void)close(pdes[1]); -done: (void)fclose(fp); - (void)unlink(path); - while (wait(&status) >= 0); + write(pdes[1], buf, nread); + close(pdes[1]); + +done: + fclose(fp); + unlink(path); + while (wait(&status) >= 0) + ; } diff --git a/usr.bin/calendar/locale.c b/usr.bin/calendar/locale.c index 133cd84a05..a5a7b0cfe8 100644 --- a/usr.bin/calendar/locale.c +++ b/usr.bin/calendar/locale.c @@ -27,11 +27,10 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: head/usr.bin/calendar/locale.c 326025 2017-11-20 19:49:47Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/locale.c 326025 2017-11-20 19:49:47Z pfg $"); - #include #include #include @@ -163,6 +162,4 @@ setnsequences(char *seq) } nsequences[i].name = strdup(nsequences[i].name); nsequences[i].len = strlen(nsequences[i].name); - - return; } diff --git a/usr.bin/calendar/ostern.c b/usr.bin/calendar/ostern.c index 01fc39bef7..45b1d2fc48 100644 --- a/usr.bin/calendar/ostern.c +++ b/usr.bin/calendar/ostern.c @@ -24,11 +24,10 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: head/usr.bin/calendar/ostern.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/ostern.c 326276 2017-11-27 15:37:16Z pfg $"); - #include #include #include diff --git a/usr.bin/calendar/parsedata.c b/usr.bin/calendar/parsedata.c index ee4d1149f8..354f52aff9 100644 --- a/usr.bin/calendar/parsedata.c +++ b/usr.bin/calendar/parsedata.c @@ -25,11 +25,9 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * $FreeBSD: head/usr.bin/calendar/parsedata.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/parsedata.c 326276 2017-11-27 15:37:16Z pfg $"); - #include #include #include @@ -40,7 +38,7 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/parsedata.c 326276 2017-11-27 15:37:16 #include "calendar.h" static char *showflags(int flags); -static int isonlydigits(char *s, int nostar); +static bool isonlydigits(char *s, int nostar); static const char *getmonthname(int i); static int checkmonth(char *s, size_t *len, size_t *offset, const char **month); static const char *getdayofweekname(int i); @@ -49,7 +47,13 @@ static int indextooffset(char *s); static int parseoffset(char *s); static char *floattoday(int year, double f); static char *floattotime(double f); -static int wdayom (int day, int offset, int month, int year); +static int wdayom(int day, int offset, int month, int year); +static int determinestyle(char *date, int *flags, char *month, int *imonth, + char *dayofmonth, int *idayofmonth, char *dayofweek, int *idayofweek, + char *modifieroffset, char *modifierindex, char *specialday, + char *year, int *iyear); +static void remember(int *rememberindex, int *y, int *m, int *d, char **ed, + int yy, int mm, int dd, char *extra); /* * Expected styles: @@ -302,20 +306,15 @@ determinestyle(char *date, int *flags, goto allfine; } - /* FALLTHROUGH */ fail: *p = pold; return (0); allfine: *p = pold; return (1); - } -void -remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm, - int dd, char *extra); -void +static void remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm, int dd, char *extra) { @@ -343,7 +342,6 @@ debug_determinestyle(int dateonly, char *date, int flags, char *month, int idayofweek, char *modifieroffset, char *modifierindex, char *specialday, char *year, int iyear) { - if (dateonly != 0) { printf("-------\ndate: |%s|\n", date); if (dateonly == 1) @@ -383,17 +381,16 @@ static struct yearinfo { * offset the ordinal number of the weekday in the month. */ static int -wdayom (int day, int offset, int month, int year) +wdayom(int day, int offset, int month, int year) { -/* Weekday of first day in month */ - int wday1; /* first day of month */ -/* Weekday of last day in month */ - int wdayn; + int wday1; /* Weekday of first day in month */ + int wdayn; /* Weekday of first day in month */ int d; wday1 = first_dayofweek_of_month(year, month); if (wday1 < 0) /* not set */ return (wday1); + /* * Date of zeroth or first of our weekday in month, depending on the * relationship with the first of the month. The range is -6:6. @@ -415,8 +412,9 @@ wdayom (int day, int offset, int month, int year) d += offset * 7 - 7; else d += offset * 7; - } else + } else { warnx ("Invalid offset 0"); + } return (d); } @@ -450,7 +448,6 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, * Monthname: Jan .. Dec * Day: 1-31 * Weekday: Mon .. Sun - * */ *flags = 0; @@ -590,9 +587,9 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, } /* - * Every so-manied dayofweek of every month of the year: - * Thu-3 - */ + * Every so-manied dayofweek of every month of the year: + * Thu-3 + */ if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) { offset = indextooffset(modifierindex); @@ -609,9 +606,9 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, } /* - * A certain dayofweek of a month - * Jan/Thu-3 - */ + * A certain dayofweek of a month + * Jan/Thu-3 + */ if (lflags == (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) { offset = indextooffset(modifierindex); @@ -668,9 +665,10 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, yearinfo->ieaster + offset, - &rm, &rd)) + &rm, &rd)) { remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, NULL); + } continue; } @@ -681,9 +679,10 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, yearinfo->ipaskha + offset, - &rm, &rd)) + &rm, &rd)) { remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, NULL); + } continue; } @@ -694,9 +693,10 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, yearinfo->firstcnyday + offset, - &rm, &rd)) + &rm, &rd)) { remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, NULL); + } continue; } @@ -710,8 +710,8 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, offset = parseoffset(modifieroffset); for (i = 0; yearinfo->ffullmoon[i] > 0; i++) { if (remember_yd(year, - floor(yearinfo->ffullmoon[i]) + offset, - &rm, &rd)) { + floor(yearinfo->ffullmoon[i]) + offset, + &rm, &rd)) { ed = floattotime( yearinfo->ffullmoon[i]); remember(&remindex, @@ -732,8 +732,8 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, offset = parseoffset(modifieroffset); for (i = 0; yearinfo->ffullmoon[i] > 0; i++) { if (remember_yd(year, - floor(yearinfo->fnewmoon[i]) + offset, - &rm, &rd)) { + floor(yearinfo->fnewmoon[i]) + offset, + &rm, &rd)) { ed = floattotime(yearinfo->fnewmoon[i]); remember(&remindex, yearp, monthp, dayp, edp, @@ -750,7 +750,7 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, yearinfo->equinoxdays[0] + offset, - &rm, &rd)) { + &rm, &rd)) { ed = floattotime(yearinfo->equinoxdays[0]); remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, ed); @@ -763,7 +763,7 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, yearinfo->equinoxdays[1] + offset, - &rm, &rd)) { + &rm, &rd)) { ed = floattotime(yearinfo->equinoxdays[1]); remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, ed); @@ -778,7 +778,7 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, - yearinfo->solsticedays[0] + offset, &rm, &rd)) { + yearinfo->solsticedays[0] + offset, &rm, &rd)) { ed = floattotime(yearinfo->solsticedays[0]); remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, ed); @@ -791,7 +791,7 @@ parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, if ((lflags & F_MODIFIEROFFSET) != 0) offset = parseoffset(modifieroffset); if (remember_yd(year, - yearinfo->solsticedays[1] + offset, &rm, &rd)) { + yearinfo->solsticedays[1] + offset, &rm, &rd)) { ed = floattotime(yearinfo->solsticedays[1]); remember(&remindex, yearp, monthp, dayp, edp, year, rm, rd, ed); @@ -967,17 +967,17 @@ checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow) return (0); } -static int +static bool isonlydigits(char *s, int nostar) { int i; for (i = 0; s[i] != '\0'; i++) { if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0') - return 1; + return (true); if (!isdigit((unsigned char)s[i])) - return (0); + return (false); } - return (1); + return (true); } static int @@ -1069,7 +1069,7 @@ dodebug(char *what) int year; printf("UTCOffset: %g\n", UTCOffset); - printf("eastlongitude: %d\n", EastLongitude); + printf("EastLongitude: %g\n", EastLongitude); if (strcmp(what, "moon") == 0) { double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS]; @@ -1088,9 +1088,7 @@ dodebug(char *what) floattoday(year, fnewmoon[i])); } printf("\n"); - } - return; } diff --git a/usr.bin/calendar/paskha.c b/usr.bin/calendar/paskha.c index 9ce397ebca..f25aa6912a 100644 --- a/usr.bin/calendar/paskha.c +++ b/usr.bin/calendar/paskha.c @@ -24,11 +24,10 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: head/usr.bin/calendar/paskha.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/paskha.c 326276 2017-11-27 15:37:16Z pfg $"); - #include #include #include @@ -38,8 +37,12 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/paskha.c 326276 2017-11-27 15:37:16Z p #define PASKHA "paskha" #define PASKHALEN (sizeof(PASKHA) - 1) -/* return difference in days between Julian and Gregorian calendars */ -int +static int j2g(int year); + +/* + * Return difference in days between Julian and Gregorian calendars + */ +static int j2g(int year) { return (year < 1500) ? @@ -47,9 +50,10 @@ j2g(int year) 10 + (year/100 - 16) - ((year/100 - 16) / 4); } -/* return year day for Orthodox Easter using Gauss formula */ -/* (new style result) */ - +/* + * Return year day for Orthodox Easter using Gauss formula + * (new style result) + */ int paskha(int R) /*year*/ { diff --git a/usr.bin/calendar/pom.c b/usr.bin/calendar/pom.c index 457eb827fa..ede5ed5d70 100644 --- a/usr.bin/calendar/pom.c +++ b/usr.bin/calendar/pom.c @@ -29,22 +29,11 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * @(#)pom.c 8.1 (Berkeley) 5/31/93 + * $FreeBSD: head/usr.bin/calendar/pom.c 326025 2017-11-20 19:49:47Z pfg $ */ -#if 0 -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1989, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#ifndef lint -static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93"; -#endif /* not lint */ -#endif -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/pom.c 326025 2017-11-20 19:49:47Z pfg $"); - /* * Phase of the Moon. Calculates the current phase of the moon. * Based on routines from `Practical Astronomy with Your Calculator', @@ -52,22 +41,17 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/pom.c 326025 2017-11-20 19:49:47Z pfg * particular piece of code was adapted from. * * -- Keith E. Brandt VIII 1984 - * */ #include #include #include #include -#include #include -#include +#include #include "calendar.h" -#ifndef PI -#define PI 3.14159265358979323846 -#endif #define EPOCH 85 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */ #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */ @@ -75,10 +59,9 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/pom.c 326025 2017-11-20 19:49:47Z pfg #define lzero 18.251907 /* lunar mean long at EPOCH */ #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */ #define Nzero 55.204723 /* lunar mean long of node at EPOCH */ -#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) -static void adj360(double *); -static double dtor(double); +static void adj360(double *deg); +static double dtor(double deg); static double potm(double onday); static double potm_minute(double onday, int olddir); @@ -205,11 +188,10 @@ potm_minute(double onday, int olddir) { else p1 -= (period / SECSPERDAY); period /= 2; -// printf("newdir:%d - p1:%10.10f - period:%g\n", -// newdir, p1, period); +// printf("newdir:%d - p1:%10.10f - period:%g\n", newdir, p1, period); } p1 -= floor(p1); - //exit(0); + return (p1); } @@ -227,7 +209,7 @@ potm(double onday) adj360(&N); Msol = N + EPSILONg - RHOg; /* sec 42 #4 */ adj360(&Msol); - Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */ + Ec = 360 / M_PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */ LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */ adj360(&LambdaSol); l = 13.1763966 * onday + lzero; /* sec 61 #4 */ @@ -246,7 +228,7 @@ potm(double onday) V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */ ldprime = lprime + V; /* sec 61 #14 */ D = ldprime - LambdaSol; /* sec 63 #2 */ - return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */ + return (50 * (1 - cos(dtor(D)))); /* sec 63 #3 */ } /* @@ -256,8 +238,7 @@ potm(double onday) static double dtor(double deg) { - - return(deg * PI / 180); + return (deg * M_PI / 180); } /* @@ -267,12 +248,12 @@ dtor(double deg) static void adj360(double *deg) { - - for (;;) + for (;;) { if (*deg < 0) *deg += 360; else if (*deg > 360) *deg -= 360; else break; + } } diff --git a/usr.bin/calendar/sunpos.c b/usr.bin/calendar/sunpos.c index 41fdf7014f..1e34dec29c 100644 --- a/usr.bin/calendar/sunpos.c +++ b/usr.bin/calendar/sunpos.c @@ -25,11 +25,9 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * $FreeBSD: head/usr.bin/calendar/sunpos.c 326276 2017-11-27 15:37:16Z pfg $ */ -#include -__FBSDID("$FreeBSD: head/usr.bin/calendar/sunpos.c 326276 2017-11-27 15:37:16Z pfg $"); - /* * This code is created to match the formulas available at: * Formula and examples obtained from "How to Calculate alt/az: SAAO" at @@ -42,6 +40,7 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/sunpos.c 326276 2017-11-27 15:37:16Z p #include #include #include + #include "calendar.h" #define D2R(m) ((m) / 180 * M_PI) @@ -57,7 +56,6 @@ __FBSDID("$FreeBSD: head/usr.bin/calendar/sunpos.c 326276 2017-11-27 15:37:16Z p static void comp(char *s, double v, double c) { - printf("%-*s %*g %*g %*g\n", 15, s, 15, v, 15, c, 15, v - c); } @@ -82,7 +80,6 @@ double expAZ = 67.49; static double fixup(double *d) { - if (*d < 0) { while (*d < 0) *d += 360; @@ -139,11 +136,10 @@ sunpos(int inYY, int inMM, int inDD, double UTCOFFSET, int inHOUR, int inMIN, HA = *L - alpha + 180 + 15 * UTHM + eastlongitude; /* 12 */ fixup(&HA); fixup(&latitude); -#ifdef NOTDEF +#ifdef DEBUG printf("%02d/%02d %02d:%02d:%02d l:%g d:%g h:%g\n", inMM, inDD, inHOUR, inMIN, inSEC, latitude, *DEC, HA); #endif - return; /* * The following calculations are not used, so to save time @@ -161,9 +157,7 @@ sunpos(int inYY, int inMM, int inDD, double UTCOFFSET, int inHOUR, int inMIN, if (*ALT < -180) *ALT += 360; printf("a:%g a:%g\n", *ALT, *AZ); -#endif -#ifdef NOTDEF printf("Y:\t\t\t %d\t\t %d\t\t %d\n", Y, expY, Y - expY); comp("ZJ", ZJ, expZJ); comp("UTHM", UTHM, expUTHM); @@ -184,14 +178,14 @@ sunpos(int inYY, int inMM, int inDD, double UTCOFFSET, int inHOUR, int inMIN, } -#define SIGN(a) (((a) > 180) ? -1 : 1) -#define ANGLE(a, b) (((a) < (b)) ? 1 : -1) -#define SHOUR(s) ((s) / 3600) -#define SMINUTE(s) (((s) % 3600) / 60) -#define SSEC(s) ((s) % 60) -#define HOUR(h) ((h) / 4) -#define MINUTE(h) (15 * ((h) % 4)) -#define SEC(h) 0 +#define SIGN(a) (((a) > 180) ? -1 : 1) +#define ANGLE(a, b) (((a) < (b)) ? 1 : -1) +#define SHOUR(s) ((s) / 3600) +#define SMINUTE(s) (((s) % 3600) / 60) +#define SSEC(s) ((s) % 60) +#define HOUR(h) ((h) / 4) +#define MINUTE(h) (15 * ((h) % 4)) +#define SEC(h) 0 #define DEBUG1(y, m, d, hh, mm, pdec, dec) \ printf("%4d-%02d-%02d %02d:%02d:00 - %7.7g -> %7.7g\n", \ y, m, d, hh, mm, pdec, dec) @@ -316,7 +310,7 @@ fequinoxsolstice(int year, double UTCoffset, double *equinoxdays, double *solsti 0.0, 0.0, &L, &dec); angle = ANGLE(prevdec, dec); if (prevangle != angle) { -#ifdef NOTDEF +#ifdef DEBUG DEBUG2(year, 6, d, HOUR(h), MINUTE(h), prevdec, dec, prevangle, angle); #endif @@ -346,7 +340,7 @@ fequinoxsolstice(int year, double UTCoffset, double *equinoxdays, double *solsti 0.0, 0.0, &L, &dec); angle = ANGLE(prevdec, dec); if (prevangle != angle) { -#ifdef NOTDEF +#ifdef DEBUG DEBUG2(year, 12, d, HOUR(h), MINUTE(h), prevdec, dec, prevangle, angle); #endif @@ -398,7 +392,7 @@ printf("%04d-%02d-%02d %02d:%02d - %d %g\n", #endif pichinesemonths++; } else { - for (i = 0; i <= 360; i += 30) + for (i = 0; i <= 360; i += 30) { if (curL > i && prevL < i) { *pichinesemonths = cumdays[m] + d; @@ -410,6 +404,7 @@ printf("%04d-%02d-%02d %02d:%02d - %d %g\n", firstmonth330 = *pichinesemonths; pichinesemonths++; } + } } prevL = curL; } -- 2.41.0