date and time created 88/10/19 15:41:32 by bostic
[unix-history] / usr / src / games / dm / dm.c
index 7b4b943..82bae6c 100644 (file)
@@ -1,18 +1,29 @@
 /*
  * Copyright (c) 1987 Regents of the University of California.
 /*
  * Copyright (c) 1987 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)dm.c       5.4 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)dm.c       5.9 (Berkeley) %G%";
+#endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/file.h>
 
 #include <sys/param.h>
 #include <sys/file.h>
@@ -24,24 +35,18 @@ static char sccsid[] = "@(#)dm.c    5.4 (Berkeley) %G%";
 #include <stdio.h>
 #include <ctype.h>
 
 #include <stdio.h>
 #include <ctype.h>
 
-#define        GAMEHIDE        "/usr/games/hide"
-#define        NOGAMING        "/usr/games/nogames"
-#define        CONTROL         "/usr/games/dm.config"
-#ifdef LOG
-#define        LOGFILE         "/usr/adm/dm.log"
-#endif
-
 static time_t  now;                    /* current time value */
 static int     priority = 0;           /* priority game runs at */
 static char    *game,                  /* requested game */
                *gametty;               /* from tty? */
 
 static time_t  now;                    /* current time value */
 static int     priority = 0;           /* priority game runs at */
 static char    *game,                  /* requested game */
                *gametty;               /* from tty? */
 
+/*ARGSUSED*/
 main(argc, argv)
 main(argc, argv)
-       int     argc;
-       char    **argv;
+       int argc;
+       char **argv;
 {
 {
-       char    *cp, *rindex(), *ttyname();
-       time_t  time();
+       char *cp, *rindex(), *ttyname();
+       time_t time();
 
        nogamefile();
        game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
 
        nogamefile();
        game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
@@ -52,7 +57,6 @@ main(argc, argv)
        gametty = ttyname(0);
        (void)time(&now);
        read_config();
        gametty = ttyname(0);
        (void)time(&now);
        read_config();
-
 #ifdef LOG
        logfile();
 #endif
 #ifdef LOG
        logfile();
 #endif
@@ -64,18 +68,19 @@ main(argc, argv)
  * play --
  *     play the game
  */
  * play --
  *     play the game
  */
+#define        GAMEHIDE        "/usr/games/hide/"
 static
 play(args)
 static
 play(args)
-       char    **args;
+       char **args;
 {
 {
-       if (chdir(GAMEHIDE)) {
-               perror("dm: chdir");
-               exit(1);
-       }
+       char pbuf[MAXPATHLEN], *strcpy();
+
+       (void)strcpy(pbuf, GAMEHIDE);
+       (void)strcpy(pbuf + sizeof(GAMEHIDE) - 1, game);
        if (priority > 0)       /* < 0 requires root */
                (void)setpriority(PRIO_PROCESS, 0, priority);
        setgid(getgid());       /* we run setgid kmem; lose it */
        if (priority > 0)       /* < 0 requires root */
                (void)setpriority(PRIO_PROCESS, 0, priority);
        setgid(getgid());       /* we run setgid kmem; lose it */
-       execv(game, args);
+       execv(pbuf, args);
        perror("dm");
        exit(1);
 }
        perror("dm");
        exit(1);
 }
@@ -84,14 +89,27 @@ play(args)
  * read_config --
  *     read through config file, looking for key words.
  */
  * read_config --
  *     read through config file, looking for key words.
  */
+#define        CONTROL         "/usr/games/dm.config"
 static
 read_config()
 {
 static
 read_config()
 {
-       FILE    *cfp;
-       char    lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
+       FILE *cfp;
+       char *control, *host, *index(), *strcpy();
+       char lbuf[BUFSIZ], path[MAXHOSTNAMELEN + sizeof(CONTROL)];
+       char f1[40], f2[40], f3[40], f4[40], f5[40];
 
 
-       if (!(cfp = fopen(CONTROL, "r"))) {
-               fprintf(stderr, "dm: unable to read %s.\n", CONTROL);
+       host = &path[sizeof(CONTROL)];
+       if (gethostname(host, MAXHOSTNAMELEN)) {
+               perror("dm: gethostname");
+               exit(1);
+       }
+       (void)strcpy(path, control = CONTROL);
+       host[-1] = '.';
+       if (host = index(host, '.'))
+               *host = '\0';
+       if (!(cfp = fopen(path, "r")) && !(cfp = fopen(control, "r"))) {
+               fprintf(stderr, "dm: unable to read %s or %s.\n",
+                   path, control);
                exit(1);
        }
        while (fgets(lbuf, sizeof(lbuf), cfp))
                exit(1);
        }
        while (fgets(lbuf, sizeof(lbuf), cfp))
@@ -123,14 +141,14 @@ read_config()
  */
 static
 c_day(s_day, s_start, s_stop)
  */
 static
 c_day(s_day, s_start, s_stop)
-       char    *s_day, *s_start, *s_stop;
+       char *s_day, *s_start, *s_stop;
 {
 {
-       static char     *days[] = {
+       static char *days[] = {
                "sunday", "monday", "tuesday", "wednesday",
                "thursday", "friday", "saturday",
        };
                "sunday", "monday", "tuesday", "wednesday",
                "thursday", "friday", "saturday",
        };
-       static struct tm        *ct;
-       int     start, stop;
+       static struct tm *ct;
+       int start, stop;
 
        if (!ct)
                ct = localtime(&now);
 
        if (!ct)
                ct = localtime(&now);
@@ -140,7 +158,7 @@ c_day(s_day, s_start, s_stop)
                return;
        start = atoi(s_start);
        stop = atoi(s_stop);
                return;
        start = atoi(s_start);
        stop = atoi(s_stop);
-       if (ct->tm_hour >= start && ct->tm_hour <= stop) {
+       if (ct->tm_hour >= start && ct->tm_hour < stop) {
                fputs("dm: Sorry, games are not available from ", stderr);
                hour(start);
                fputs(" to ", stderr);
                fputs("dm: Sorry, games are not available from ", stderr);
                hour(start);
                fputs(" to ", stderr);
@@ -156,11 +174,11 @@ c_day(s_day, s_start, s_stop)
  */
 static
 c_tty(tty)
  */
 static
 c_tty(tty)
-       char    *tty;
+       char *tty;
 {
 {
-       static int      first = 1;
-       static char     *p_tty;
-       char    *rindex();
+       static int first = 1;
+       static char *p_tty;
+       char *rindex();
 
        if (first) {
                p_tty = rindex(gametty, '/');
 
        if (first) {
                p_tty = rindex(gametty, '/');
@@ -179,10 +197,10 @@ c_tty(tty)
  */
 static
 c_game(s_game, s_load, s_users, s_priority)
  */
 static
 c_game(s_game, s_load, s_users, s_priority)
-       char    *s_game, *s_load, *s_users, *s_priority;
+       char *s_game, *s_load, *s_users, *s_priority;
 {
 {
-       static int      found;
-       double  load();
+       static int found;
+       double load();
 
        if (found)
                return;
 
        if (found)
                return;
@@ -201,7 +219,7 @@ c_game(s_game, s_load, s_users, s_priority)
                priority = atoi(s_priority);
 }
 
                priority = atoi(s_priority);
 }
 
-static struct  nlist nl[] = {
+static struct nlist nl[] = {
        { "_avenrun" },
 #define        X_AVENRUN       0
        { "" },
        { "_avenrun" },
 #define        X_AVENRUN       0
        { "" },
@@ -214,9 +232,9 @@ static struct       nlist nl[] = {
 static double
 load()
 {
 static double
 load()
 {
-       double  avenrun[3];
-       int     kmem;
-       long    lseek();
+       double avenrun[3];
+       int kmem;
+       long lseek();
 
        if (nlist("/vmunix", nl)) {
                fputs("dm: nlist of /vmunix failed.\n", stderr);
 
        if (nlist("/vmunix", nl)) {
                fputs("dm: nlist of /vmunix failed.\n", stderr);
@@ -240,9 +258,8 @@ load()
 static
 users()
 {
 static
 users()
 {
-       register int    nusers,
-                       utmp;
-       struct utmp     buf;
+       register int nusers, utmp;
+       struct utmp buf;
 
        if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) {
                perror("dm: /etc/utmp");
 
        if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) {
                perror("dm: /etc/utmp");
@@ -259,11 +276,12 @@ users()
  *     if the file NOGAMING exists, no games allowed.
  *     file may also contain a message for the user.
  */
  *     if the file NOGAMING exists, no games allowed.
  *     file may also contain a message for the user.
  */
+#define        NOGAMING        "/usr/games/nogames"
 static
 nogamefile()
 {
 static
 nogamefile()
 {
-       register int    fd, n;
-       char    buf[BUFSIZ];
+       register int fd, n;
+       char buf[BUFSIZ];
 
        if ((fd = open(NOGAMING, O_RDONLY, 0)) >= 0) {
 #define        MESG    "Sorry, no games right now.\n\n"
 
        if ((fd = open(NOGAMING, O_RDONLY, 0)) >= 0) {
 #define        MESG    "Sorry, no games right now.\n\n"
@@ -280,7 +298,7 @@ nogamefile()
  */
 static
 hour(h)
  */
 static
 hour(h)
-       int     h;
+       int h;
 {
        switch(h) {
        case 0:
 {
        switch(h) {
        case 0:
@@ -298,14 +316,19 @@ hour(h)
 }
 
 #ifdef LOG
 }
 
 #ifdef LOG
+/*
+ * logfile --
+ *     log play of game
+ */
+#define        LOGFILE         "/usr/adm/dm.log"
 static
 logfile()
 {
 static
 logfile()
 {
-       struct passwd   *pw, *getpwuid();
-       FILE    *lp;
-       uid_t   uid;
-       int     lock_cnt;
-       char    *ctime();
+       struct passwd *pw, *getpwuid();
+       FILE *lp;
+       uid_t uid;
+       int lock_cnt;
+       char *ctime();
 
        if (lp = fopen(LOGFILE, "a")) {
                for (lock_cnt = 0;; ++lock_cnt) {
 
        if (lp = fopen(LOGFILE, "a")) {
                for (lock_cnt = 0;; ++lock_cnt) {
@@ -327,4 +350,4 @@ logfile()
                (void)flock(fileno(lp), LOCK_UN);
        }
 }
                (void)flock(fileno(lp), LOCK_UN);
        }
 }
-#endif
+#endif /* LOG */