X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/46b902da5252697ebfccc29f3d5525122fbf1033..a92dd07897672b9d6ecfb6ab8353cf224a94e1f6:/dm/dm.c diff --git a/dm/dm.c b/dm/dm.c index fca2fd39..e2f7b36c 100644 --- a/dm/dm.c +++ b/dm/dm.c @@ -1,4 +1,4 @@ -/* $NetBSD: dm.c,v 1.5 1996/02/06 22:47:20 jtc Exp $ */ +/* $NetBSD: dm.c,v 1.29 2009/08/27 00:22:28 dholland Exp $ */ /* * Copyright (c) 1987, 1993 @@ -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,17 +29,17 @@ * SUCH DAMAGE. */ +#include #ifndef lint -static char copyright[] = -"@(#) Copyright (c) 1987, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1987, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93"; #else -static char rcsid[] = "$NetBSD: dm.c,v 1.5 1996/02/06 22:47:20 jtc Exp $"; +__RCSID("$NetBSD: dm.c,v 1.29 2009/08/27 00:22:28 dholland Exp $"); #endif #endif /* not lint */ @@ -52,16 +48,17 @@ static char rcsid[] = "$NetBSD: dm.c,v 1.5 1996/02/06 22:47:20 jtc Exp $"; #include #include +#include #include -#include +#include #include #include +#include #include #include #include -#include -#include +#include "utmpentry.h" #include "pathnames.h" static time_t now; /* current time value */ @@ -69,20 +66,33 @@ static int priority = 0; /* priority game runs at */ static char *game, /* requested game */ *gametty; /* from tty? */ +static void c_day(const char *, const char *, const char *); +static void c_game(const char *, const char *, const char *, const char *); +static void c_tty(const char *); +static const char *hour(int); +static double load(void); +static void nogamefile(void); +static void play(char **) __dead; +static void read_config(void); +static int users(void); + +#ifdef LOG +static void logfile(void); +#endif + int -main(argc, argv) - int argc; - char *argv[]; +main(int argc __unused, char *argv[]) { char *cp; nogamefile(); - game = (cp = rindex(*argv, '/')) ? ++cp : *argv; + game = (cp = strrchr(*argv, '/')) ? ++cp : *argv; if (!strcmp(game, "dm")) exit(0); gametty = ttyname(0); + unsetenv("TZ"); (void)time(&now); read_config(); #ifdef LOG @@ -90,32 +100,31 @@ main(argc, argv) #endif play(argv); /*NOTREACHED*/ + return (0); } /* * play -- * play the game */ -play(args) - char **args; +static void +play(char **args) { char pbuf[MAXPATHLEN]; - (void)strcpy(pbuf, _PATH_HIDE); - (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game); + snprintf(pbuf, sizeof(pbuf), "%s%s", _PATH_HIDE, game); if (priority > 0) /* < 0 requires root */ (void)setpriority(PRIO_PROCESS, 0, priority); - setgid(getgid()); /* we run setgid kmem; lose it */ execv(pbuf, args); - (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno)); - exit(1); + err(1, "%s", pbuf); } /* * read_config -- * read through config file, looking for key words. */ -read_config() +static void +read_config(void) { FILE *cfp; char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40]; @@ -123,21 +132,21 @@ read_config() if (!(cfp = fopen(_PATH_CONFIG, "r"))) return; while (fgets(lbuf, sizeof(lbuf), cfp)) - switch(*lbuf) { + switch (*lbuf) { case 'b': /* badtty */ - if (sscanf(lbuf, "%s%s", f1, f2) != 2 || + if (sscanf(lbuf, "%39s%39s", f1, f2) != 2 || strcasecmp(f1, "badtty")) break; c_tty(f2); break; case 'g': /* game */ - if (sscanf(lbuf, "%s%s%s%s%s", + if (sscanf(lbuf, "%39s%39s%39s%39s%39s", f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game")) break; c_game(f2, f3, f4, f5); break; case 't': /* time */ - if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 || + if (sscanf(lbuf, "%39s%39s%39s%39s", f1, f2, f3, f4) != 4 || strcasecmp(f1, "time")) break; c_day(f2, f3, f4); @@ -149,10 +158,10 @@ read_config() * c_day -- * if day is today, see if okay to play */ -c_day(s_day, s_start, s_stop) - char *s_day, *s_start, *s_stop; +static void +c_day(const char *s_day, const char *s_start, const char *s_stop) { - static char *days[] = { + static const char *const days[] = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", }; @@ -163,17 +172,17 @@ c_day(s_day, s_start, s_stop) ct = localtime(&now); if (strcasecmp(s_day, days[ct->tm_wday])) return; - if (!isdigit(*s_start) || !isdigit(*s_stop)) + if (!isdigit((unsigned char)*s_start) || + !isdigit((unsigned char)*s_stop)) return; start = atoi(s_start); stop = atoi(s_stop); if (ct->tm_hour >= start && ct->tm_hour < stop) { - fputs("dm: Sorry, games are not available from ", stderr); - hour(start); - fputs(" to ", stderr); - hour(stop); - fputs(" today.\n", stderr); - exit(0); + if (start == 0 && stop == 24) + errx(0, "Sorry, games are not available today."); + else + errx(0, "Sorry, games are not available from %s to %s today.", + hour(start), hour(stop)); } } @@ -181,47 +190,41 @@ c_day(s_day, s_start, s_stop) * c_tty -- * decide if this tty can be used for games. */ -c_tty(tty) - char *tty; +static void +c_tty(const char *tty) { static int first = 1; static char *p_tty; if (first) { - p_tty = rindex(gametty, '/'); + p_tty = strrchr(gametty, '/'); first = 0; } - if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) { - fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty); - exit(0); - } + if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty))) + errx(1, "Sorry, you may not play games on %s.", gametty); } /* * c_game -- * see if game can be played now. */ -c_game(s_game, s_load, s_users, s_priority) - char *s_game, *s_load, *s_users, *s_priority; +static void +c_game(const char *s_game, const char *s_load, const char *s_users, + const char *s_priority) { static int found; - double load(); if (found) return; if (strcmp(game, s_game) && strcasecmp("default", s_game)) return; ++found; - if (isdigit(*s_load) && atoi(s_load) < load()) { - fputs("dm: Sorry, the load average is too high right now.\n", stderr); - exit(0); - } - if (isdigit(*s_users) && atoi(s_users) <= users()) { - fputs("dm: Sorry, there are too many users logged on right now.\n", stderr); - exit(0); - } - if (isdigit(*s_priority)) + if (isdigit((unsigned char)*s_load) && atoi(s_load) < load()) + errx(0, "Sorry, the load average is too high right now."); + if (isdigit((unsigned char)*s_users) && atoi(s_users) <= users()) + errx(0, "Sorry, there are too many users logged on right now."); + if (isdigit((unsigned char)*s_priority)) priority = atoi(s_priority); } @@ -229,16 +232,14 @@ c_game(s_game, s_load, s_users, s_priority) * load -- * return 15 minute load average */ -double -load() +static double +load(void) { double avenrun[3]; - if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) { - fputs("dm: getloadavg() failed.\n", stderr); - exit(1); - } - return(avenrun[2]); + if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) + err(1, "getloadavg() failed"); + return (avenrun[2]); } /* @@ -247,26 +248,20 @@ load() * todo: check idle time; if idle more than X minutes, don't * count them. */ -users() +static int +users(void) { - - register int nusers, utmp; - struct utmp buf; + struct utmpentry *ep; + int nusers; - if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) { - (void)fprintf(stderr, "dm: %s: %s\n", - _PATH_UTMP, strerror(errno)); - exit(1); - } - for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;) - if (buf.ut_name[0] != '\0') - ++nusers; - return(nusers); + nusers = getutentries(NULL, &ep); + return nusers; } -nogamefile() +static void +nogamefile(void) { - register int fd, n; + int fd, n; char buf[BUFSIZ]; if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) { @@ -282,22 +277,19 @@ nogamefile() * hour -- * print out the hour in human form */ -hour(h) - int h; +static const char * +hour(int h) { - switch(h) { - case 0: - fputs("midnight", stderr); - break; - case 12: - fputs("noon", stderr); - break; - default: - if (h > 12) - fprintf(stderr, "%dpm", h - 12); - else - fprintf(stderr, "%dam", h); - } + static const char *const hours[] = { + "midnight", "1am", "2am", "3am", "4am", "5am", + "6am", "7am", "8am", "9am", "10am", "11am", + "noon", "1pm", "2pm", "3pm", "4pm", "5pm", + "6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" }; + + if (h < 0 || h > 24) + return ("BAD TIME"); + else + return (hours[h]); } #ifdef LOG @@ -305,7 +297,8 @@ hour(h) * logfile -- * log play of game */ -logfile() +static void +logfile(void) { struct passwd *pw; FILE *lp; @@ -317,19 +310,19 @@ logfile() if (!flock(fileno(lp), LOCK_EX)) break; if (lock_cnt == 4) { - perror("dm: log lock"); + warnx("log lock"); (void)fclose(lp); return; } - sleep((u_int)1); + sleep(1); } if (pw = getpwuid(uid = getuid())) fputs(pw->pw_name, lp); else fprintf(lp, "%u", uid); fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now)); - (void)fclose(lp); (void)flock(fileno(lp), LOCK_UN); + (void)fclose(lp); } } #endif /* LOG */