From owner-svn-src-stable@FreeBSD.ORG Wed Nov 7 00:41:08 2012 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id F002E349; Wed, 7 Nov 2012 00:41:07 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id D3E408FC08; Wed, 7 Nov 2012 00:41:07 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id qA70f72G003912; Wed, 7 Nov 2012 00:41:07 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.5/8.14.5/Submit) id qA70f7CW003909; Wed, 7 Nov 2012 00:41:07 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201211070041.qA70f7CW003909@svn.freebsd.org> From: Eitan Adler Date: Wed, 7 Nov 2012 00:41:07 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r242687 - stable/9/games/fortune/fortune X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Nov 2012 00:41:08 -0000 Author: eadler Date: Wed Nov 7 00:41:07 2012 New Revision: 242687 URL: http://svnweb.freebsd.org/changeset/base/242687 Log: MFC r242577: Make OK_TO_WRITE_TO_DISK an envrionment variable instead of a compile time option. While here, don't differ based on the existence of LOCK_EX which doesn't seem to have ever made a difference on FreeBSD. Approved by: cperciva (implicit) Modified: stable/9/games/fortune/fortune/fortune.6 stable/9/games/fortune/fortune/fortune.c Directory Properties: stable/9/games/fortune/ (props changed) Modified: stable/9/games/fortune/fortune/fortune.6 ============================================================================== --- stable/9/games/fortune/fortune/fortune.6 Wed Nov 7 00:40:01 2012 (r242686) +++ stable/9/games/fortune/fortune/fortune.6 Wed Nov 7 00:41:07 2012 (r242687) @@ -170,6 +170,9 @@ looks for data files. If not set it will default to .Pa /usr/games/fortune . If none of the directories specified exist, it will print a warning and exit. +.It Ev FORTUNE_SAVESTATE +If set, fortune will save some state about what fortune +it was up to on disk. .El .Sh FILES .Bl -tag -width ".Pa /usr/share/games/fortune/*" Modified: stable/9/games/fortune/fortune/fortune.c ============================================================================== --- stable/9/games/fortune/fortune/fortune.c Wed Nov 7 00:40:01 2012 (r242686) +++ stable/9/games/fortune/fortune/fortune.c Wed Nov 7 00:41:07 2012 (r242687) @@ -107,6 +107,7 @@ bool Offend = FALSE; /* offensive fortu bool All_forts = FALSE; /* any fortune allowed */ bool Equal_probs = FALSE; /* scatter un-allocted prob equally */ bool Match = FALSE; /* dump fortunes matching a pattern */ +static bool WriteToDisk = false; /* use files on disk to save state */ #ifdef DEBUG bool Debug = FALSE; /* print debug messages */ #endif @@ -170,9 +171,10 @@ static regex_t Re_pat; int main(int argc, char *argv[]) { -#ifdef OK_TO_WRITE_DISK int fd; -#endif /* OK_TO_WRITE_DISK */ + + if (getenv("FORTUNE_SAVESTATE") != NULL) + WriteToDisk = true; (void) setlocale(LC_ALL, ""); @@ -190,26 +192,22 @@ main(int argc, char *argv[]) display(Fortfile); -#ifdef OK_TO_WRITE_DISK - if ((fd = creat(Fortfile->posfile, 0666)) < 0) { - perror(Fortfile->posfile); - exit(1); + if (WriteToDisk) { + if ((fd = creat(Fortfile->posfile, 0666)) < 0) { + perror(Fortfile->posfile); + exit(1); + } + /* + * if we can, we exclusive lock, but since it isn't very + * important, we just punt if we don't have easy locking + * available. + */ + flock(fd, LOCK_EX); + write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos); + if (!Fortfile->was_pos_file) + chmod(Fortfile->path, 0666); + flock(fd, LOCK_UN); } -#ifdef LOCK_EX - /* - * if we can, we exclusive lock, but since it isn't very - * important, we just punt if we don't have easy locking - * available. - */ - (void) flock(fd, LOCK_EX); -#endif /* LOCK_EX */ - write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos); - if (!Fortfile->was_pos_file) - (void) chmod(Fortfile->path, 0666); -#ifdef LOCK_EX - (void) flock(fd, LOCK_UN); -#endif /* LOCK_EX */ -#endif /* OK_TO_WRITE_DISK */ if (Wait) { if (Fort_len == 0) (void) fortlen(); @@ -587,9 +585,8 @@ over: fp->next = *head; *head = fp; } -#ifdef OK_TO_WRITE_DISK - fp->was_pos_file = (access(fp->posfile, W_OK) >= 0); -#endif /* OK_TO_WRITE_DISK */ + if (WriteToDisk) + fp->was_pos_file = (access(fp->posfile, W_OK) >= 0); return (TRUE); } @@ -691,10 +688,9 @@ all_forts(FILEDESC *fp, char *offensive) obscene->name = ++sp; obscene->datfile = datfile; obscene->posfile = posfile; - obscene->read_tbl = FALSE; -#ifdef OK_TO_WRITE_DISK - obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0); -#endif /* OK_TO_WRITE_DISK */ + obscene->read_tbl = false; + if (WriteToDisk) + obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0); } /* @@ -824,12 +820,13 @@ is_fortfile(const char *file, char **dat else free(datfile); if (posp != NULL) { -#ifdef OK_TO_WRITE_DISK - *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */ - (void) strcat(*posp, ".pos"); -#else - *posp = NULL; -#endif /* OK_TO_WRITE_DISK */ + if (WriteToDisk) { + *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */ + strcat(*posp, ".pos"); + } + else { + *posp = NULL; + } } DPRINTF(2, (stderr, "TRUE\n")); @@ -1110,23 +1107,21 @@ open_dat(FILEDESC *fp) void get_pos(FILEDESC *fp) { -#ifdef OK_TO_WRITE_DISK int fd; -#endif /* OK_TO_WRITE_DISK */ assert(fp->read_tbl); if (fp->pos == POS_UNKNOWN) { -#ifdef OK_TO_WRITE_DISK - if ((fd = open(fp->posfile, O_RDONLY)) < 0 || - read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos) + if (WriteToDisk) { + if ((fd = open(fp->posfile, O_RDONLY)) < 0 || + read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos) + fp->pos = arc4random_uniform(fp->tbl.str_numstr); + else if (fp->pos >= fp->tbl.str_numstr) + fp->pos %= fp->tbl.str_numstr; + if (fd >= 0) + close(fd); + } + else fp->pos = arc4random_uniform(fp->tbl.str_numstr); - else if (fp->pos >= fp->tbl.str_numstr) - fp->pos %= fp->tbl.str_numstr; - if (fd >= 0) - (void) close(fd); -#else - fp->pos = arc4random_uniform(fp->tbl.str_numstr); -#endif /* OK_TO_WRITE_DISK */ } if (++(fp->pos) >= fp->tbl.str_numstr) fp->pos -= fp->tbl.str_numstr;