Annotation of 42BSD/games/mille/save.c, revision 1.1

1.1     ! root        1: #include       "mille.h"
        !             2: #include       <sys/types.h>
        !             3: #include       <sys/stat.h>
        !             4: #include       <sys/time.h>
        !             5: # ifdef        attron
        !             6: #      include <term.h>
        !             7: #      define  _tty    cur_term->Nttyb
        !             8: # endif        attron
        !             9: 
        !            10: /*
        !            11:  * @(#)save.c  1.4 (Berkeley) 7/3/83
        !            12:  */
        !            13: 
        !            14: typedef        struct stat     STAT;
        !            15: typedef        struct tm       TIME;
        !            16: 
        !            17: char   *ctime();
        !            18: 
        !            19: int    read(), write();
        !            20: 
        !            21: /*
        !            22:  *     This routine saves the current game for use at a later date
        !            23:  */
        !            24: extern int     errno;
        !            25: extern char    *sys_errlist[];
        !            26: 
        !            27: save() {
        !            28: 
        !            29:        reg char        *sp;
        !            30:        reg int         outf;
        !            31:        reg TIME        *tp;
        !            32:        char            buf[80];
        !            33:        TIME            tme;
        !            34:        STAT            junk;
        !            35: 
        !            36:        tp = &tme;
        !            37:        if (Fromfile && getyn("Same file? "))
        !            38:                strcpy(buf, Fromfile);
        !            39:        else {
        !            40: over:
        !            41:                mvaddstr(MOVE_Y, MOVE_X, "file: ");
        !            42:                clrtoeol();
        !            43:                leaveok(Board, FALSE);
        !            44:                refresh();
        !            45:                sp = buf;
        !            46:                while ((*sp = readch()) != '\n') {
        !            47:                        if (*sp == _tty.sg_kill)
        !            48:                                goto over;
        !            49:                        else if (*sp == _tty.sg_erase) {
        !            50:                                if (--sp < buf)
        !            51:                                        sp = buf;
        !            52:                                else {
        !            53:                                        addch('\b');
        !            54:                                        /*
        !            55:                                         * if the previous char was a control
        !            56:                                         * char, cover up two characters.
        !            57:                                         */
        !            58:                                        if (*sp < ' ')
        !            59:                                                addch('\b');
        !            60:                                        clrtoeol();
        !            61:                                }
        !            62:                        }
        !            63:                        else
        !            64:                                addstr(unctrl(*sp++));
        !            65:                        refresh();
        !            66:                }
        !            67:                *sp = '\0';
        !            68:                leaveok(Board, TRUE);
        !            69:        }
        !            70: 
        !            71:        /*
        !            72:         * check for existing files, and confirm overwrite if needed
        !            73:         */
        !            74: 
        !            75:        if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
        !            76:            && getyn("Overwrite File? ") == FALSE))
        !            77:                return FALSE;
        !            78: 
        !            79:        if ((outf = creat(buf, 0644)) < 0) {
        !            80:                error(sys_errlist[errno]);
        !            81:                return FALSE;
        !            82:        }
        !            83:        mvwaddstr(Score, ERR_Y, ERR_X, buf);
        !            84:        wrefresh(Score);
        !            85:        time(tp);                       /* get current time             */
        !            86:        strcpy(buf, ctime(tp));
        !            87:        for (sp = buf; *sp != '\n'; sp++)
        !            88:                continue;
        !            89:        *sp = '\0';
        !            90:        varpush(outf, write);
        !            91:        close(outf);
        !            92:        wprintw(Score, " [%s]", buf);
        !            93:        wclrtoeol(Score);
        !            94:        wrefresh(Score);
        !            95:        return TRUE;
        !            96: }
        !            97: 
        !            98: /*
        !            99:  *     This does the actual restoring.  It returns TRUE if the
        !           100:  * backup was made on exiting, in which case certain things must
        !           101:  * be cleaned up before the game starts.
        !           102:  */
        !           103: rest_f(file)
        !           104: reg char       *file; {
        !           105: 
        !           106:        reg char        *sp;
        !           107:        reg int         inf;
        !           108:        char            buf[80];
        !           109:        STAT            sbuf;
        !           110: 
        !           111:        if ((inf = open(file, 0)) < 0) {
        !           112:                perror(file);
        !           113:                exit(1);
        !           114:        }
        !           115:        if (fstat(inf, &sbuf) < 0) {            /* get file stats       */
        !           116:                perror(file);
        !           117:                exit(1);
        !           118:        }
        !           119:        varpush(inf, read);
        !           120:        close(inf);
        !           121:        strcpy(buf, ctime(&sbuf.st_mtime));
        !           122:        for (sp = buf; *sp != '\n'; sp++)
        !           123:                continue;
        !           124:        *sp = '\0';
        !           125:        /*
        !           126:         * initialize some necessary values
        !           127:         */
        !           128:        sprintf(Initstr, "%s [%s]\n", file, buf);
        !           129:        Fromfile = file;
        !           130:        return !On_exit;
        !           131: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.