121216Sdist /* 221216Sdist * Copyright (c) 1980 Regents of the University of California. 3*33487Sbostic * All rights reserved. 4*33487Sbostic * 5*33487Sbostic * Redistribution and use in source and binary forms are permitted 6*33487Sbostic * provided that this notice is preserved and that due credit is given 7*33487Sbostic * to the University of California at Berkeley. The name of the University 8*33487Sbostic * may not be used to endorse or promote products derived from this 9*33487Sbostic * software without specific prior written permission. This software 10*33487Sbostic * is provided ``as is'' without express or implied warranty. 1121216Sdist */ 126765Srrh 1321216Sdist #ifndef lint 14*33487Sbostic static char sccsid[] = "@(#)save.c 5.2 (Berkeley) 02/16/88"; 15*33487Sbostic #endif /* not lint */ 1621216Sdist 176765Srrh #include "back.h" 186765Srrh 196765Srrh extern int errno; 206765Srrh 216765Srrh static char confirm[] = "Are you sure you want to leave now?"; 226765Srrh static char prompt[] = "Enter a file name: "; 236765Srrh static char exist1[] = "The file '"; 246765Srrh static char exist2[] = 256765Srrh "' already exists.\nAre you sure you want to use this file?"; 266765Srrh static char cantuse[] = "\nCan't use "; 279359Smckusick static char saved[] = "This game has been saved on the file '"; 289359Smckusick static char type[] = "'.\nType \"backgammon "; 299359Smckusick static char rec[] = "\" to recover your game.\n\n"; 306765Srrh static char cantrec[] = "Can't recover file: "; 316765Srrh 326765Srrh save (n) 336765Srrh register int n; 346765Srrh 356765Srrh { 366765Srrh register int fdesc; 376765Srrh register char *fs; 386765Srrh char fname[50]; 396765Srrh 406765Srrh if (n) { 416765Srrh if (tflag) { 426765Srrh curmove (20,0); 436765Srrh clend(); 446765Srrh } else 456765Srrh writec ('\n'); 466765Srrh writel (confirm); 476765Srrh if (! yorn(0)) 486765Srrh return; 496765Srrh } 506765Srrh cflag = 1; 516765Srrh for (;;) { 526765Srrh writel (prompt); 536765Srrh fs = fname; 546765Srrh while ((*fs = readc()) != '\n') { 556765Srrh if (*fs == tty.sg_erase) { 566765Srrh if (fs > fname) { 576765Srrh fs--; 586765Srrh if (tflag) 596765Srrh curmove (curr,curc-1); 606765Srrh else 616765Srrh writec (*fs); 626765Srrh } else 636765Srrh writec ('\007'); 646765Srrh continue; 656765Srrh } 666765Srrh writec (*fs++); 676765Srrh } 686765Srrh *fs = '\0'; 696765Srrh if ((fdesc = open(fname,2)) == -1 && errno == 2) { 706765Srrh if ((fdesc = creat (fname,0700)) != -1) 716765Srrh break; 726765Srrh } 736765Srrh if (fdesc != -1) { 746765Srrh if (tflag) { 756765Srrh curmove (18,0); 766765Srrh clend(); 776765Srrh } else 786765Srrh writec ('\n'); 796765Srrh writel (exist1); 806765Srrh writel (fname); 816765Srrh writel (exist2); 826765Srrh cflag = 0; 836765Srrh close (fdesc); 846765Srrh if (yorn (0)) { 856765Srrh unlink (fname); 866765Srrh fdesc = creat (fname,0700); 876765Srrh break; 886765Srrh } else { 896765Srrh cflag = 1; 906765Srrh continue; 916765Srrh } 926765Srrh } 936765Srrh writel (cantuse); 946765Srrh writel (fname); 956765Srrh writel (".\n"); 966765Srrh close (fdesc); 976765Srrh cflag = 1; 986765Srrh } 999359Smckusick write (fdesc,board,sizeof board); 1009359Smckusick write (fdesc,off,sizeof off); 1019359Smckusick write (fdesc,in,sizeof in); 1029359Smckusick write (fdesc,dice,sizeof dice); 1039359Smckusick write (fdesc,&cturn,sizeof cturn); 1049359Smckusick write (fdesc,&dlast,sizeof dlast); 1059359Smckusick write (fdesc,&pnum,sizeof pnum); 1069359Smckusick write (fdesc,&rscore,sizeof rscore); 1079359Smckusick write (fdesc,&wscore,sizeof wscore); 1089359Smckusick write (fdesc,&gvalue,sizeof gvalue); 1099359Smckusick write (fdesc,&raflag,sizeof raflag); 1106765Srrh close (fdesc); 1116765Srrh if (tflag) 1126765Srrh curmove (18,0); 1136765Srrh writel (saved); 1146765Srrh writel (fname); 1156765Srrh writel (type); 1166765Srrh writel (fname); 1176765Srrh writel (rec); 1186765Srrh if (tflag) 1196765Srrh clend(); 1206765Srrh getout (); 1216765Srrh } 1226765Srrh 1236765Srrh recover (s) 1246765Srrh char *s; 1256765Srrh 1266765Srrh { 1276765Srrh register int i; 1286765Srrh int fdesc; 1296765Srrh 1306765Srrh if ((fdesc = open (s,0)) == -1) 1316765Srrh norec (s); 1329359Smckusick read (fdesc,board,sizeof board); 1339359Smckusick read (fdesc,off,sizeof off); 1349359Smckusick read (fdesc,in,sizeof in); 1359359Smckusick read (fdesc,dice,sizeof dice); 1369359Smckusick read (fdesc,&cturn,sizeof cturn); 1379359Smckusick read (fdesc,&dlast,sizeof dlast); 1389359Smckusick read (fdesc,&pnum,sizeof pnum); 1399359Smckusick read (fdesc,&rscore,sizeof rscore); 1409359Smckusick read (fdesc,&wscore,sizeof wscore); 1419359Smckusick read (fdesc,&gvalue,sizeof gvalue); 1429359Smckusick read (fdesc,&raflag,sizeof raflag); 1436765Srrh close (fdesc); 1446765Srrh rflag = 1; 1456765Srrh } 1466765Srrh 1476765Srrh norec (s) 1486765Srrh register char *s; 1496765Srrh 1506765Srrh { 1516765Srrh register char *c; 1526765Srrh 1536765Srrh tflag = 0; 1546765Srrh writel (cantrec); 1556765Srrh c = s; 1566765Srrh while (*c != '\0') 1576765Srrh writec (*c++); 1586765Srrh getout (); 1596765Srrh } 160