BSD 4_4_Lite2 release
[unix-history] / usr / src / games / sail / sync.c
index 059d9f7..8f8dc7d 100644 (file)
@@ -1,8 +1,45 @@
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * 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
-static char *sccsid = "@(#)sync.c      2.4 84/01/27";
-#endif
+static char sccsid[] = "@(#)sync.c     8.2 (Berkeley) 4/28/95";
+#endif /* not lint */
+
+#include <sys/file.h>
+#include <errno.h>
+#include "extern.h"
 
 
-#include "externs.h"
+#define BUFSIZE 4096
 
 static char sync_buf[BUFSIZE];
 static char *sync_bp = sync_buf;
 
 static char sync_buf[BUFSIZE];
 static char *sync_bp = sync_buf;
@@ -15,9 +52,9 @@ static FILE *sync_fp;
 
 /*VARARGS3*/
 makesignal(from, fmt, ship, a, b, c)
 
 /*VARARGS3*/
 makesignal(from, fmt, ship, a, b, c)
-struct ship *from;
-char *fmt;
-register struct ship *ship;
+       struct ship *from;
+       char *fmt;
+       register struct ship *ship;
 {
        char message[80];
 
 {
        char message[80];
 
@@ -43,6 +80,8 @@ sync_exists(game)
        if (stat(buf, &s) < 0)
                return 0;
        if (s.st_mtime < t - 60*60*2) {         /* 2 hours */
        if (stat(buf, &s) < 0)
                return 0;
        if (s.st_mtime < t - 60*60*2) {         /* 2 hours */
+               (void) unlink(buf);
+               (void) sprintf(buf, LF, game);
                (void) unlink(buf);
                return 0;
        } else
                (void) unlink(buf);
                return 0;
        } else
@@ -51,27 +90,24 @@ sync_exists(game)
 
 sync_open()
 {
 
 sync_open()
 {
+       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) {
        (void) sprintf(sync_lock, LF, game);
        (void) sprintf(sync_file, SF, game);
        if (access(sync_file, 0) < 0) {
-               int omask;
-#ifdef SETUID
-               omask = umask(077);
-#else
-               omask = umask(011);
-#endif
+               int omask = umask(issetuid ? 077 : 011);
                sync_fp = fopen(sync_file, "w+");
                (void) umask(omask);
        } else
                sync_fp = fopen(sync_file, "r+");
                sync_fp = fopen(sync_file, "w+");
                (void) umask(omask);
        } else
                sync_fp = fopen(sync_file, "r+");
-       if (sync_fp == 0)
+       if (sync_fp == NULL)
                return -1;
                return -1;
-       sync_seek == 0;
+       sync_seek = 0;
        return 0;
 }
 
 sync_close(remove)
        return 0;
 }
 
 sync_close(remove)
-char remove;
+       char remove;
 {
        if (sync_fp != 0)
                (void) fclose(sync_fp);
 {
        if (sync_fp != 0)
                (void) fclose(sync_fp);
@@ -80,10 +116,10 @@ char remove;
 }
 
 Write(type, ship, isstr, a, b, c, d)
 }
 
 Write(type, ship, isstr, a, b, c, d)
-int type;
-struct ship *ship;
-char isstr;
-int a, b, c, d;
+       int type;
+       struct ship *ship;
+       char isstr;
+       int a, b, c, d;
 {
        if (isstr)
                (void) sprintf(sync_bp, "%d %d %d %s\n",
 {
        if (isstr)
                (void) sprintf(sync_bp, "%d %d %d %s\n",
@@ -96,24 +132,52 @@ int a, b, c, d;
        sync_bp--;
        if (sync_bp >= &sync_buf[sizeof sync_buf])
                abort();
        sync_bp--;
        if (sync_bp >= &sync_buf[sizeof sync_buf])
                abort();
-       sync_update(type, ship, a, b, c, d);
+       (void) sync_update(type, ship, a, b, c, d);
 }
 
 Sync()
 {
 }
 
 Sync()
 {
-       int (*sig1)(), (*sig2)();
+       sig_t sighup, sigint;
+       register n;
        int type, shipnum, isstr, a, b, c, d;
        int type, shipnum, isstr, a, b, c, d;
-       char buf[60];
-       register char *p = buf;
-       int n;
+       char buf[80];
+       char erred = 0;
+       extern errno;
 
 
-       sig1 = signal(SIGHUP, SIG_IGN);
-       sig2 = signal(SIGINT, SIG_IGN);
-       for (n = 0; link(sync_file, sync_lock) < 0 && n < 30; n++)
-               sleep(2);
+       sighup = signal(SIGHUP, SIG_IGN);
+       sigint = signal(SIGINT, SIG_IGN);
+       for (n = TIMEOUT; --n >= 0;) {
+#ifdef LOCK_EX
+               if (flock(fileno(sync_fp), LOCK_EX|LOCK_NB) >= 0)
+                       break;
+               if (errno != EWOULDBLOCK)
+                       return -1;
+#else
+               if (link(sync_file, sync_lock) >= 0)
+                       break;
+               if (errno != EEXIST)
+                       return -1;
+#endif
+               sleep(1);
+       }
+       if (n <= 0)
+               return -1;
        (void) fseek(sync_fp, sync_seek, 0);
        (void) fseek(sync_fp, sync_seek, 0);
-       while (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr) != EOF) {
+       for (;;) {
+               switch (fscanf(sync_fp, "%d%d%d", &type, &shipnum, &isstr)) {
+               case 3:
+                       break;
+               case EOF:
+                       goto out;
+               default:
+                       goto bad;
+               }
+               if (shipnum < 0 || shipnum >= cc->vessels)
+                       goto bad;
+               if (isstr != 0 && isstr != 1)
+                       goto bad;
                if (isstr) {
                if (isstr) {
+                       register char *p;
                        for (p = buf;;) {
                                switch (*p++ = getc(sync_fp)) {
                                case '\n':
                        for (p = buf;;) {
                                switch (*p++ = getc(sync_fp)) {
                                case '\n':
@@ -121,6 +185,8 @@ Sync()
                                case EOF:
                                        break;
                                default:
                                case EOF:
                                        break;
                                default:
+                                       if (p >= buf + sizeof buf)
+                                               p--;
                                        continue;
                                }
                                break;
                                        continue;
                                }
                                break;
@@ -131,25 +197,36 @@ Sync()
                        a = (int)p;
                        b = c = d = 0;
                } else
                        a = (int)p;
                        b = c = d = 0;
                } else
-                       (void) fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d);
-               sync_update(type, SHIP(shipnum), a, b, c, d);
+                       if (fscanf(sync_fp, "%d%d%d%d", &a, &b, &c, &d) != 4)
+                               goto bad;
+               if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0)
+                       goto bad;
        }
        }
-       if (sync_bp != sync_buf) {
+bad:
+       erred++;
+out:
+       if (!erred && sync_bp != sync_buf) {
                (void) fseek(sync_fp, 0L, 2);
                (void) fseek(sync_fp, 0L, 2);
-               (void) fputs(sync_buf, sync_fp);
+               (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf,
+                       sync_fp);
                (void) fflush(sync_fp);
                sync_bp = sync_buf;
        }
        sync_seek = ftell(sync_fp);
                (void) fflush(sync_fp);
                sync_bp = sync_buf;
        }
        sync_seek = ftell(sync_fp);
+#ifdef LOCK_EX
+       (void) flock(fileno(sync_fp), LOCK_UN);
+#else
        (void) unlink(sync_lock);
        (void) unlink(sync_lock);
-       (void) signal(SIGHUP, sig1);
-       (void) signal(SIGINT, sig2);
+#endif
+       (void) signal(SIGHUP, sighup);
+       (void) signal(SIGINT, sigint);
+       return erred ? -1 : 0;
 }
 
 sync_update(type, ship, a, b, c, d)
 }
 
 sync_update(type, ship, a, b, c, d)
-int type;
-register struct ship *ship;
-int a, b, c, d;
+       int type;
+       register struct ship *ship;
+       int a, b, c, d;
 {
        switch (type) {
        case W_DBP: {
 {
        switch (type) {
        case W_DBP: {
@@ -209,8 +286,11 @@ int a, b, c, d;
                break;
                }
        case W_SIGNAL:
                break;
                }
        case W_SIGNAL:
-               if (isplayer)
-                       Signal("\7%s (%c%c): %s", ship, a);
+               if (mode == MODE_PLAYER)
+                       if (nobells)
+                               Signal("%s (%c%c): %s", ship, a);
+                       else
+                               Signal("\7%s (%c%c): %s", ship, a);
                break;
        case W_CREW: {
                register struct shipspecs *s = ship->specs;
                break;
        case W_CREW: {
                register struct shipspecs *s = ship->specs;
@@ -326,11 +406,16 @@ int a, b, c, d;
                people++;
                break;
        case W_END:
                people++;
                break;
        case W_END:
-               (void) strcpy(ship->file->captain, "");
+               *ship->file->captain = 0;
+               ship->file->points = 0;
                people--;
                break;
                people--;
                break;
-       default:
-               fprintf(stderr, "sync_update: unknown type %d\n", type);
+       case W_DDEAD:
+               hasdriver = 0;
                break;
                break;
+       default:
+               fprintf(stderr, "sync_update: unknown type %d\r\n", type);
+               return -1;
        }
        }
+       return 0;
 }
 }