X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/9b85680687e77d758ed4d5f002375aa6f88dc803..283621ee3b4a30d66d08567b6507c0ba1580dbf1:/dm/dm.c?ds=inline diff --git a/dm/dm.c b/dm/dm.c index bbd0c447..488110d8 100644 --- a/dm/dm.c +++ b/dm/dm.c @@ -1,6 +1,8 @@ +/* $NetBSD: dm.c,v 1.15 1999/09/22 18:54:42 jsm Exp $ */ + /* - * Copyright (c) 1987 Regents of the University of California. - * All rights reserved. + * Copyright (c) 1987, 1993 + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,49 +33,69 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1987 Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)dm.c 5.16 (Berkeley) 2/28/91"; +#if 0 +static char sccsid[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: dm.c,v 1.15 1999/09/22 18:54:42 jsm Exp $"); +#endif #endif /* not lint */ #include #include #include #include + +#include +#include +#include #include -#include -#include #include -#include +#include #include +#include +#include +#include + #include "pathnames.h" -extern int errno; static time_t now; /* current time value */ static int priority = 0; /* priority game runs at */ static char *game, /* requested game */ *gametty; /* from tty? */ -/*ARGSUSED*/ +void c_day __P((const char *, const char *, const char *)); +void c_game __P((const char *, const char *, const char *, const char *)); +void c_tty __P((const char *)); +const char *hour __P((int)); +double load __P((void)); +int main __P((int, char *[])); +void nogamefile __P((void)); +void play __P((char **)) __attribute__((__noreturn__)); +void read_config __P((void)); +int users __P((void)); + +int main(argc, argv) int argc; - char **argv; + char *argv[]; { - char *cp, *rindex(), *ttyname(); - time_t time(); + 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 @@ -81,31 +103,34 @@ main(argc, argv) #endif play(argv); /*NOTREACHED*/ + return (0); } /* * play -- * play the game */ +void play(args) char **args; { char pbuf[MAXPATHLEN]; - (void)strcpy(pbuf, _PATH_HIDE); - (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game); + (void)strncpy(pbuf, _PATH_HIDE, sizeof(pbuf) - 1); + (void)strncpy(pbuf + sizeof(_PATH_HIDE) - 1, game, + sizeof(pbuf) - sizeof(_PATH_HIDE) - 1); + pbuf[sizeof(pbuf) - 1] = '\0'; 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. */ +void read_config() { FILE *cfp; @@ -114,7 +139,7 @@ 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 || strcasecmp(f1, "badtty")) @@ -140,10 +165,11 @@ read_config() * c_day -- * if day is today, see if okay to play */ +void c_day(s_day, s_start, s_stop) - char *s_day, *s_start, *s_stop; + const char *s_day, *s_start, *s_stop; { - static char *days[] = { + static const char *const days[] = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", }; @@ -159,12 +185,11 @@ c_day(s_day, s_start, s_stop) 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)); } } @@ -172,47 +197,41 @@ c_day(s_day, s_start, s_stop) * c_tty -- * decide if this tty can be used for games. */ +void c_tty(tty) - char *tty; + const char *tty; { static int first = 1; static char *p_tty; - char *rindex(); 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. */ +void c_game(s_game, s_load, s_users, s_priority) - char *s_game, *s_load, *s_users, *s_priority; + const char *s_game, *s_load, *s_users, *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_load) && atoi(s_load) < load()) + errx(0, "Sorry, the load average is too high right now."); + if (isdigit(*s_users) && atoi(s_users) <= users()) + errx(0, "Sorry, there are too many users logged on right now."); if (isdigit(*s_priority)) priority = atoi(s_priority); } @@ -226,11 +245,9 @@ load() { 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]); } /* @@ -239,26 +256,25 @@ load() * todo: check idle time; if idle more than X minutes, don't * count them. */ +int users() { - register int nusers, utmp; + int nusers, utmp; struct utmp buf; - if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) { - (void)fprintf(stderr, "dm: %s: %s\n", - _PATH_UTMP, strerror(errno)); - exit(1); - } + if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) + err(1, "%s", _PATH_UTMP); for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;) if (buf.ut_name[0] != '\0') ++nusers; - return(nusers); + return (nusers); } +void nogamefile() { - register int fd, n; + int fd, n; char buf[BUFSIZ]; if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) { @@ -274,22 +290,20 @@ nogamefile() * hour -- * print out the hour in human form */ +const char * hour(h) 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 @@ -299,18 +313,17 @@ hour(h) */ logfile() { - struct passwd *pw, *getpwuid(); + struct passwd *pw; FILE *lp; uid_t uid; int lock_cnt; - char *ctime(); if (lp = fopen(_PATH_LOG, "a")) { for (lock_cnt = 0;; ++lock_cnt) { if (!flock(fileno(lp), LOCK_EX)) break; if (lock_cnt == 4) { - perror("dm: log lock"); + warnx("log lock"); (void)fclose(lp); return; }