use fwrite instead of fputs, and other changes
[unix-history] / usr / src / games / sail / sync.c
index f2573dc..b72ebe7 100644 (file)
@@ -1,11 +1,21 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)sync.c      2.3 83/12/17";
-#endif
+static char sccsid[] = "@(#)sync.c     5.2 (Berkeley) %G%";
+#endif not lint
 
 #include "externs.h"
 
 #include "externs.h"
+#include <sys/file.h>
+#include <sys/errno.h>
+
+#define BUFSIZE 4096
 
 static char sync_buf[BUFSIZE];
 
 static char sync_buf[BUFSIZE];
-static char *sync_bufp = sync_buf;
+static char *sync_bp = sync_buf;
 static char sync_lock[25];
 static char sync_file[25];
 static long sync_seek;
 static char sync_lock[25];
 static char sync_file[25];
 static long sync_seek;
@@ -15,9 +25,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 +53,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 +63,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,40 +89,68 @@ 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)
 {
        if (isstr)
-               (void) sprintf(sync_bufp, "%d %d %d %s\n",
+               (void) sprintf(sync_bp, "%d %d %d %s\n",
                        type, ship->file->index, isstr, a);
        else
                        type, ship->file->index, isstr, a);
        else
-               (void) sprintf(sync_bufp, "%d %d %d %d %d %d %d\n",
+               (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
                        type, ship->file->index, isstr, a, b, c, d);
                        type, ship->file->index, isstr, a, b, c, d);
-       while (*sync_bufp++)
+       while (*sync_bp++)
                ;
                ;
-       sync_bufp--;
-       if (sync_bufp >= &sync_buf[sizeof sync_buf])
+       sync_bp--;
+       if (sync_bp >= &sync_buf[sizeof sync_buf])
                abort();
                abort();
-       sync_update(type, ship, a, b, c, d);
+       (void) sync_update(type, ship, a, b, c, d);
 }
 
 Sync()
 {
 }
 
 Sync()
 {
-       int (*sig1)(), (*sig2)();
+       int (*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 +158,8 @@ Sync()
                                case EOF:
                                        break;
                                default:
                                case EOF:
                                        break;
                                default:
+                                       if (p >= buf + sizeof buf)
+                                               p--;
                                        continue;
                                }
                                break;
                                        continue;
                                }
                                break;
@@ -131,25 +170,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_bufp != 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);
                (void) fflush(sync_fp);
-               sync_bufp = sync_buf;
+               sync_bp = sync_buf;
        }
        sync_seek = ftell(sync_fp);
        }
        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 +259,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 +379,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;
 }
 }