ansi and lint fixes from Anon Ymous

This commit is contained in:
christos 2008-03-07 19:22:22 +00:00
parent f0a736e7f1
commit 53175f5071
3 changed files with 182 additions and 167 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.12 2002/11/30 03:10:55 lukem Exp $
# $NetBSD: Makefile,v 1.13 2008/03/07 19:22:22 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
.include <bsd.own.mk>
@ -8,5 +8,6 @@ PROG= calendar
FILESDIR=/usr/share/calendar
FILES!= echo ${.CURDIR}/calendars/calendar.*
.endif
WARNS=4
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
.\" $NetBSD: calendar.1,v 1.21 2004/12/08 15:44:21 wiz Exp $
.\" $NetBSD: calendar.1,v 1.22 2008/03/07 19:22:22 christos Exp $
.\"
.\" Copyright (c) 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -232,3 +232,9 @@ command appeared in
.Nm
doesn't handle events that move around from year to year, i.e.,
.Dq the last Monday in April .
.Pp
The
.Fl a
option ignores the user's
.Ev CALENDAR_DIR
environment variable.

View File

@ -1,4 +1,4 @@
/* $NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos Exp $ */
/* $NetBSD: calendar.c,v 1.44 2008/03/07 19:22:22 christos Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
#if 0
static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
#endif
__RCSID("$NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos Exp $");
__RCSID("$NetBSD: calendar.c,v 1.44 2008/03/07 19:22:22 christos Exp $");
#endif /* not lint */
#include <sys/param.h>
@ -53,6 +53,7 @@ __RCSID("$NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos Exp $");
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -62,51 +63,45 @@ __RCSID("$NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos Exp $");
#include "pathnames.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
static unsigned short lookahead = 1, weekend = 2;
static char *fname = NULL, *datestr = NULL;
static char *defaultnames[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR, NULL};
static unsigned short lookahead = 1;
static unsigned short weekend = 2;
static char *fname = NULL;
static char *datestr = NULL;
static const char *defaultnames[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR, NULL};
static struct passwd *pw;
static int doall;
static char path[MAXPATHLEN + 1];
static int cpp_restricted = 0;
static bool doall = false;
static bool cpp_restricted = false;
/* 1-based month, 0-based days, cumulative */
static int daytab[][14] = {
static const int daytab[][14] = {
{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
};
static struct tm *tp;
static int *cumdays, offset, yrdays;
static const int *cumdays;
static int offset, yrdays;
static char dayname[10];
static struct iovec header[] = {
{ "From: ", 6 },
{ __UNCONST("From: "), 6 },
{ NULL, 0 },
{ " (Reminder Service)\nTo: ", 24 },
{ __UNCONST(" (Reminder Service)\nTo: "), 24 },
{ NULL, 0 },
{ "\nSubject: ", 10 },
{ __UNCONST("\nSubject: "), 10 },
{ NULL, 0 },
{ "'s Calendar\nPrecedence: bulk\n\n", 30 },
{ __UNCONST("'s Calendar\nPrecedence: bulk\n\n"), 30 },
};
static char *days[] = {
static const char *days[] = {
"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
};
static char *months[] = {
static const char *months[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec", NULL,
};
int main(int, char **);
static void atodays(int, char *, unsigned short *);
static void cal(void);
static void closecal(FILE *);
@ -114,28 +109,28 @@ static int getday(char *);
static int getfield(char *, char **, int *);
static void getmmdd(struct tm *, char *);
static int getmonth(char *);
static int isnow(char *);
static bool isnow(char *);
static FILE *opencal(void);
static void settime(void);
static void usage(void) __dead;
int
main(argc, argv)
int argc;
char *argv[];
main(int argc, char **argv)
{
int ch;
const char *caldir;
while ((ch = getopt(argc, argv, "-ad:f:l:w:x")) != -1)
(void)setprogname(argv[0]); /* for portability */
while ((ch = getopt(argc, argv, "-ad:f:l:w:x")) != -1) {
switch (ch) {
case '-': /* backward contemptible */
case 'a':
if (getuid()) {
errno = EPERM;
err(1, NULL);
err(EXIT_FAILURE, NULL);
}
doall = 1;
doall = true;
break;
case 'd':
datestr = optarg;
@ -150,12 +145,13 @@ main(argc, argv)
atodays(ch, optarg, &weekend);
break;
case 'x':
cpp_restricted = 1;
cpp_restricted = true;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
@ -164,38 +160,43 @@ main(argc, argv)
settime();
if (doall) {
/*
* XXX - This ignores the user's CALENDAR_DIR variable.
* Run under user's login shell?
*/
while ((pw = getpwent()) != NULL) {
(void)setegid(pw->pw_gid);
(void)seteuid(pw->pw_uid);
if (!chdir(pw->pw_dir))
if (chdir(pw->pw_dir) != -1)
cal();
(void)seteuid(0);
}
} else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
if(!chdir(caldir))
if (chdir(caldir) != -1)
cal();
} else {
if (((pw = getpwuid(geteuid())) != NULL) && !chdir(pw->pw_dir))
} else if ((pw = getpwuid(geteuid())) != NULL) {
if (chdir(pw->pw_dir) != -1)
cal();
}
exit(0);
return 0;
}
static void
cal(void)
{
int printing = 0;
bool printing;
FILE *fp;
char *line;
if ((fp = opencal()) == NULL)
return;
while ((line = fparseln(stdin, NULL, NULL, NULL, FPARSELN_UNESCCOMM))
!= NULL) {
printing = false;
while ((line = fparseln(stdin,
NULL, NULL, NULL, FPARSELN_UNESCCOMM)) != NULL) {
if (line[0] == '\0')
continue;
if (line[0] != '\t')
printing = isnow(line) ? 1 : 0;
printing = isnow(line);
if (printing)
(void)fprintf(fp, "%s\n", line);
free(line);
@ -204,7 +205,6 @@ cal(void)
closecal(fp);
}
static void
settime(void)
{
@ -212,9 +212,9 @@ settime(void)
(void)time(&now);
tp = localtime(&now);
if (datestr) {
if (datestr)
getmmdd(tp, datestr);
}
if (isleap(tp->tm_year + TM_YEAR_BASE)) {
yrdays = DAYSPERLYEAR;
cumdays = daytab[1];
@ -238,11 +238,14 @@ settime(void)
* following a line that is matched, that starts with "whitespace", is shown
* along with the matched line.
*/
static int
isnow(endp)
char *endp;
static bool
isnow(char *endp)
{
int day, flags, month, v1, v2;
int day;
int flags;
int month;
int v1;
int v2;
#define F_ISMONTH 0x01
#define F_ISDAY 0x02
@ -253,19 +256,20 @@ isnow(endp)
/* didn't recognize anything, skip it */
if (!(v1 = getfield(endp, &endp, &flags)))
return (0);
return false;
if (flags & F_ISDAY || v1 > 12) {
/* found a day */
day = v1;
/* if no recognizable month, assume wildcard ('*') month */
if (!(month = getfield(endp, &endp, &flags))) {
if ((month = getfield(endp, &endp, &flags)) != 0) {
flags |= F_ISMONTH | F_WILDMONTH;
month = tp->tm_mon + 1;
}
} else if (flags & F_ISMONTH) {
month = v1;
/* if no recognizable day, assume the first */
if (!(day = getfield(endp, &endp, &flags)))
if ((day = getfield(endp, &endp, &flags)) != 0)
day = 1;
} else {
v2 = getfield(endp, &endp, &flags);
@ -279,15 +283,14 @@ isnow(endp)
day = v2 ? v2 : 1;
}
}
if (flags & F_WILDMONTH && flags & F_WILDDAY)
return true;
if ((flags & F_WILDMONTH) && (flags & F_WILDDAY))
return (1);
if (flags & F_WILDMONTH && flags & F_ISDAY && day == tp->tm_mday)
return true;
if ((flags & F_WILDMONTH) && (flags & F_ISDAY) && (day == tp->tm_mday))
return (1);
if (((flags & F_ISMONTH) && (flags & F_WILDDAY)) && (month == tp->tm_mon + 1))
return (1);
if (flags & F_ISMONTH && flags & F_WILDDAY && month == tp->tm_mon + 1)
return true;
if (flags & F_ISDAY)
day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
@ -295,97 +298,100 @@ isnow(endp)
/* if today or today + offset days */
if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
return (1);
return true;
/* if number of days left in this year + days to event in next year */
if (yrdays - tp->tm_yday + day <= offset)
return (1);
return (0);
return true;
return false;
}
static int
getfield(p, endp, flags)
char *p, **endp;
int *flags;
getfield(char *p, char **endp, int *flags)
{
int val;
char *start, savech;
char *start;
char savech;
#define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
!isalpha((unsigned char)*p) && *p != '*')
for (; FLDCHAR(*p); ++p)
for (/*EMPTY*/; FLDCHAR(*p); ++p)
continue;
if (*p == '*') { /* `*' is current month */
if (!(*flags & F_ISMONTH)) {
*flags |= F_ISMONTH|F_WILDMONTH;
*endp = p+1;
return (tp->tm_mon + 1);
*flags |= F_ISMONTH | F_WILDMONTH;
*endp = p + 1;
return tp->tm_mon + 1;
} else {
*flags |= F_ISDAY|F_WILDDAY;
*endp = p+1;
return (1);
*flags |= F_ISDAY | F_WILDDAY;
*endp = p + 1;
return 1;
}
}
if (isdigit((unsigned char)*p)) {
val = strtol(p, &p, 10); /* if 0, it's failure */
for (; FLDCHAR(*p); ++p)
val = (int)strtol(p, &p, 10); /* if 0, it's failure */
for (/*EMPTY*/; FLDCHAR(*p); ++p)
continue;
*endp = p;
return (val);
return val;
}
for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
for (start = p; *p != '\0' && isalpha((unsigned char)*++p); /*EMPTY*/)
continue;
savech = *p;
*p = '\0';
if ((val = getmonth(start)) != 0) {
if ((val = getmonth(start)) != 0)
*flags |= F_ISMONTH;
} else if ((val = getday(start)) != 0) {
else if ((val = getday(start)) != 0)
*flags |= F_ISDAY;
} else {
else {
*p = savech;
return (0);
return 0;
}
for (*p = savech; FLDCHAR(*p); ++p)
continue;
*endp = p;
return (val);
return val;
}
static FILE *
opencal(void)
{
int fd, pdes[2];
char **name;
int fd;
int pdes[2];
const char **name;
/* open up calendar file as stdin */
if (fname == NULL) {
for (name = defaultnames; *name != NULL; name++) {
if (!freopen(*name, "rf", stdin))
if (freopen(*name, "rf", stdin) == NULL)
continue;
else
break;
}
if (*name == NULL) {
if (doall)
return (NULL);
err(1, "Cannot open calendar file");
return NULL;
err(EXIT_FAILURE, "Cannot open calendar file");
}
} else if (!freopen(fname, "rf", stdin)) {
} else if (freopen(fname, "rf", stdin) == NULL) {
if (doall)
return (NULL);
err(1, "Cannot open `%s'", fname);
return NULL;
err(EXIT_FAILURE, "Cannot open `%s'", fname);
}
if (pipe(pdes) < 0) {
if (pipe(pdes) == -1) {
warn("Cannot open pipe");
return (NULL);
return NULL;
}
switch (fork()) {
case -1: /* error */
case -1:
/* error */
(void)close(pdes[0]);
(void)close(pdes[1]);
return (NULL);
return NULL;
case 0:
/* child -- stdin already setup, set stdout to pipe input */
if (pdes[1] != STDOUT_FILENO) {
@ -394,55 +400,62 @@ opencal(void)
}
(void)close(pdes[0]);
/* tell CPP to only open regular files */
if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1))
err(1, "Cannot restrict cpp");
cpp_restricted = 1;
if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1) == -1)
err(EXIT_FAILURE, "Cannot restrict cpp");
cpp_restricted = true;
(void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
"-I" _PATH_CALENDARS, NULL);
err(1, "Cannot exec `%s'", _PATH_CPP);
err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_CPP);
/*NOTREACHED*/
default:
/* parent -- set stdin to pipe output */
(void)dup2(pdes[0], STDIN_FILENO);
(void)close(pdes[0]);
(void)close(pdes[1]);
/* not reading all calendar files, just set output to stdout */
if (!doall)
return stdout;
/*
* Set output to a temporary file, so if no output
* don't send mail.
*/
(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
if ((fd = mkstemp(path)) == -1) {
warn("Cannot create temporary file");
return NULL;
}
return fdopen(fd, "w+");
}
/* parent -- set stdin to pipe output */
(void)dup2(pdes[0], STDIN_FILENO);
(void)close(pdes[0]);
(void)close(pdes[1]);
/* not reading all calendar files, just set output to stdout */
if (!doall)
return (stdout);
/* set output to a temporary file, so if no output don't send mail */
(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
if ((fd = mkstemp(path)) < 0) {
warn("Cannot create temporary file");
return (NULL);
}
return (fdopen(fd, "w+"));
/*NOTREACHED*/
}
static void
closecal(fp)
FILE *fp;
closecal(FILE *fp)
{
struct stat sbuf;
int nread, pdes[2], status;
ssize_t nread;
int pdes[2];
int status;
char buf[1024];
if (!doall)
return;
(void)rewind(fp);
if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
if (fstat(fileno(fp), &sbuf) == -1 || sbuf.st_size == 0)
goto done;
if (pipe(pdes) < 0)
if (pipe(pdes) == -1)
goto done;
switch (fork()) {
case -1: /* error */
case -1:
/* error */
(void)close(pdes[0]);
(void)close(pdes[1]);
goto done;
break;
case 0:
/* child -- set stdin to pipe output */
if (pdes[0] != STDIN_FILENO) {
@ -452,60 +465,59 @@ closecal(fp)
(void)close(pdes[1]);
(void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
"\"Reminder Service\"", "-f", "root", NULL);
err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_SENDMAIL);
/*NOTREACHED*/
}
/* parent -- write to pipe input */
(void)close(pdes[0]);
default:
/* parent -- write to pipe input */
(void)close(pdes[0]);
header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
writev(pdes[1], header, 7);
while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
(void)write(pdes[1], buf, nread);
(void)close(pdes[1]);
header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
(void)writev(pdes[1], header, 7);
while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
(void)write(pdes[1], buf, (size_t)nread);
(void)close(pdes[1]);
break;
}
done: (void)fclose(fp);
(void)unlink(path);
while (wait(&status) >= 0)
while (wait(&status) != -1)
continue;
}
static int
getmonth(s)
char *s;
getmonth(char *s)
{
char **p;
const char **p;
for (p = months; *p; ++p)
if (!strncasecmp(s, *p, 3))
return ((p - months) + 1);
return (0);
if (strncasecmp(s, *p, 3) == 0)
return (int)(p - months) + 1;
return 0;
}
static int
getday(s)
char *s;
getday(char *s)
{
char **p;
const char **p;
for (p = days; *p; ++p)
if (!strncasecmp(s, *p, 3))
return ((p - days) + 1);
return (0);
if (strncasecmp(s, *p, 3) == 0)
return (int)(p - days) + 1;
return 0;
}
static void
atodays(int ch, char *optarg, unsigned short *days)
atodays(int ch, char *arg, unsigned short *rvp)
{
int u;
u = atoi(optarg);
if ((u < 0) || (u > 366)) {
u = atoi(arg);
if (u < 0 || u > 366)
warnx("-%c %d out of range 0-366, ignored.", ch, u);
} else {
*days = u;
}
else
*rvp = u;
}
#define todigit(x) ((x) - '0')
@ -513,26 +525,23 @@ atodays(int ch, char *optarg, unsigned short *days)
#define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
static void
getmmdd(struct tm *tp, char *ds)
getmmdd(struct tm *ptm, char *ds)
{
int ok = FALSE;
bool ok = false;
struct tm ttm;
ttm = *tp;
ttm = *ptm;
ttm.tm_isdst = -1;
if (ISDIG2(ds)) {
ttm.tm_mon = ATOI2(ds) - 1;
ds += 2;
}
if (ISDIG2(ds)) {
ttm.tm_mday = ATOI2(ds);
ds += 2;
ok = TRUE;
ok = true;
}
if (ok) {
if (ISDIG2(ds) && ISDIG2(ds + 2)) {
ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
@ -546,19 +555,18 @@ getmmdd(struct tm *tp, char *ds)
ttm.tm_year += 1900 - TM_YEAR_BASE;
}
}
if (ok && (mktime(&ttm) < 0)) {
ok = FALSE;
}
if (ok) {
*tp = ttm;
} else {
if (ok && mktime(&ttm) == -1)
ok = false;
if (ok)
*ptm = ttm;
else {
warnx("Can't convert `%s' to date, ignored.", ds);
usage();
}
}
__dead
static void
usage(void)
{