diff xrogue/rip.c @ 143:7faf4568c295

Advanced Rogue family: overhaul privilege handling. Advanced Rogue 5 and 7, and XRogue, now open the scorefile and logfile at startup and then drop any set[ug]id privileges if the savedir is not being used.
author John "Elwin" Edwards
date Sat, 16 May 2015 13:39:26 -0400
parents dd137c35c3b1
children 5a77931393f4
line wrap: on
line diff
--- a/xrogue/rip.c	Tue May 12 21:39:39 2015 -0400
+++ b/xrogue/rip.c	Sat May 16 13:39:26 2015 -0400
@@ -20,8 +20,6 @@
 #define EDITSCORE 2     /* Edit the current score file */
 #define ADDSCORE 3      /* Add a new score */
 
-#define LOGFILE "xrogue.log"
-
 #include <curses.h>
 #include <time.h>
 #include <signal.h>
@@ -58,6 +56,8 @@
 
 char    *killname();
 
+extern FILE *scorefi, *logfile;
+
 /*UNUSED*/
 void
 byebye(sig)
@@ -138,7 +138,6 @@
 void 
 writelog(unsigned long amount, int flags, short monst)
 {
-    FILE *logwriter;
     char had_quest = '0';
     char fate[LINELEN];
     struct linked_list *item;
@@ -146,6 +145,11 @@
 #ifdef LOGFILE
     if (waswizard)
         return;
+    if (logfile == NULL)
+    {
+        /* Error message? */
+        return;
+    }
     /* Adjustments to the score */
     if (level == 0 && max_level == 0) 
         amount = 0;
@@ -169,14 +173,11 @@
     }
     else
         return;
-    /* Open and write */
-    logwriter = fopen(LOGFILE, "a");
-    if (logwriter == NULL)
-        return;
-    fprintf(logwriter, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
+    /* Write the line */
+    fprintf(logfile, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
             whoami, pstats.s_lvl, char_class[char_type].name, level, max_level,
             quest_item, had_quest, fate);
-    fclose(logwriter);
+    fclose(logfile);
 #endif
     return;
 }
@@ -234,14 +235,15 @@
      * Open file and read list
      */
 
-    if ((outf = fopen(score_file, "rb+")) == NULL)
+    if (scorefi == NULL)
     {
-        if ((outf = fopen(score_file, "wb+")) == NULL)
-        {
-            mvprintw(lines - 1, 0, "Unable to open or create score file: %s",score_file);
-            refresh();
-            return;
-        }
+        mvprintw(lines - 1, 0, "Unable to open or create score file: %s",score_file);
+        refresh();
+        return;
+    }
+    else
+    {
+        outf = scorefi;
     }
 
 	thissys = md_gethostname();