]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - sail/sync.c
fortune: arc4random_uniform for better uniform values than random() % ...
[bsdgames-darwin.git] / sail / sync.c
index 83f9e19b25509a2543bb3c32a70965999d6e4fbd..26b4c10c805b4302b93844fb5858f457d22ad77d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: sync.c,v 1.30 2009/03/14 22:52:53 dholland Exp $       */
+/*     $NetBSD: sync.c,v 1.34 2013/10/19 17:23:08 christos Exp $       */
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)sync.c     8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: sync.c,v 1.30 2009/03/14 22:52:53 dholland Exp $");
+__RCSID("$NetBSD: sync.c,v 1.34 2013/10/19 17:23:08 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -42,6 +42,7 @@ __RCSID("$NetBSD: sync.c,v 1.30 2009/03/14 22:52:53 dholland Exp $");
 
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -62,7 +63,7 @@ __RCSID("$NetBSD: sync.c,v 1.30 2009/03/14 22:52:53 dholland Exp $");
 #define W_DBP          5
 #define W_DRIFT                6
 #define W_EXPLODE      7
-#define W_FILE         8
+/*      W_FILE         8   not used */
 #define W_FOUL         9
 #define W_GUNL         10
 #define W_GUNR         11
@@ -103,7 +104,6 @@ static void recv_crew(struct ship *ship, long a, long b, long c);
 static void recv_dbp(struct ship *ship, long a, long b, long c, long d);
 static void recv_drift(struct ship *ship, long a);
 static void recv_explode(struct ship *ship, long a);
-static void recv_file(void);
 static void recv_foul(struct ship *ship, long a);
 static void recv_gunl(struct ship *ship, long a, long b);
 static void recv_gunr(struct ship *ship, long a, long b);
@@ -142,15 +142,29 @@ static void Writestr(int, struct ship *, const char *);
 static int sync_update(int, struct ship *, const char *,
                       long, long, long, long);
 
-static const char SF[] = _PATH_SYNC;
-static const char LF[] = _PATH_LOCK;
 static char sync_buf[BUFSIZE];
 static char *sync_bp = sync_buf;
-static char sync_lock[sizeof SF];
-static char sync_file[sizeof LF];
 static long sync_seek;
 static FILE *sync_fp;
 
+static const char *
+get_sync_file(int scenario_number)
+{
+       static char sync_file[NAME_MAX];
+
+       snprintf(sync_file, sizeof(sync_file), _FILE_SYNC, scenario_number);
+       return sync_file;
+}
+
+static const char *
+get_lock_file(int scenario_number)
+{
+       static char sync_lock[NAME_MAX];
+
+       snprintf(sync_lock, sizeof(sync_lock), _FILE_LOCK, scenario_number);
+       return sync_lock;
+}
+
 void
 fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
 {
@@ -206,21 +220,21 @@ makemsg(struct ship *from, const char *fmt, ...)
 int
 sync_exists(int gamenum)
 {
-       char buf[sizeof sync_file];
+       const char *path;
        struct stat s;
        time_t t;
 
-       snprintf(buf, sizeof(buf), SF, gamenum);
+       path = get_sync_file(gamenum);
        time(&t);
        setegid(egid);
-       if (stat(buf, &s) < 0) {
+       if (stat(path, &s) < 0) {
                setegid(gid);
                return 0;
        }
        if (s.st_mtime < t - 60*60*2) {         /* 2 hours */
-               unlink(buf);
-               snprintf(buf, sizeof(buf), LF, gamenum);
-               unlink(buf);
+               unlink(path);
+               path = get_lock_file(gamenum);
+               unlink(path);
                setegid(gid);
                return 0;
        } else {
@@ -232,11 +246,13 @@ sync_exists(int gamenum)
 int
 sync_open(void)
 {
+       const char *sync_file;
        struct stat tmp;
+
        if (sync_fp != NULL)
                fclose(sync_fp);
-       snprintf(sync_lock, sizeof(sync_lock), LF, game);
-       snprintf(sync_file, sizeof(sync_file), SF, game);
+       sync_file = get_sync_file(game);
+       (void)get_lock_file(game);
        setegid(egid);
        if (stat(sync_file, &tmp) < 0) {
                mode_t omask = umask(002);
@@ -254,9 +270,12 @@ sync_open(void)
 void
 sync_close(int doremove)
 {
+       const char *sync_file;
+
        if (sync_fp != 0)
                fclose(sync_fp);
        if (doremove) {
+               sync_file = get_sync_file(game);
                setegid(egid);
                unlink(sync_file);
                setegid(gid);
@@ -304,6 +323,10 @@ Sync(void)
        long a, b, c, d;
        char buf[80];
        char erred = 0;
+#ifndef LOCK_EX
+       const char *sync_file;
+       const char *sync_lock;
+#endif
 
        sighup = signal(SIGHUP, SIG_IGN);
        sigint = signal(SIGINT, SIG_IGN);
@@ -314,6 +337,8 @@ Sync(void)
                if (errno != EWOULDBLOCK)
                        return -1;
 #else
+               sync_file = get_sync_file(game);
+               sync_lock = get_lock_file(game);
                setegid(egid);
                if (link(sync_file, sync_lock) >= 0) {
                        setegid(gid);
@@ -409,7 +434,6 @@ sync_update(int type, struct ship *ship, const char *astr,
        case W_DBP:      recv_dbp(ship, a, b, c, d);  break;
        case W_DRIFT:    recv_drift(ship, a);         break;
        case W_EXPLODE:  recv_explode(ship, a);       break;
-       case W_FILE:     recv_file();                 break;
        case W_FOUL:     recv_foul(ship, a);          break;
        case W_GUNL:     recv_gunl(ship, a, b);       break;
        case W_GUNR:     recv_gunr(ship, a, b);       break;
@@ -494,12 +518,6 @@ send_explode(struct ship *ship, long a)
        Write(W_EXPLODE, ship, a, 0, 0, 0);
 }
 
-void
-send_file(void)
-{
-       Write(W_FILE, NULL, 0, 0, 0, 0);
-}
-
 void
 send_foul(struct ship *ship, long a)
 {
@@ -745,12 +763,6 @@ recv_explode(struct ship *ship, long a)
                ship->file->dir = 0;
 }
 
-// XXX why does this exist?
-static void
-recv_file(void)
-{
-}
-
 static void
 recv_foul(struct ship *ship, long a)
 {