]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - sail/sync.c
More rationalization of include files:
[bsdgames-darwin.git] / sail / sync.c
index ec4fc61dc9bfbc073d6d6d19a3dfb7e34eafc9d9..d793aac551986648f4e5733e07620e6df54c056e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: sync.c,v 1.3 1995/04/22 10:37:26 cgd Exp $     */
+/*     $NetBSD: sync.c,v 1.18 2001/01/04 03:51:24 jwise Exp $  */
 
 /*
  * Copyright (c) 1983, 1993
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
 #if 0
-static char sccsid[] = "@(#)sync.c     8.1 (Berkeley) 5/31/93";
+static char sccsid[] = "@(#)sync.c     8.2 (Berkeley) 4/28/95";
 #else
-static char rcsid[] = "$NetBSD: sync.c,v 1.3 1995/04/22 10:37:26 cgd Exp $";
+__RCSID("$NetBSD: sync.c,v 1.18 2001/01/04 03:51:24 jwise Exp $");
 #endif
 #endif /* not lint */
 
-#include "externs.h"
-#include <sys/file.h>
-#include <sys/errno.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "extern.h"
+#include "pathnames.h"
 
 #define BUFSIZE 4096
 
+void   fmtship(char *, size_t, const char *, struct ship *);
+void   makesignal(struct ship *, const char *, struct ship *, ...);
+void   makemsg(struct ship *, const char *, ...);
+int    sync_exists(int);
+int    sync_open(void);
+void   sync_close(int);
+void   Write(int, struct ship *, long, long, long, long);
+void   Writestr(int, struct ship *, const char *);
+int    Sync(void);
+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[25];
-static char sync_file[25];
+static char sync_lock[sizeof SF];
+static char sync_file[sizeof LF];
 static long sync_seek;
 static FILE *sync_fp;
-#define SF "/tmp/#sailsink.%d"
-#define LF "/tmp/#saillock.%d"
+
+void
+fmtship(char *buf, size_t len, const char *fmt, struct ship *ship)
+{
+       while (*fmt) {
+               if (len-- == 0) {
+                       *buf = '\0';
+                       return;
+               }
+               if (*fmt == '$' && fmt[1] == '$') {
+                       size_t l = snprintf(buf, len, "%s (%c%c)",
+                           ship->shipname, colours(ship), sterncolour(ship));
+                       buf += l;
+                       len -= l - 1;
+                       fmt += 2;
+               }
+               else
+                       *buf++ = *fmt++;
+       }
+
+       if (len > 0)
+               *buf = '\0';
+}
+
 
 /*VARARGS3*/
-makesignal(from, fmt, ship, a, b, c)
-       struct ship *from;
-       char *fmt;
-       register struct ship *ship;
+void
+makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
+{
+       char message[BUFSIZ];
+       char format[BUFSIZ];
+       va_list ap;
+
+       va_start(ap, ship);
+       fmtship(format, sizeof(format), fmt, ship);
+       vsprintf(message, format, ap);
+       va_end(ap);
+       Writestr(W_SIGNAL, from, message);
+}
+
+/*VARARGS2*/
+void
+makemsg(struct ship *from, const char *fmt, ...)
 {
-       char message[80];
+       char message[BUFSIZ];
+       va_list ap;
 
-       if (ship == 0)
-               (void) sprintf(message, fmt, a, b, c);
-       else
-               (void) sprintf(message, fmt,
-                       ship->shipname, colours(ship),
-                       sterncolour(ship), a, b, c);
-       Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
+       va_start(ap, fmt);
+       vsprintf(message, fmt, ap);
+       va_end(ap);
+       Writestr(W_SIGNAL, from, message);
 }
 
-#include <sys/types.h>
-#include <sys/stat.h>
-sync_exists(game)
+int
+sync_exists(int game)
 {
        char buf[sizeof sync_file];
        struct stat s;
        time_t t;
 
-       (void) sprintf(buf, SF, game);
-       (void) time(&t);
-       if (stat(buf, &s) < 0)
+       sprintf(buf, SF, game);
+       time(&t);
+       setegid(egid);
+       if (stat(buf, &s) < 0) {
+               setegid(gid);
                return 0;
+       }
        if (s.st_mtime < t - 60*60*2) {         /* 2 hours */
-               (void) unlink(buf);
-               (void) sprintf(buf, LF, game);
-               (void) unlink(buf);
+               unlink(buf);
+               sprintf(buf, LF, game);
+               unlink(buf);
+               setegid(gid);
                return 0;
-       } else
+       } else {
+               setegid(gid);
                return 1;
+       }
 }
 
-sync_open()
+int
+sync_open(void)
 {
+       struct stat tmp;
        if (sync_fp != NULL)
-               (void) fclose(sync_fp);
-       (void) sprintf(sync_lock, LF, game);
-       (void) sprintf(sync_file, SF, game);
-       if (access(sync_file, 0) < 0) {
-               int omask = umask(issetuid ? 077 : 011);
+               fclose(sync_fp);
+       sprintf(sync_lock, LF, game);
+       sprintf(sync_file, SF, game);
+       setegid(egid);
+       if (stat(sync_file, &tmp) < 0) {
+               mode_t omask = umask(002);
                sync_fp = fopen(sync_file, "w+");
-               (void) umask(omask);
+               umask(omask);
        } else
                sync_fp = fopen(sync_file, "r+");
+       setegid(gid);
        if (sync_fp == NULL)
                return -1;
        sync_seek = 0;
        return 0;
 }
 
-sync_close(remove)
-       char remove;
+void
+sync_close(int remove)
 {
        if (sync_fp != 0)
-               (void) fclose(sync_fp);
-       if (remove)
-               (void) unlink(sync_file);
+               fclose(sync_fp);
+       if (remove) {
+               setegid(egid);
+               unlink(sync_file);
+               setegid(gid);
+       }
+}
+
+void
+Write(int type, struct ship *ship, long a, long b, long c, long d)
+{
+
+       sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n",
+                      type, ship->file->index, a, b, c, d);
+       while (*sync_bp++)
+               ;
+       sync_bp--;
+       if (sync_bp >= &sync_buf[sizeof sync_buf])
+               abort();
+       sync_update(type, ship, NULL, a, b, c, d);
 }
 
-Write(type, ship, isstr, a, b, c, d)
-       int type;
-       struct ship *ship;
-       char isstr;
-       int a, b, c, d;
+void
+Writestr(int type, struct ship *ship, const char *a)
 {
-       if (isstr)
-               (void) sprintf(sync_bp, "%d %d %d %s\n",
-                       type, ship->file->index, isstr, a);
-       else
-               (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
-                       type, ship->file->index, isstr, a, b, c, d);
+       sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
        while (*sync_bp++)
                ;
        sync_bp--;
        if (sync_bp >= &sync_buf[sizeof sync_buf])
                abort();
-       (void) sync_update(type, ship, a, b, c, d);
+       sync_update(type, ship, a, 0, 0, 0, 0);
 }
 
-Sync()
+int
+Sync(void)
 {
        sig_t sighup, sigint;
-       register n;
-       int type, shipnum, isstr, a, b, c, d;
+       int n;
+       int type, shipnum, isstr;
+       char *astr;
+       long a, b, c, d;
        char buf[80];
        char erred = 0;
-       extern errno;
 
        sighup = signal(SIGHUP, SIG_IGN);
        sigint = signal(SIGINT, SIG_IGN);
@@ -159,8 +234,12 @@ Sync()
                if (errno != EWOULDBLOCK)
                        return -1;
 #else
-               if (link(sync_file, sync_lock) >= 0)
+               setegid(egid);
+               if (link(sync_file, sync_lock) >= 0) {
+                       setegid(gid);
                        break;
+               }
+               setegid(gid);
                if (errno != EEXIST)
                        return -1;
 #endif
@@ -168,7 +247,7 @@ Sync()
        }
        if (n <= 0)
                return -1;
-       (void) fseek(sync_fp, sync_seek, 0);
+       fseek(sync_fp, sync_seek, SEEK_SET);
        for (;;) {
                switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
                case 3:
@@ -183,7 +262,7 @@ Sync()
                if (isstr != 0 && isstr != 1)
                        goto bad;
                if (isstr) {
-                       register char *p;
+                       char *p;
                        for (p = buf;;) {
                                switch (*p++ = getc(sync_fp)) {
                                case '\n':
@@ -200,57 +279,59 @@ Sync()
                        *p = 0;
                        for (p = buf; *p == ' '; p++)
                                ;
-                       a = (int)p;
-                       b = c = d = 0;
-               } else
-                       if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
+                       astr = p;
+                       a = b = c = d = 0;
+               } else {
+                       if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4)
                                goto bad;
-               if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
+                       astr = NULL;
+               }
+               if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0)
                        goto bad;
        }
 bad:
        erred++;
 out:
        if (!erred && sync_bp != sync_buf) {
-               (void) fseek(sync_fp, 0L, 2);
-               (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
+               fseek(sync_fp, 0L, SEEK_END);
+               fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
                        sync_fp);
-               (void) fflush(sync_fp);
+               fflush(sync_fp);
                sync_bp = sync_buf;
        }
        sync_seek = ftell(sync_fp);
 #ifdef LOCK_EX
-       (void) flock(fileno(sync_fp), LOCK_UN);
+       flock(fileno(sync_fp), LOCK_UN);
 #else
-       (void) unlink(sync_lock);
+       setegid(egid);
+       unlink(sync_lock);
+       setegid(gid);
 #endif
-       (void) signal(SIGHUP, sighup);
-       (void) signal(SIGINT, sigint);
+       signal(SIGHUP, sighup);
+       signal(SIGINT, sigint);
        return erred ? -1 : 0;
 }
 
-sync_update(type, ship, a, b, c, d)
-       int type;
-       register struct ship *ship;
-       int a, b, c, d;
+static int
+sync_update(int type, struct ship *ship, const char *astr, long a, long b, long c, long d)
 {
        switch (type) {
        case W_DBP: {
-               register struct BP *p = &ship->file->DBP[a];
+               struct BP *p = &ship->file->DBP[a];
                p->turnsent = b;
                p->toship = SHIP(c);
                p->mensent = d;
                break;
                }
        case W_OBP: {
-               register struct BP *p = &ship->file->OBP[a];
+               struct BP *p = &ship->file->OBP[a];
                p->turnsent = b;
                p->toship = SHIP(c);
                p->mensent = d;
                break;
                }
        case W_FOUL: {
-               register struct snag *p = &ship->file->foul[a];
+               struct snag *p = &ship->file->foul[a];
                if (SHIP(a)->file->dir == 0)
                        break;
                if (p->sn_count++ == 0)
@@ -259,7 +340,7 @@ sync_update(type, ship, a, b, c, d)
                break;
                }
        case W_GRAP: {
-               register struct snag *p = &ship->file->grap[a];
+               struct snag *p = &ship->file->grap[a];
                if (SHIP(a)->file->dir == 0)
                        break;
                if (p->sn_count++ == 0)
@@ -268,8 +349,8 @@ sync_update(type, ship, a, b, c, d)
                break;
                }
        case W_UNFOUL: {
-               register struct snag *p = &ship->file->foul[a];
-               if (p->sn_count > 0)
+               struct snag *p = &ship->file->foul[a];
+               if (p->sn_count > 0) {
                        if (b) {
                                ship->file->nfoul -= p->sn_count;
                                p->sn_count = 0;
@@ -277,11 +358,12 @@ sync_update(type, ship, a, b, c, d)
                                ship->file->nfoul--;
                                p->sn_count--;
                        }
+               }
                break;
                }
        case W_UNGRAP: {
-               register struct snag *p = &ship->file->grap[a];
-               if (p->sn_count > 0)
+               struct snag *p = &ship->file->grap[a];
+               if (p->sn_count > 0) {
                        if (b) {
                                ship->file->ngrap -= p->sn_count;
                                p->sn_count = 0;
@@ -289,24 +371,26 @@ sync_update(type, ship, a, b, c, d)
                                ship->file->ngrap--;
                                p->sn_count--;
                        }
+               }
                break;
                }
        case W_SIGNAL:
-               if (mode == MODE_PLAYER)
+               if (mode == MODE_PLAYER) {
                        if (nobells)
-                               Signal("%s (%c%c): %s", ship, a);
+                               Signal("$$: %s", ship, astr);
                        else
-                               Signal("\7%s (%c%c): %s", ship, a);
+                               Signal("\7$$: %s", ship, astr);
+               }
                break;
        case W_CREW: {
-               register struct shipspecs *s = ship->specs;
+               struct shipspecs *s = ship->specs;
                s->crew1 = a;
                s->crew2 = b;
                s->crew3 = c;
                break;
                }
        case W_CAPTAIN:
-               (void) strncpy(ship->file->captain, (char *)a,
+               strncpy(ship->file->captain, astr,
                        sizeof ship->file->captain - 1);
                ship->file->captain[sizeof ship->file->captain - 1] = 0;
                break;
@@ -330,13 +414,13 @@ sync_update(type, ship, a, b, c, d)
                ship->file->FS = a;
                break;
        case W_GUNL: {
-               register struct shipspecs *s = ship->specs;
+               struct shipspecs *s = ship->specs;
                s->gunL = a;
                s->carL = b;
                break;
                }
        case W_GUNR: {
-               register struct shipspecs *s = ship->specs;
+               struct shipspecs *s = ship->specs;
                s->gunR = a;
                s->carR = b;
                break;
@@ -345,7 +429,7 @@ sync_update(type, ship, a, b, c, d)
                ship->specs->hull = a;
                break;
        case W_MOVE:
-               (void) strncpy(ship->file->movebuf, (char *)a,
+               strncpy(ship->file->movebuf, astr,
                        sizeof ship->file->movebuf - 1);
                ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
                break;
@@ -359,7 +443,7 @@ sync_update(type, ship, a, b, c, d)
                ship->specs->qual = a;
                break;
        case W_RIGG: {
-               register struct shipspecs *s = ship->specs;
+               struct shipspecs *s = ship->specs;
                s->rig1 = a;
                s->rig2 = b;
                s->rig3 = c;
@@ -408,7 +492,7 @@ sync_update(type, ship, a, b, c, d)
                windspeed = b;
                break;
        case W_BEGIN:
-               (void) strcpy(ship->file->captain, "begin");
+               strcpy(ship->file->captain, "begin");
                people++;
                break;
        case W_END: