$OpenBSD: patch-ctm_ctm_rmail_ctm_rmail_c,v 1.2 2007/10/31 23:14:46 ajacoutot Exp $
--- ctm/ctm_rmail/ctm_rmail.c.orig	Wed May  1 01:01:45 1996
+++ ctm/ctm_rmail/ctm_rmail.c	Wed Oct 31 23:14:02 2007
@@ -12,7 +12,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <strings.h>
+#include <string.h>
 #include <ctype.h>
 #include <errno.h>
 #include <unistd.h>
@@ -292,6 +292,7 @@ read_piece(char *input_file)
 	if (!decoding)
 	    {
 	    char *s;
+	    int fd = -1;
 
 	    if (sscanf(line, "CTM_MAIL BEGIN %s %d %d %c",
 		    delta, &pce, &npieces, junk) != 3)
@@ -302,19 +303,19 @@ read_piece(char *input_file)
 
 	    got_one++;
 	    strcpy(tname, piece_dir);
-	    strcat(tname, "/p.XXXXXX");
-	    if (mktemp(tname) == NULL)
+	    strcat(tname, "/p.XXXXXXXXXX");
+	    if ((fd = mkstemp(tname)) == -1 ||
+		(ofp = fdopen(fd, "w")) == NULL)
 		{
-		err("*mktemp: '%s'", tname);
+		if (fd != -1) {
+		    err("cannot open '%s' for writing", tname);
+		    close(fd);
+		    }
+		else
+		    err("*mkstemp: '%s'", tname);
 		status++;
 		continue;
 		}
-	    if ((ofp = fopen(tname, "w")) == NULL)
-		{
-		err("cannot open '%s' for writing", tname);
-		status++;
-		continue;
-		}
 
 	    cksum = 0xffff;
 	    decoding++;
@@ -494,17 +495,19 @@ combine(char *delta, int npieces, char *dname, char *p
     FILE *dfp, *pfp;
     int i, n, e;
     char buf[BUFSIZ];
+    int fd = -1;
 
     strcpy(tname, delta_dir);
-    strcat(tname, "/d.XXXXXX");
-    if (mktemp(tname) == NULL)
+    strcat(tname, "/d.XXXXXXXXXX");
+    if ((fd = mkstemp(tname)) == -1 ||
+	(dfp = fdopen(fd, "w")) == NULL)
 	{
-	err("*mktemp: '%s'", tname);
-	return 0;
-	}
-    if ((dfp = fopen(tname, "w")) == NULL)
-	{
-	err("cannot open '%s' for writing", tname);
+	if (fd != -1) {
+	    close(fd);
+	    err("cannot open '%s' for writing", tname);
+	    }
+	else
+	    err("*mktemp: '%s'", tname);
 	return 0;
 	}
 
