1*9359Smckusick static char	sccsid[] = "	save.c	4.2	82/11/27	";
26765Srrh 
36765Srrh #include "back.h"
46765Srrh 
56765Srrh extern int	errno;
66765Srrh 
76765Srrh static char	confirm[] = "Are you sure you want to leave now?";
86765Srrh static char	prompt[] = "Enter a file name:  ";
96765Srrh static char	exist1[] = "The file '";
106765Srrh static char	exist2[] =
116765Srrh 	"' already exists.\nAre you sure you want to use this file?";
126765Srrh static char	cantuse[] = "\nCan't use ";
13*9359Smckusick static char	saved[] = "This game has been saved on the file '";
14*9359Smckusick static char	type[] = "'.\nType \"backgammon ";
15*9359Smckusick static char	rec[] = "\" to recover your game.\n\n";
166765Srrh static char	cantrec[] = "Can't recover file:  ";
176765Srrh 
186765Srrh save (n)
196765Srrh register int	n;
206765Srrh 
216765Srrh {
226765Srrh 	register int	fdesc;
236765Srrh 	register char	*fs;
246765Srrh 	char		fname[50];
256765Srrh 
266765Srrh 	if (n)  {
276765Srrh 		if (tflag)  {
286765Srrh 			curmove (20,0);
296765Srrh 			clend();
306765Srrh 		} else
316765Srrh 			writec ('\n');
326765Srrh 		writel (confirm);
336765Srrh 		if (! yorn(0))
346765Srrh 			return;
356765Srrh 	}
366765Srrh 	cflag = 1;
376765Srrh 	for (;;)  {
386765Srrh 		writel (prompt);
396765Srrh 		fs = fname;
406765Srrh 		while ((*fs = readc()) != '\n')  {
416765Srrh 			if (*fs == tty.sg_erase)  {
426765Srrh 				if (fs > fname)  {
436765Srrh 					fs--;
446765Srrh 					if (tflag)
456765Srrh 						curmove (curr,curc-1);
466765Srrh 					else
476765Srrh 						writec (*fs);
486765Srrh 				} else
496765Srrh 					writec ('\007');
506765Srrh 				continue;
516765Srrh 			}
526765Srrh 			writec (*fs++);
536765Srrh 		}
546765Srrh 		*fs = '\0';
556765Srrh 		if ((fdesc = open(fname,2)) == -1 && errno == 2)  {
566765Srrh 			if ((fdesc = creat (fname,0700)) != -1)
576765Srrh 			break;
586765Srrh 		}
596765Srrh 		if (fdesc != -1)  {
606765Srrh 			if (tflag)  {
616765Srrh 				curmove (18,0);
626765Srrh 				clend();
636765Srrh 			} else
646765Srrh 				writec ('\n');
656765Srrh 			writel (exist1);
666765Srrh 			writel (fname);
676765Srrh 			writel (exist2);
686765Srrh 			cflag = 0;
696765Srrh 			close (fdesc);
706765Srrh 			if (yorn (0))  {
716765Srrh 				unlink (fname);
726765Srrh 				fdesc = creat (fname,0700);
736765Srrh 				break;
746765Srrh 			} else  {
756765Srrh 				cflag = 1;
766765Srrh 				continue;
776765Srrh 			}
786765Srrh 		}
796765Srrh 		writel (cantuse);
806765Srrh 		writel (fname);
816765Srrh 		writel (".\n");
826765Srrh 		close (fdesc);
836765Srrh 		cflag = 1;
846765Srrh 	}
85*9359Smckusick 	write (fdesc,board,sizeof board);
86*9359Smckusick 	write (fdesc,off,sizeof off);
87*9359Smckusick 	write (fdesc,in,sizeof in);
88*9359Smckusick 	write (fdesc,dice,sizeof dice);
89*9359Smckusick 	write (fdesc,&cturn,sizeof cturn);
90*9359Smckusick 	write (fdesc,&dlast,sizeof dlast);
91*9359Smckusick 	write (fdesc,&pnum,sizeof pnum);
92*9359Smckusick 	write (fdesc,&rscore,sizeof rscore);
93*9359Smckusick 	write (fdesc,&wscore,sizeof wscore);
94*9359Smckusick 	write (fdesc,&gvalue,sizeof gvalue);
95*9359Smckusick 	write (fdesc,&raflag,sizeof raflag);
966765Srrh 	close (fdesc);
976765Srrh 	if (tflag)
986765Srrh 		curmove (18,0);
996765Srrh 	writel (saved);
1006765Srrh 	writel (fname);
1016765Srrh 	writel (type);
1026765Srrh 	writel (fname);
1036765Srrh 	writel (rec);
1046765Srrh 	if (tflag)
1056765Srrh 		clend();
1066765Srrh 	getout ();
1076765Srrh }
1086765Srrh 
1096765Srrh recover (s)
1106765Srrh char	*s;
1116765Srrh 
1126765Srrh {
1136765Srrh 	register int	i;
1146765Srrh 	int		fdesc;
1156765Srrh 
1166765Srrh 	if ((fdesc = open (s,0)) == -1)
1176765Srrh 		norec (s);
118*9359Smckusick 	read (fdesc,board,sizeof board);
119*9359Smckusick 	read (fdesc,off,sizeof off);
120*9359Smckusick 	read (fdesc,in,sizeof in);
121*9359Smckusick 	read (fdesc,dice,sizeof dice);
122*9359Smckusick 	read (fdesc,&cturn,sizeof cturn);
123*9359Smckusick 	read (fdesc,&dlast,sizeof dlast);
124*9359Smckusick 	read (fdesc,&pnum,sizeof pnum);
125*9359Smckusick 	read (fdesc,&rscore,sizeof rscore);
126*9359Smckusick 	read (fdesc,&wscore,sizeof wscore);
127*9359Smckusick 	read (fdesc,&gvalue,sizeof gvalue);
128*9359Smckusick 	read (fdesc,&raflag,sizeof raflag);
1296765Srrh 	close (fdesc);
1306765Srrh 	rflag = 1;
1316765Srrh }
1326765Srrh 
1336765Srrh norec (s)
1346765Srrh register char	*s;
1356765Srrh 
1366765Srrh {
1376765Srrh 	register char	*c;
1386765Srrh 
1396765Srrh 	tflag = 0;
1406765Srrh 	writel (cantrec);
1416765Srrh 	c = s;
1426765Srrh 	while (*c != '\0')
1436765Srrh 		writec (*c++);
1446765Srrh 	getout ();
1456765Srrh }
146