save.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* $NetBSD: save.c,v 1.10 2003/08/07 09:36:57 agc Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: save.c,v 1.10 2003/08/07 09:36:57 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <errno.h>
  39. #include "back.h"
  40. static const char confirm[] = "Are you sure you want to leave now?";
  41. static const char prompt[] = "Enter a file name: ";
  42. static const char exist1[] = "The file '";
  43. static const char exist2[] =
  44. "' already exists.\nAre you sure you want to use this file?";
  45. static const char cantuse[] = "\nCan't use ";
  46. static const char saved[] = "This game has been saved on the file '";
  47. static const char type[] = "'.\nType \"backgammon ";
  48. static const char rec[] = "\" to recover your game.\n\n";
  49. static const char cantrec[] = "Can't recover file: ";
  50. void
  51. save(n)
  52. int n;
  53. {
  54. int fdesc;
  55. char *fs;
  56. char fname[50];
  57. if (n) {
  58. if (tflag) {
  59. curmove(20, 0);
  60. clend();
  61. } else
  62. writec('\n');
  63. writel(confirm);
  64. if (!yorn(0))
  65. return;
  66. }
  67. cflag = 1;
  68. for (;;) {
  69. writel(prompt);
  70. fs = fname;
  71. while ((*fs = readc()) != '\n') {
  72. if (*fs == old.c_cc[VERASE]) {
  73. if (fs > fname) {
  74. fs--;
  75. if (tflag)
  76. curmove(curr, curc - 1);
  77. else
  78. writec(*fs);
  79. } else
  80. writec('\007');
  81. continue;
  82. }
  83. writec(*fs++);
  84. }
  85. *fs = '\0';
  86. if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) {
  87. if ((fdesc = creat(fname, 0600)) != -1)
  88. break;
  89. }
  90. if (fdesc != -1) {
  91. if (tflag) {
  92. curmove(18, 0);
  93. clend();
  94. } else
  95. writec('\n');
  96. writel(exist1);
  97. writel(fname);
  98. writel(exist2);
  99. cflag = 0;
  100. close(fdesc);
  101. if (yorn(0)) {
  102. unlink(fname);
  103. fdesc = creat(fname, 0600);
  104. break;
  105. } else {
  106. cflag = 1;
  107. continue;
  108. }
  109. }
  110. writel(cantuse);
  111. writel(fname);
  112. writel(".\n");
  113. close(fdesc);
  114. cflag = 1;
  115. }
  116. write(fdesc, board, sizeof board);
  117. write(fdesc, off, sizeof off);
  118. write(fdesc, in, sizeof in);
  119. write(fdesc, dice, sizeof dice);
  120. write(fdesc, &cturn, sizeof cturn);
  121. write(fdesc, &dlast, sizeof dlast);
  122. write(fdesc, &pnum, sizeof pnum);
  123. write(fdesc, &rscore, sizeof rscore);
  124. write(fdesc, &wscore, sizeof wscore);
  125. write(fdesc, &gvalue, sizeof gvalue);
  126. write(fdesc, &raflag, sizeof raflag);
  127. close(fdesc);
  128. if (tflag)
  129. curmove(18, 0);
  130. writel(saved);
  131. writel(fname);
  132. writel(type);
  133. writel(fname);
  134. writel(rec);
  135. if (tflag)
  136. clend();
  137. getout(0);
  138. }
  139. void
  140. recover(s)
  141. const char *s;
  142. {
  143. int fdesc;
  144. if ((fdesc = open(s, O_RDONLY)) == -1)
  145. norec(s);
  146. read(fdesc, board, sizeof board);
  147. read(fdesc, off, sizeof off);
  148. read(fdesc, in, sizeof in);
  149. read(fdesc, dice, sizeof dice);
  150. read(fdesc, &cturn, sizeof cturn);
  151. read(fdesc, &dlast, sizeof dlast);
  152. read(fdesc, &pnum, sizeof pnum);
  153. read(fdesc, &rscore, sizeof rscore);
  154. read(fdesc, &wscore, sizeof wscore);
  155. read(fdesc, &gvalue, sizeof gvalue);
  156. read(fdesc, &raflag, sizeof raflag);
  157. close(fdesc);
  158. rflag = 1;
  159. }
  160. void
  161. norec(s)
  162. const char *s;
  163. {
  164. const char *c;
  165. tflag = 0;
  166. writel(cantrec);
  167. c = s;
  168. while (*c != '\0')
  169. writec(*c++);
  170. getout(0);
  171. }