BSD 4_4 release
[unix-history] / usr / src / games / dm / dm.c
index d66cb68..b268a5c 100644 (file)
@@ -1,52 +1,74 @@
 /*
 /*
- * 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 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 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
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1987, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dm.c       5.8 (Berkeley) 6/18/88";
+static char sccsid[] = "@(#)dm.c       8.1 (Berkeley) 5/31/93";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/resource.h>
-#include <pwd.h>
-#include <utmp.h>
+
+#include <ctype.h>
 #include <nlist.h>
 #include <nlist.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <utmp.h>
 
 
+#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? */
 
 static time_t  now;                    /* current time value */
 static int     priority = 0;           /* priority game runs at */
 static char    *game,                  /* requested game */
                *gametty;               /* from tty? */
 
-/*ARGSUSED*/
+int
 main(argc, argv)
        int argc;
 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;
 
        nogamefile();
        game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
@@ -68,20 +90,18 @@ main(argc, argv)
  * play --
  *     play the game
  */
  * play --
  *     play the game
  */
-#define        GAMEHIDE        "/usr/games/hide/"
-static
 play(args)
        char **args;
 {
 play(args)
        char **args;
 {
-       char pbuf[MAXPATHLEN], *strcpy();
+       char pbuf[MAXPATHLEN];
 
 
-       (void)strcpy(pbuf, GAMEHIDE);
-       (void)strcpy(pbuf + sizeof(GAMEHIDE) - 1, game);
+       (void)strcpy(pbuf, _PATH_HIDE);
+       (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
        if (priority > 0)       /* < 0 requires root */
                (void)setpriority(PRIO_PROCESS, 0, priority);
        setgid(getgid());       /* we run setgid kmem; lose it */
        execv(pbuf, args);
        if (priority > 0)       /* < 0 requires root */
                (void)setpriority(PRIO_PROCESS, 0, priority);
        setgid(getgid());       /* we run setgid kmem; lose it */
        execv(pbuf, args);
-       perror("dm");
+       (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
        exit(1);
 }
 
        exit(1);
 }
 
@@ -89,29 +109,13 @@ 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()
 {
        FILE *cfp;
 read_config()
 {
        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];
+       char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
 
 
-       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);
-       }
+       if (!(cfp = fopen(_PATH_CONFIG, "r")))
+               return;
        while (fgets(lbuf, sizeof(lbuf), cfp))
                switch(*lbuf) {
                case 'b':               /* badtty */
        while (fgets(lbuf, sizeof(lbuf), cfp))
                switch(*lbuf) {
                case 'b':               /* badtty */
@@ -139,7 +143,6 @@ read_config()
  * c_day --
  *     if day is today, see if okay to play
  */
  * c_day --
  *     if day is today, see if okay to play
  */
-static
 c_day(s_day, s_start, s_stop)
        char *s_day, *s_start, *s_stop;
 {
 c_day(s_day, s_start, s_stop)
        char *s_day, *s_start, *s_stop;
 {
@@ -158,7 +161,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);
@@ -172,13 +175,11 @@ c_day(s_day, s_start, s_stop)
  * c_tty --
  *     decide if this tty can be used for games.
  */
  * c_tty --
  *     decide if this tty can be used for games.
  */
-static
 c_tty(tty)
        char *tty;
 {
        static int first = 1;
        static char *p_tty;
 c_tty(tty)
        char *tty;
 {
        static int first = 1;
        static char *p_tty;
-       char *rindex();
 
        if (first) {
                p_tty = rindex(gametty, '/');
 
        if (first) {
                p_tty = rindex(gametty, '/');
@@ -195,7 +196,6 @@ c_tty(tty)
  * c_game --
  *     see if game can be played now.
  */
  * c_game --
  *     see if game can be played now.
  */
-static
 c_game(s_game, s_load, s_users, s_priority)
        char *s_game, *s_load, *s_users, *s_priority;
 {
 c_game(s_game, s_load, s_users, s_priority)
        char *s_game, *s_load, *s_users, *s_priority;
 {
@@ -219,33 +219,19 @@ c_game(s_game, s_load, s_users, s_priority)
                priority = atoi(s_priority);
 }
 
                priority = atoi(s_priority);
 }
 
-static struct nlist nl[] = {
-       { "_avenrun" },
-#define        X_AVENRUN       0
-       { "" },
-};
-
 /*
  * load --
  *     return 15 minute load average
  */
 /*
  * load --
  *     return 15 minute load average
  */
-static double
+double
 load()
 {
        double avenrun[3];
 load()
 {
        double avenrun[3];
-       int kmem;
-       long lseek();
 
 
-       if (nlist("/vmunix", nl)) {
-               fputs("dm: nlist of /vmunix failed.\n", stderr);
+       if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
+               fputs("dm: getloadavg() failed.\n", stderr);
                exit(1);
        }
                exit(1);
        }
-       if ((kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) {
-               perror("dm: /dev/kmem");
-               exit(1);
-       }
-       (void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET);
-       (void)read(kmem, (char *)avenrun, sizeof(avenrun));
        return(avenrun[2]);
 }
 
        return(avenrun[2]);
 }
 
@@ -255,14 +241,15 @@ load()
  *     todo: check idle time; if idle more than X minutes, don't
  *     count them.
  */
  *     todo: check idle time; if idle more than X minutes, don't
  *     count them.
  */
-static
 users()
 {
 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(_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;)
                exit(1);
        }
        for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
@@ -271,19 +258,12 @@ users()
        return(nusers);
 }
 
        return(nusers);
 }
 
-/*
- * nogamefile --
- *     if the file NOGAMING exists, no games allowed.
- *     file may also contain a message for the user.
- */
-#define        NOGAMING        "/usr/games/nogames"
-static
 nogamefile()
 {
        register int fd, n;
        char buf[BUFSIZ];
 
 nogamefile()
 {
        register int fd, n;
        char buf[BUFSIZ];
 
-       if ((fd = open(NOGAMING, O_RDONLY, 0)) >= 0) {
+       if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
 #define        MESG    "Sorry, no games right now.\n\n"
                (void)write(2, MESG, sizeof(MESG) - 1);
                while ((n = read(fd, buf, sizeof(buf))) > 0)
 #define        MESG    "Sorry, no games right now.\n\n"
                (void)write(2, MESG, sizeof(MESG) - 1);
                while ((n = read(fd, buf, sizeof(buf))) > 0)
@@ -296,7 +276,6 @@ nogamefile()
  * hour --
  *     print out the hour in human form
  */
  * hour --
  *     print out the hour in human form
  */
-static
 hour(h)
        int h;
 {
 hour(h)
        int h;
 {
@@ -320,17 +299,14 @@ hour(h)
  * logfile --
  *     log play of game
  */
  * logfile --
  *     log play of game
  */
-#define        LOGFILE         "/usr/adm/dm.log"
-static
 logfile()
 {
 logfile()
 {
-       struct passwd *pw, *getpwuid();
+       struct passwd *pw;
        FILE *lp;
        uid_t uid;
        int lock_cnt;
        FILE *lp;
        uid_t uid;
        int lock_cnt;
-       char *ctime();
 
 
-       if (lp = fopen(LOGFILE, "a")) {
+       if (lp = fopen(_PATH_LOG, "a")) {
                for (lock_cnt = 0;; ++lock_cnt) {
                        if (!flock(fileno(lp), LOCK_EX))
                                break;
                for (lock_cnt = 0;; ++lock_cnt) {
                        if (!flock(fileno(lp), LOCK_EX))
                                break;