=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/calendar/calendar.c,v retrieving revision 1.17 retrieving revision 1.35 diff -u -r1.17 -r1.35 --- src/usr.bin/calendar/calendar.c 2002/02/16 21:27:44 1.17 +++ src/usr.bin/calendar/calendar.c 2015/12/07 18:46:35 1.35 @@ -1,4 +1,4 @@ -/* $OpenBSD: calendar.c,v 1.17 2002/02/16 21:27:44 millert Exp $ */ +/* $OpenBSD: calendar.c,v 1.35 2015/12/07 18:46:35 espie Exp $ */ /* * Copyright (c) 1989, 1993, 1994 @@ -12,11 +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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -33,20 +29,6 @@ * SUCH DAMAGE. */ -#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 -#if 0 -static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; -#else -static char rcsid[] = "$OpenBSD: calendar.c,v 1.17 2002/02/16 21:27:44 millert Exp $"; -#endif -#endif /* not lint */ - #include #include #include @@ -59,8 +41,8 @@ #include #include #include +#include #include -#include #include #include "pathnames.h" @@ -72,27 +54,28 @@ struct passwd *pw; int doall = 0; +int daynames = 0; time_t f_time = 0; int bodun_always = 0; int f_dayAfter = 0; /* days after current date */ int f_dayBefore = 0; /* days before current date */ +int f_SetdayAfter = 0; /* calendar invoked with -A */ struct specialev spev[NUMEV]; void childsig(int); int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { int ch; + const char *errstr; char *caldir; (void)setlocale(LC_ALL, ""); - while ((ch = getopt(argc, argv, "-abf:t:A:B:")) != -1) + while ((ch = getopt(argc, argv, "abwf:t:A:B:-")) != -1) switch (ch) { case '-': /* backward contemptible */ case 'a': @@ -102,7 +85,7 @@ break; case 'b': - bodun_always++; + bodun_always = 1; break; case 'f': /* other calendar file */ @@ -115,13 +98,22 @@ break; case 'A': /* days after current date */ - f_dayAfter = atoi(optarg); + f_dayAfter = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(1, "-A %s: %s", optarg, errstr); + f_SetdayAfter = 1; break; case 'B': /* days before current date */ - f_dayBefore = atoi(optarg); + f_dayBefore = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(1, "-B %s: %s", optarg, errstr); break; + case 'w': + daynames = 1; + break; + default: usage(); } @@ -131,6 +123,15 @@ if (argc) usage(); + if (doall) { + if (pledge("stdio rpath tmppath fattr getpw id proc exec", NULL) + == -1) + err(1, "pledge"); + } else { + if (pledge("stdio rpath proc exec", NULL) == -1) + err(1, "pledge"); + } + /* use current time */ if (f_time <= 0) (void)time(&f_time); @@ -187,15 +188,16 @@ warn("fork"); continue; case 0: /* child */ + (void)setpgid(getpid(), getpid()); (void)setlocale(LC_ALL, ""); if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL ^ LOGIN_SETLOGIN)) - err(1, "unable to set user context (uid %d)", - (int)pw->pw_uid); + err(1, "unable to set user context (uid %u)", + pw->pw_uid); if (acstat) { if (chdir(pw->pw_dir) || stat(calendarFile, &sbuf) != 0 || - chdir(calendarHome) || + chdir(calendarHome) || stat(calendarNoMail, &sbuf) == 0 || stat(calendarFile, &sbuf) != 0) exit(0); @@ -232,12 +234,16 @@ /* It doesn't _really_ matter if the kill fails, e.g. * if there's only a zombie now. */ - (void)kill(kid, SIGTERM); - warnx("uid %d did not finish in time", (int)pw->pw_uid); + if (getpgid(kid) != getpgrp()) + (void)killpg(getpgid(kid), SIGTERM); + else + (void)kill(kid, SIGTERM); + warnx("uid %u did not finish in time", pw->pw_uid); } if (time(NULL) - t >= SECSPERDAY) - errx(2, "'calendar -a' took more than a day; stopped at uid %d", - (int)pw->pw_uid); + errx(2, "'calendar -a' took more than a day; " + "stopped at uid %u", + pw->pw_uid); } for (;;) { deadkid = waitpid(-1, &kidstat, WNOHANG); @@ -246,10 +252,9 @@ runningkids--; } if (runningkids) - warnx( -"%d child processes still running when 'calendar -a' finished", runningkids); - } - else if ((caldir = getenv("CALENDAR_DIR")) != NULL) { + warnx("%d child processes still running when " + "'calendar -a' finished", runningkids); + } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) { if(!chdir(caldir)) cal(); } else @@ -260,17 +265,16 @@ void -usage() +usage(void) { (void)fprintf(stderr, - "usage: calendar [-a] [-A num] [-b] [-B num] [-t [[[cc]yy][mm]]dd] " - "[-f calendarfile]\n"); + "usage: calendar [-abw] [-A num] [-B num] [-f calendarfile] " + "[-t [[[cc]yy]mm]dd]\n"); exit(1); } void -childsig(sig) - int sig; +childsig(int signo) { }