121216Sdist /* 221216Sdist * Copyright (c) 1980 Regents of the University of California. 333487Sbostic * All rights reserved. 433487Sbostic * 5*42565Sbostic * %sccs.include.redist.c% 621216Sdist */ 76765Srrh 821216Sdist #ifndef lint 9*42565Sbostic static char sccsid[] = "@(#)save.c 5.4 (Berkeley) 06/01/90"; 1033487Sbostic #endif /* not lint */ 1121216Sdist 126765Srrh #include "back.h" 136765Srrh 146765Srrh extern int errno; 156765Srrh 166765Srrh static char confirm[] = "Are you sure you want to leave now?"; 176765Srrh static char prompt[] = "Enter a file name: "; 186765Srrh static char exist1[] = "The file '"; 196765Srrh static char exist2[] = 206765Srrh "' already exists.\nAre you sure you want to use this file?"; 216765Srrh static char cantuse[] = "\nCan't use "; 229359Smckusick static char saved[] = "This game has been saved on the file '"; 239359Smckusick static char type[] = "'.\nType \"backgammon "; 249359Smckusick static char rec[] = "\" to recover your game.\n\n"; 256765Srrh static char cantrec[] = "Can't recover file: "; 266765Srrh 276765Srrh save (n) 286765Srrh register int n; 296765Srrh 306765Srrh { 316765Srrh register int fdesc; 326765Srrh register char *fs; 336765Srrh char fname[50]; 346765Srrh 356765Srrh if (n) { 366765Srrh if (tflag) { 376765Srrh curmove (20,0); 386765Srrh clend(); 396765Srrh } else 406765Srrh writec ('\n'); 416765Srrh writel (confirm); 426765Srrh if (! yorn(0)) 436765Srrh return; 446765Srrh } 456765Srrh cflag = 1; 466765Srrh for (;;) { 476765Srrh writel (prompt); 486765Srrh fs = fname; 496765Srrh while ((*fs = readc()) != '\n') { 506765Srrh if (*fs == tty.sg_erase) { 516765Srrh if (fs > fname) { 526765Srrh fs--; 536765Srrh if (tflag) 546765Srrh curmove (curr,curc-1); 556765Srrh else 566765Srrh writec (*fs); 576765Srrh } else 586765Srrh writec ('\007'); 596765Srrh continue; 606765Srrh } 616765Srrh writec (*fs++); 626765Srrh } 636765Srrh *fs = '\0'; 646765Srrh if ((fdesc = open(fname,2)) == -1 && errno == 2) { 656765Srrh if ((fdesc = creat (fname,0700)) != -1) 666765Srrh break; 676765Srrh } 686765Srrh if (fdesc != -1) { 696765Srrh if (tflag) { 706765Srrh curmove (18,0); 716765Srrh clend(); 726765Srrh } else 736765Srrh writec ('\n'); 746765Srrh writel (exist1); 756765Srrh writel (fname); 766765Srrh writel (exist2); 776765Srrh cflag = 0; 786765Srrh close (fdesc); 796765Srrh if (yorn (0)) { 806765Srrh unlink (fname); 816765Srrh fdesc = creat (fname,0700); 826765Srrh break; 836765Srrh } else { 846765Srrh cflag = 1; 856765Srrh continue; 866765Srrh } 876765Srrh } 886765Srrh writel (cantuse); 896765Srrh writel (fname); 906765Srrh writel (".\n"); 916765Srrh close (fdesc); 926765Srrh cflag = 1; 936765Srrh } 949359Smckusick write (fdesc,board,sizeof board); 959359Smckusick write (fdesc,off,sizeof off); 969359Smckusick write (fdesc,in,sizeof in); 979359Smckusick write (fdesc,dice,sizeof dice); 989359Smckusick write (fdesc,&cturn,sizeof cturn); 999359Smckusick write (fdesc,&dlast,sizeof dlast); 1009359Smckusick write (fdesc,&pnum,sizeof pnum); 1019359Smckusick write (fdesc,&rscore,sizeof rscore); 1029359Smckusick write (fdesc,&wscore,sizeof wscore); 1039359Smckusick write (fdesc,&gvalue,sizeof gvalue); 1049359Smckusick write (fdesc,&raflag,sizeof raflag); 1056765Srrh close (fdesc); 1066765Srrh if (tflag) 1076765Srrh curmove (18,0); 1086765Srrh writel (saved); 1096765Srrh writel (fname); 1106765Srrh writel (type); 1116765Srrh writel (fname); 1126765Srrh writel (rec); 1136765Srrh if (tflag) 1146765Srrh clend(); 1156765Srrh getout (); 1166765Srrh } 1176765Srrh 1186765Srrh recover (s) 1196765Srrh char *s; 1206765Srrh 1216765Srrh { 1226765Srrh register int i; 1236765Srrh int fdesc; 1246765Srrh 1256765Srrh if ((fdesc = open (s,0)) == -1) 1266765Srrh norec (s); 1279359Smckusick read (fdesc,board,sizeof board); 1289359Smckusick read (fdesc,off,sizeof off); 1299359Smckusick read (fdesc,in,sizeof in); 1309359Smckusick read (fdesc,dice,sizeof dice); 1319359Smckusick read (fdesc,&cturn,sizeof cturn); 1329359Smckusick read (fdesc,&dlast,sizeof dlast); 1339359Smckusick read (fdesc,&pnum,sizeof pnum); 1349359Smckusick read (fdesc,&rscore,sizeof rscore); 1359359Smckusick read (fdesc,&wscore,sizeof wscore); 1369359Smckusick read (fdesc,&gvalue,sizeof gvalue); 1379359Smckusick read (fdesc,&raflag,sizeof raflag); 1386765Srrh close (fdesc); 1396765Srrh rflag = 1; 1406765Srrh } 1416765Srrh 1426765Srrh norec (s) 1436765Srrh register char *s; 1446765Srrh 1456765Srrh { 1466765Srrh register char *c; 1476765Srrh 1486765Srrh tflag = 0; 1496765Srrh writel (cantrec); 1506765Srrh c = s; 1516765Srrh while (*c != '\0') 1526765Srrh writec (*c++); 1536765Srrh getout (); 1546765Srrh } 155