]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - dm/dm.c
1 /* $NetBSD: dm.c,v 1.8 1997/11/19 08:24:09 mrg Exp $ */
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: dm.c,v 1.8 1997/11/19 08:24:09 mrg Exp $");
50 #include <sys/param.h>
53 #include <sys/resource.h>
67 #include "pathnames.h"
69 static time_t now
; /* current time value */
70 static int priority
= 0; /* priority game runs at */
71 static char *game
, /* requested game */
72 *gametty
; /* from tty? */
74 void c_day
__P((char *, char *, char *));
75 void c_game
__P((char *, char *, char *, char *));
76 void c_tty
__P((char *));
77 const char *hour
__P((int));
78 double load
__P((void));
79 int main
__P((int, char *[]));
80 void nogamefile
__P((void));
81 void play
__P((char **));
82 void read_config
__P((void));
83 int users
__P((void));
93 game
= (cp
= strrchr(*argv
, '/')) ? ++cp
: *argv
;
95 if (!strcmp(game
, "dm"))
117 char pbuf
[MAXPATHLEN
];
119 (void)strncpy(pbuf
, _PATH_HIDE
, sizeof(pbuf
) - 1);
120 (void)strncpy(pbuf
+ sizeof(_PATH_HIDE
) - 1, game
,
121 sizeof(pbuf
) - sizeof(_PATH_HIDE
) - 1);
122 pbuf
[sizeof(pbuf
) - 1] = '\0';
123 if (priority
> 0) /* < 0 requires root */
124 (void)setpriority(PRIO_PROCESS
, 0, priority
);
131 * read through config file, looking for key words.
137 char lbuf
[BUFSIZ
], f1
[40], f2
[40], f3
[40], f4
[40], f5
[40];
139 if (!(cfp
= fopen(_PATH_CONFIG
, "r")))
141 while (fgets(lbuf
, sizeof(lbuf
), cfp
))
143 case 'b': /* badtty */
144 if (sscanf(lbuf
, "%s%s", f1
, f2
) != 2 ||
145 strcasecmp(f1
, "badtty"))
150 if (sscanf(lbuf
, "%s%s%s%s%s",
151 f1
, f2
, f3
, f4
, f5
) != 5 || strcasecmp(f1
, "game"))
153 c_game(f2
, f3
, f4
, f5
);
156 if (sscanf(lbuf
, "%s%s%s%s", f1
, f2
, f3
, f4
) != 4 ||
157 strcasecmp(f1
, "time"))
166 * if day is today, see if okay to play
169 c_day(s_day
, s_start
, s_stop
)
170 char *s_day
, *s_start
, *s_stop
;
172 static char *days
[] = {
173 "sunday", "monday", "tuesday", "wednesday",
174 "thursday", "friday", "saturday",
176 static struct tm
*ct
;
180 ct
= localtime(&now
);
181 if (strcasecmp(s_day
, days
[ct
->tm_wday
]))
183 if (!isdigit(*s_start
) || !isdigit(*s_stop
))
185 start
= atoi(s_start
);
187 if (ct
->tm_hour
>= start
&& ct
->tm_hour
< stop
)
188 errx(0, "Sorry, games are not available from %s to %s today.",
189 hour(start
), hour(stop
));
194 * decide if this tty can be used for games.
200 static int first
= 1;
204 p_tty
= strrchr(gametty
, '/');
208 if (!strcmp(gametty
, tty
) || (p_tty
&& !strcmp(p_tty
, tty
)))
209 errx(1, "Sorry, you may not play games on %s.", gametty
);
214 * see if game can be played now.
217 c_game(s_game
, s_load
, s_users
, s_priority
)
218 char *s_game
, *s_load
, *s_users
, *s_priority
;
224 if (strcmp(game
, s_game
) && strcasecmp("default", s_game
))
227 if (isdigit(*s_load
) && atoi(s_load
) < load())
228 errx(0, "Sorry, the load average is too high right now.");
229 if (isdigit(*s_users
) && atoi(s_users
) <= users())
230 errx(0, "Sorry, there are too many users logged on right now.");
231 if (isdigit(*s_priority
))
232 priority
= atoi(s_priority
);
237 * return 15 minute load average
244 if (getloadavg(avenrun
, sizeof(avenrun
)/sizeof(avenrun
[0])) < 0)
245 err(1, "getloadavg() failed.");
251 * return current number of users
252 * todo: check idle time; if idle more than X minutes, don't
262 if ((utmp
= open(_PATH_UTMP
, O_RDONLY
, 0)) < 0)
263 err(1, "%s", _PATH_UTMP
);
264 for (nusers
= 0; read(utmp
, (char *)&buf
, sizeof(struct utmp
)) > 0;)
265 if (buf
.ut_name
[0] != '\0')
276 if ((fd
= open(_PATH_NOGAMES
, O_RDONLY
, 0)) >= 0) {
277 #define MESG "Sorry, no games right now.\n\n"
278 (void)write(2, MESG
, sizeof(MESG
) - 1);
279 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0)
280 (void)write(2, buf
, n
);
287 * print out the hour in human form
293 static char *hours
[] = {
294 "midnight", "1am", "2am", "3am", "4am", "5am",
295 "6am", "7am", "8am", "9am", "10am", "11am",
296 "noon", "1pm", "2pm", "3pm", "4pm", "5pm",
297 "6pm", "7pm", "8pm", "9pm", "10pm", "11pm" };
317 if (lp
= fopen(_PATH_LOG
, "a")) {
318 for (lock_cnt
= 0;; ++lock_cnt
) {
319 if (!flock(fileno(lp
), LOCK_EX
))
328 if (pw
= getpwuid(uid
= getuid()))
329 fputs(pw
->pw_name
, lp
);
331 fprintf(lp
, "%u", uid
);
332 fprintf(lp
, "\t%s\t%s\t%s", game
, gametty
, ctime(&now
));
334 (void)flock(fileno(lp
), LOCK_UN
);