save.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* $NetBSD: save.c,v 1.11 2003/08/07 09:37:26 agc Exp $ */
  2. /*
  3. * Copyright (c) 1983, 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.11 2003/08/07 09:37:26 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <time.h>
  39. #include "mille.h"
  40. #ifndef unctrl
  41. #include "unctrl.h"
  42. #endif
  43. /*
  44. * @(#)save.c 1.2 (Berkeley) 3/28/83
  45. */
  46. typedef struct stat STAT;
  47. /*
  48. * This routine saves the current game for use at a later date
  49. * Returns FALSE if it couldn't be done.
  50. */
  51. bool
  52. save()
  53. {
  54. char *sp;
  55. int outf;
  56. time_t *tp;
  57. char buf[80];
  58. time_t tme;
  59. STAT junk;
  60. bool rv;
  61. sp = NULL;
  62. tp = &tme;
  63. if (Fromfile && getyn(SAMEFILEPROMPT))
  64. strcpy(buf, Fromfile);
  65. else {
  66. over:
  67. prompt(FILEPROMPT);
  68. leaveok(Board, FALSE);
  69. refresh();
  70. sp = buf;
  71. while ((*sp = readch()) != '\n' && *sp != '\r') {
  72. if (*sp == killchar())
  73. goto over;
  74. else if (*sp == erasechar()) {
  75. if (--sp < buf)
  76. sp = buf;
  77. else {
  78. addch('\b');
  79. /*
  80. * if the previous char was a control
  81. * char, cover up two characters.
  82. */
  83. if (*sp < ' ')
  84. addch('\b');
  85. clrtoeol();
  86. }
  87. }
  88. else {
  89. addstr(unctrl(*sp));
  90. ++sp;
  91. }
  92. refresh();
  93. }
  94. *sp = '\0';
  95. leaveok(Board, TRUE);
  96. }
  97. /*
  98. * check for existing files, and confirm overwrite if needed
  99. */
  100. if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
  101. && getyn(OVERWRITEFILEPROMPT) == FALSE))
  102. return FALSE;
  103. if ((outf = creat(buf, 0644)) < 0) {
  104. error(strerror(errno));
  105. return FALSE;
  106. }
  107. mvwaddstr(Score, ERR_Y, ERR_X, buf);
  108. wrefresh(Score);
  109. time(tp); /* get current time */
  110. rv = varpush(outf, writev);
  111. close(outf);
  112. if (rv == FALSE) {
  113. unlink(buf);
  114. } else {
  115. strcpy(buf, ctime(tp));
  116. for (sp = buf; *sp != '\n'; sp++)
  117. continue;
  118. *sp = '\0';
  119. wprintw(Score, " [%s]", buf);
  120. }
  121. wclrtoeol(Score);
  122. wrefresh(Score);
  123. return rv;
  124. }
  125. /*
  126. * This does the actual restoring. It returns TRUE if the
  127. * backup was made on exiting, in which case certain things must
  128. * be cleaned up before the game starts.
  129. */
  130. bool
  131. rest_f(file)
  132. const char *file;
  133. {
  134. char *sp;
  135. int inf;
  136. char buf[80];
  137. STAT sbuf;
  138. if ((inf = open(file, O_RDONLY)) < 0) {
  139. warn("%s", file);
  140. exit(1);
  141. }
  142. if (fstat(inf, &sbuf) < 0) { /* get file stats */
  143. warn("%s", file);
  144. exit(1);
  145. }
  146. varpush(inf, readv);
  147. close(inf);
  148. strcpy(buf, ctime(&sbuf.st_mtime));
  149. for (sp = buf; *sp != '\n'; sp++)
  150. continue;
  151. *sp = '\0';
  152. /*
  153. * initialize some necessary values
  154. */
  155. (void)sprintf(Initstr, "%s [%s]\n", file, buf);
  156. Fromfile = file;
  157. return !On_exit;
  158. }