home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / mille / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  4.2 KB  |  173 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)save.c    5.6 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. #include    "mille.h"
  39. #include    <sys/types.h>
  40. #include    <sys/stat.h>
  41. #include    <string.h>
  42. #ifndef    unctrl
  43. #include    "unctrl.h"
  44. #endif
  45.  
  46. # ifdef    attron
  47. #    include    <term.h>
  48. #    define    _tty    cur_term->Nttyb
  49. # endif    attron
  50.  
  51. /*
  52.  * @(#)save.c    1.2 (Berkeley) 3/28/83
  53.  */
  54.  
  55. typedef    struct stat    STAT;
  56.  
  57. char    *ctime();
  58.  
  59. int    read(), write();
  60.  
  61. /*
  62.  *    This routine saves the current game for use at a later date
  63.  */
  64.  
  65. save() {
  66.  
  67.     extern int    errno;
  68.     reg char    *sp;
  69.     reg int        outf;
  70.     reg time_t    *tp;
  71.     char        buf[80];
  72.     time_t        tme;
  73.     STAT        junk;
  74.  
  75.     tp = &tme;
  76.     if (Fromfile && getyn(SAMEFILEPROMPT))
  77.         strcpy(buf, Fromfile);
  78.     else {
  79. over:
  80.         prompt(FILEPROMPT);
  81.         leaveok(Board, FALSE);
  82.         refresh();
  83.         sp = buf;
  84.         while ((*sp = readch()) != '\n') {
  85.             if (*sp == killchar())
  86.                 goto over;
  87.             else if (*sp == erasechar()) {
  88.                 if (--sp < buf)
  89.                     sp = buf;
  90.                 else {
  91.                     addch('\b');
  92.                     /*
  93.                      * if the previous char was a control
  94.                      * char, cover up two characters.
  95.                      */
  96.                     if (*sp < ' ')
  97.                         addch('\b');
  98.                     clrtoeol();
  99.                 }
  100.             }
  101.             else {
  102.                 addstr(unctrl(*sp));
  103.                 ++sp;
  104.             }
  105.             refresh();
  106.         }
  107.         *sp = '\0';
  108.         leaveok(Board, TRUE);
  109.     }
  110.  
  111.     /*
  112.      * check for existing files, and confirm overwrite if needed
  113.      */
  114.  
  115.     if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
  116.         && getyn(OVERWRITEFILEPROMPT) == FALSE))
  117.         return FALSE;
  118.  
  119.     if ((outf = creat(buf, 0644)) < 0) {
  120.         error(strerror(errno));
  121.         return FALSE;
  122.     }
  123.     mvwaddstr(Score, ERR_Y, ERR_X, buf);
  124.     wrefresh(Score);
  125.     time(tp);            /* get current time        */
  126.     strcpy(buf, ctime(tp));
  127.     for (sp = buf; *sp != '\n'; sp++)
  128.         continue;
  129.     *sp = '\0';
  130.     varpush(outf, write);
  131.     close(outf);
  132.     wprintw(Score, " [%s]", buf);
  133.     wclrtoeol(Score);
  134.     wrefresh(Score);
  135.     return TRUE;
  136. }
  137.  
  138. /*
  139.  *    This does the actual restoring.  It returns TRUE if the
  140.  * backup was made on exiting, in which case certain things must
  141.  * be cleaned up before the game starts.
  142.  */
  143. rest_f(file)
  144. reg char    *file; {
  145.  
  146.     reg char    *sp;
  147.     reg int        inf;
  148.     char        buf[80];
  149.     STAT        sbuf;
  150.  
  151.     if ((inf = open(file, 0)) < 0) {
  152.         perror(file);
  153.         exit(1);
  154.     }
  155.     if (fstat(inf, &sbuf) < 0) {        /* get file stats    */
  156.         perror(file);
  157.         exit(1);
  158.     }
  159.     varpush(inf, read);
  160.     close(inf);
  161.     strcpy(buf, ctime(&sbuf.st_mtime));
  162.     for (sp = buf; *sp != '\n'; sp++)
  163.         continue;
  164.     *sp = '\0';
  165.     /*
  166.      * initialize some necessary values
  167.      */
  168.     (void)sprintf(Initstr, "%s [%s]\n", file, buf);
  169.     Fromfile = file;
  170.     return !On_exit;
  171. }
  172.  
  173.