Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 4 Nov 2012 15:15:47 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r242577 - head/games/fortune/fortune
Message-ID:  <201211041515.qA4FFl9T015910@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler
Date: Sun Nov  4 15:15:47 2012
New Revision: 242577
URL: http://svn.freebsd.org/changeset/base/242577

Log:
  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 (from discussion)
  MFC after:	3 days

Modified:
  head/games/fortune/fortune/fortune.6
  head/games/fortune/fortune/fortune.c

Modified: head/games/fortune/fortune/fortune.6
==============================================================================
--- head/games/fortune/fortune/fortune.6	Sun Nov  4 15:15:43 2012	(r242576)
+++ head/games/fortune/fortune/fortune.6	Sun Nov  4 15:15:47 2012	(r242577)
@@ -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: head/games/fortune/fortune/fortune.c
==============================================================================
--- head/games/fortune/fortune/fortune.c	Sun Nov  4 15:15:43 2012	(r242576)
+++ head/games/fortune/fortune/fortune.c	Sun Nov  4 15:15:47 2012	(r242577)
@@ -107,6 +107,7 @@ static bool	Offend = FALSE;		/* offensiv
 static bool	All_forts = FALSE;	/* any fortune allowed */
 static bool	Equal_probs = FALSE;	/* scatter un-allocted prob equally */
 static bool	Match = FALSE;		/* dump fortunes matching a pattern */
+static bool	WriteToDisk = false;	/* use files on disk to save state */
 #ifdef DEBUG
 static bool	Debug = FALSE;		/* print debug messages */
 #endif
@@ -168,9 +169,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, "");
 
@@ -188,26 +190,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();
@@ -585,9 +583,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);
 }
@@ -689,10 +686,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);
 }
 
 /*
@@ -822,12 +818,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"));
 
@@ -1108,23 +1105,21 @@ open_dat(FILEDESC *fp)
 static 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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201211041515.qA4FFl9T015910>