file locking with flock(), plus error return from Sync().
[unix-history] / usr / src / games / sail / sync.c
index 57e4cf0..27c971a 100644 (file)
@@ -1,11 +1,13 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)sync.c      1.2 83/10/14";
+static char *sccsid = "@(#)sync.c      2.5 84/02/23";
 #endif
 
 #include "externs.h"
 #endif
 
 #include "externs.h"
+#include <sys/file.h>
+#include <sys/errno.h>
 
 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;
@@ -30,12 +32,25 @@ register struct ship *ship;
        Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
 }
 
        Write(W_SIGNAL, from, 1, (int)message, 0, 0, 0);
 }
 
+#include <sys/types.h>
+#include <sys/stat.h>
 sync_exists(game)
 {
        char buf[sizeof sync_file];
 sync_exists(game)
 {
        char buf[sizeof sync_file];
+       struct stat s;
+       time_t t;
 
        (void) sprintf(buf, SF, game);
 
        (void) sprintf(buf, SF, game);
-       return access(buf, 0) >= 0;
+       (void) time(&t);
+       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
+               return 1;
 }
 
 sync_open()
 }
 
 sync_open()
@@ -56,12 +71,14 @@ sync_open()
        if (sync_fp == 0)
                return -1;
        sync_seek == 0;
        if (sync_fp == 0)
                return -1;
        sync_seek == 0;
+       return 0;
 }
 
 sync_close(remove)
 char remove;
 {
 }
 
 sync_close(remove)
 char remove;
 {
-       (void) fclose(sync_fp);
+       if (sync_fp != 0)
+               (void) fclose(sync_fp);
        if (remove)
                (void) unlink(sync_file);
 }
        if (remove)
                (void) unlink(sync_file);
 }
@@ -73,34 +90,62 @@ char isstr;
 int a, b, c, d;
 {
        if (isstr)
 int a, b, c, d;
 {
        if (isstr)
-               (void) sprintf(sync_bufp, "%d %d %d %s\n",
-                       type, ship-SHIP(0), isstr, a);
+               (void) sprintf(sync_bp, "%d %d %d %s\n",
+                       type, ship->file->index, isstr, a);
        else
        else
-               (void) sprintf(sync_bufp, "%d %d %d %d %d %d %d\n",
-                       type, ship-SHIP(0), isstr, a, b, c, d);
-       while (*sync_bufp++)
+               (void) sprintf(sync_bp, "%d %d %d %d %d %d %d\n",
+                       type, ship->file->index, isstr, a, b, c, d);
+       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()
 {
        int (*sig1)(), (*sig2)();
 }
 
 Sync()
 {
        int (*sig1)(), (*sig2)();
-       int type, shipnum, isstr, a, b, c, d;
-       char buf[60];
-       register char *p = buf;
        int n;
        int n;
+       int type, shipnum, isstr, a, b, c, d;
+       char buf[80];
+       char erred = 0;
+       extern errno;
 
        sig1 = signal(SIGHUP, SIG_IGN);
        sig2 = signal(SIGINT, SIG_IGN);
 
        sig1 = signal(SIGHUP, SIG_IGN);
        sig2 = signal(SIGINT, SIG_IGN);
-       for (n = 0; link(sync_file, sync_lock) < 0 && n < 30; n++)
+       for (n = 0; n < 30; n++) {
+#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(2);
                sleep(2);
+       }
+       if (n >= 30)
+               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':
@@ -108,6 +153,8 @@ Sync()
                                case EOF:
                                        break;
                                default:
                                case EOF:
                                        break;
                                default:
+                                       if (p >= buf + sizeof buf)
+                                               p--;
                                        continue;
                                }
                                break;
                                        continue;
                                }
                                break;
@@ -118,19 +165,29 @@ 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) fputs(sync_buf, sync_fp);
                (void) fflush(sync_fp);
                (void) fseek(sync_fp, 0L, 2);
                (void) fputs(sync_buf, 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);
+#endif
        (void) signal(SIGHUP, sig1);
        (void) signal(SIGINT, sig2);
        (void) signal(SIGHUP, sig1);
        (void) signal(SIGINT, sig2);
+       return erred ? -1 : 0;
 }
 
 sync_update(type, ship, a, b, c, d)
 }
 
 sync_update(type, ship, a, b, c, d)
@@ -154,27 +211,50 @@ int a, b, c, d;
                break;
                }
        case W_FOUL: {
                break;
                }
        case W_FOUL: {
-               register struct snag *p = &ship->file->fouls[a];
-               p->turnfoul = b;
-               p->toship = SHIP(c);
+               register struct snag *p = &ship->file->foul[a];
+               if (SHIP(a)->file->dir == 0)
+                       break;
+               if (p->sn_count++ == 0)
+                       p->sn_turn = turn;
+               ship->file->nfoul++;
                break;
                }
        case W_GRAP: {
                break;
                }
        case W_GRAP: {
-               register struct snag *p = &ship->file->grapples[a];
-               p->turnfoul = b;
-               p->toship = SHIP(c);
+               register struct snag *p = &ship->file->grap[a];
+               if (SHIP(a)->file->dir == 0)
+                       break;
+               if (p->sn_count++ == 0)
+                       p->sn_turn = turn;
+               ship->file->ngrap++;
+               break;
+               }
+       case W_UNFOUL: {
+               register struct snag *p = &ship->file->foul[a];
+               if (p->sn_count > 0)
+                       if (b) {
+                               ship->file->nfoul -= p->sn_count;
+                               p->sn_count = 0;
+                       } else {
+                               ship->file->nfoul--;
+                               p->sn_count--;
+                       }
+               break;
+               }
+       case W_UNGRAP: {
+               register struct snag *p = &ship->file->grap[a];
+               if (p->sn_count > 0)
+                       if (b) {
+                               ship->file->ngrap -= p->sn_count;
+                               p->sn_count = 0;
+                       } else {
+                               ship->file->ngrap--;
+                               p->sn_count--;
+                       }
                break;
                }
        case W_SIGNAL:
                break;
                }
        case W_SIGNAL:
-               /*
-               (void) putchar('\7');
-               */
-               Signal("%s (%c%c): %s", ship, a);
-               /*
-               (void) strncpy(ship->file->signal, (char *)a,
-                       sizeof ship->file->signal - 1);
-               ship->file->signal[sizeof ship->file->signal - 1] = 0;
-               */
+               if (isplayer)
+                       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;
@@ -189,7 +269,10 @@ int a, b, c, d;
                ship->file->captain[sizeof ship->file->captain - 1] = 0;
                break;
        case W_CAPTURED:
                ship->file->captain[sizeof ship->file->captain - 1] = 0;
                break;
        case W_CAPTURED:
-               ship->file->captured = SHIP(a);
+               if (a < 0)
+                       ship->file->captured = 0;
+               else
+                       ship->file->captured = SHIP(a);
                break;
        case W_CLASS:
                ship->specs->class = a;
                break;
        case W_CLASS:
                ship->specs->class = a;
@@ -219,10 +302,10 @@ int a, b, c, d;
        case W_HULL:
                ship->specs->hull = a;
                break;
        case W_HULL:
                ship->specs->hull = a;
                break;
-       case W_LAST:
-               (void) strncpy(ship->file->last, (char *)a,
-                       sizeof ship->file->last - 1);
-               ship->file->last[sizeof ship->file->last - 1] = 0;
+       case W_MOVE:
+               (void) strncpy(ship->file->movebuf, (char *)a,
+                       sizeof ship->file->movebuf - 1);
+               ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0;
                break;
        case W_PCREW:
                ship->file->pcrew = a;
                break;
        case W_PCREW:
                ship->file->pcrew = a;
@@ -253,13 +336,13 @@ int a, b, c, d;
        case W_RIG4:
                ship->specs->rig4 = a;
                break;
        case W_RIG4:
                ship->specs->rig4 = a;
                break;
-       case W_SHIPCOL:
+       case W_COL:
                ship->file->col = a;
                break;
                ship->file->col = a;
                break;
-       case W_SHIPDIR:
+       case W_DIR:
                ship->file->dir = a;
                break;
                ship->file->dir = a;
                break;
-       case W_SHIPROW:
+       case W_ROW:
                ship->file->row = a;
                break;
        case W_SINK:
                ship->file->row = a;
                break;
        case W_SINK:
@@ -287,11 +370,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;
 }
 }