diff srogue/save.c @ 37:34d7a614855e

srogue: add support for SAVEDIR
author elwin
date Thu, 25 Nov 2010 17:28:29 +0000
parents 2128c7dc8a40
children 8a9525231fb6
line wrap: on
line diff
--- a/srogue/save.c	Thu Nov 25 12:21:41 2010 +0000
+++ b/srogue/save.c	Thu Nov 25 17:28:29 2010 +0000
@@ -136,8 +136,11 @@
 	FILE *savef;
 
 	ignore();
-	setuid(playuid);
-	setgid(playgid);
+	if (!use_savedir)
+	{
+		setuid(playuid);
+		setgid(playgid);
+	}
 	umask(022);
 
 	if (file_name[0] != '\0') {
@@ -209,8 +212,12 @@
 	int slines, scols;
 
 	if ((inf = open(file, O_RDONLY)) < 0) {
-		printf("Cannot read save game %s\n",file);
-		return FALSE;
+		if (use_savedir && errno == ENOENT)
+			return TRUE;
+		else {
+			printf("Cannot read save game %s\n",file);
+			return FALSE;
+		}
 	}
 
 	encread(buf, strlen(version) + 1, inf);
@@ -297,23 +304,36 @@
 	{
 #ifndef __DJGPP__
 			endwin();
-			while((pid = fork()) < 0)
-				sleep(1);
-
-			/* set id to unlink file */
-			if(pid == 0)
+			if (!use_savedir)
 			{
-				setuid(playuid);
-				setgid(playgid);
-				unlink(file);
-				exit(0);
+				while((pid = fork()) < 0)
+					sleep(1);
+
+				/* set id to unlink file */
+				if(pid == 0)
+				{
+					setuid(playuid);
+					setgid(playgid);
+					unlink(file);
+					exit(0);
+				}
+				/* wait for unlink to finish */
+				else
+				{
+					while(wait(&ret_status) != pid)
+						continue;
+					if (ret_status < 0)
+					{
+						printf("Cannot unlink file\n");
+						return FALSE;
+					}
+				}
 			}
-			/* wait for unlink to finish */
 			else
 			{
-				while(wait(&ret_status) != pid)
-					continue;
-				if (ret_status < 0)
+				/* Don't drop privileges, they're needed
+				 * for the unlink. */
+				if (unlink(file) < 0)
 				{
 					printf("Cannot unlink file\n");
 					return FALSE;