$OpenBSD: patch-src_metamail_mailto_c,v 1.3 2017/05/25 17:47:08 espie Exp $
Index: src/metamail/mailto.c
--- src/metamail/mailto.c.orig
+++ src/metamail/mailto.c
@@ -37,6 +37,9 @@ STILL NEED TO DO/SUPPORT:
 */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
 #include <ctype.h>
 #include <config.h>
 #include <pwd.h>
@@ -50,8 +53,6 @@ STILL NEED TO DO/SUPPORT:
 #include <getfiles.h>
 #include <time.h>
 #include <lib_protos.h>
-#else
-extern char *getenv();
 #endif
 
 #ifdef SYSV
@@ -60,9 +61,18 @@ extern char *getenv();
 #include <unistd.h>
 #endif
 
-extern char *malloc(), *realloc(), *index(), *getmyname();
+extern char *getmyname();
 struct mailpart *CreateNewPart();
+void WriteOutMessage(FILE *, char *, char *, char *, struct mailpart *);
+void FputsQuotingLT(char *, FILE *, struct mailpart *, int, int);
+void EditCurrentMessage(int);
+void ProcessOneMailRC(char *, int);
+void HandleAliasCommand(char *);
+void HeaderFputs(char *, FILE *, char *);
+void DeAlias(char *, FILE *, char *);
 
+
+
 /* The main data structure for the multiple parts of the mail */
 
 struct mailpart {
@@ -195,7 +205,7 @@ tmpname() {
         sprintf(s, "%s/mm.XXXXXX", tmproot);
     }
 #endif
-    mktemp(s);
+/*    mktemp(s);*/
     return(s);
 }
 
@@ -205,7 +215,7 @@ TildeHelp() {
     FILE *fp;
 
     strcpy(TmpName, tmpname());
-    fp = fopen(TmpName, "w");
+    fp = fdopen(mkstemp(TmpName), "w");
     if (!fp) fp = stdout;
     fprintf(fp, "The following tilde escapes are BSD-mail-compatible:\n");
     fprintf(fp, "~? Show help on tilde escapes\n");
@@ -570,6 +580,7 @@ char **argv;
         if (isupper(*sdum)) *sdum = tolower(*sdum);
     }
     if (strcmp(CharacterSet, "us-ascii")
+	 && strcmp(CharacterSet, "koi8-r")
          && strncmp(CharacterSet, "iso-8859-", 9)) {
         fprintf(stderr, "mailto:  Unsupported character set: %s\n", CharacterSet);
         exit(-1);
@@ -591,7 +602,7 @@ char **argv;
     }
     FirstPart = NewPart();
     CurrentPart = FirstPart;
-    fpout = fopen(CurrentPart->filename, "w");
+    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
     if (!fpout) {
         fprintf(stderr, "mailto:  Can't open temporary file %s\n", CurrentPart->filename);
         cleanexit(-1);
@@ -678,7 +689,7 @@ char **argv;
                     CurrentPart->next = NewPart();
                     CurrentPart->next->prev = CurrentPart;
                     CurrentPart = CurrentPart->next;
-                    fpout = fopen(CurrentPart->filename, "w");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
                     if (!fpout) {
                         fprintf(stderr, "mailto:  Can't open temporary file %s\n", CurrentPart->filename);
                         cleanexit(-1);
@@ -717,7 +728,7 @@ char **argv;
                     CurrentPart = CurrentPart->next;
                     CurrentPart->istext = 0;
                     CurrentPart->content_type = "message/rfc822";
-                    fpout = fopen(CurrentPart->filename, "w");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
                     if (!fpout) {
                         fprintf(stderr, "mailto: Cannot open temporary file %s\n", CurrentPart->filename);
                         break;
@@ -729,7 +740,7 @@ char **argv;
                     CurrentPart->next = NewPart();
                     CurrentPart->next->prev = CurrentPart;
                     CurrentPart = CurrentPart->next;
-                    fpout = fopen(CurrentPart->filename, "w");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
                     if (!fpout) {
                         fprintf(stderr, "mailto:  Can't open temporary file %s\n", CurrentPart->filename);
                         cleanexit(-1);
@@ -808,11 +819,11 @@ char **argv;
                     char Cmd[TMPFILE_NAME_SIZE + 15];
                     char *s=tmpname();
                     fclose(fpout);
-                    fptmp = fopen(s, "w");
+                    fptmp = fdopen(mkstemp(s), "w");
                     WriteOutMessage(fptmp, ToList, Subject, CCList, FirstPart);
                     TempCloseStyles(fptmp);
                     fclose(fptmp);
-                    fpout = fopen(CurrentPart->filename, "a");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "a");
                     if (!fpout) {
                         fprintf(stderr, "mailto:  Can't open temporary file %s\n", CurrentPart->filename);
                         cleanexit(-1);
@@ -969,7 +980,7 @@ char **argv;
                     CurrentPart = CurrentPart->next;
                     CurrentPart->istext = 0;
                     CurrentPart->content_type = "message/rfc822";
-                    fpout = fopen(CurrentPart->filename, "w");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
                     if (!fpout) {
                         fprintf(stderr, "mailto: Cannot open temporary file %s\n", CurrentPart->filename);
                         break;
@@ -991,7 +1002,7 @@ char **argv;
                     CurrentPart->next = NewPart();
                     CurrentPart->next->prev = CurrentPart;
                     CurrentPart = CurrentPart->next;
-                    fpout = fopen(CurrentPart->filename, "w");
+                    fpout = fdopen(mkstemp(CurrentPart->filename), "w");
                     if (!fpout) {
                         fprintf(stderr, "mailto:  Can't open temporary file %s\n", CurrentPart->filename);
                         cleanexit(-1);
@@ -1042,6 +1053,7 @@ char *newid() {
     return(idbuf);
 }
 
+void
 WriteOutMessage(fp, ToList, Subject, CCList, FirstPart)
 FILE *fp;
 char *ToList, *Subject, *CCList;
@@ -1130,6 +1142,7 @@ struct mailpart *part;
         if (part->isrich) {
             if (strcmp(CharacterSet, "us-ascii")
                  && (strncmp(CharacterSet, "iso-8859-", 9) 
+		     && strcmp(CharacterSet, "koi8-r")
                       || part->encoding_type_needed != ENC_NONE)) {
                 fprintf(fp, "Content-type: text/richtext; charset=\"%s\"\n", CharacterSet);
             } else {
@@ -1140,6 +1153,7 @@ struct mailpart *part;
             WriteCtypeNicely(fp, part->content_type);
             if (strcmp(CharacterSet, "us-ascii")
                  && (strncmp(CharacterSet, "iso-8859-", 9) 
+		     && strcmp(CharacterSet, "koi8-r")
                       || part->encoding_type_needed != ENC_NONE)) {
                 fprintf(fp, "; charset=\"%s\"\n", CharacterSet);
             } else fputs("\n", fp);
@@ -1328,6 +1342,7 @@ struct mailpart *part;
     }
 }
 
+void
 FputsQuotingLT(s, fp, part, EightBitMode, RightToLeftMode)
 char *s;
 FILE *fp;
@@ -1745,6 +1760,7 @@ CreateNewPart() {
     }
     printf("\n\nEnter your choice as a number from 0 to %d: ", i);
     fflush(stdout);
+    *LineBuf = '\0';
     fgets(LineBuf, sizeof(LineBuf), stdin);
     ans = atoi(LineBuf);
     if (ans == 0 || ans == 1) {
@@ -1773,7 +1789,7 @@ CreateNewPart() {
             printf("Cannot read %s, data insertion cancelled\n", sdum);
             return(NULL);
         }
-        fpo = fopen(mp->filename, "w");
+        fpo = fdopen(mkstemp(mp->filename), "w");
         if (!fpo) {
             printf("Cannot open temporary file, data insertion cancelled\n");
             return(NULL);
@@ -1791,12 +1807,13 @@ CreateNewPart() {
                 int ct;
                 printf("\nEnter the MIME Content-type value for the data from file %s\n    (type '?' for a list of locally-valid content-types): ", sdum);
                 fflush(stdout);
-                gets(LineBuf);
+		fgets(LineBuf, sizeof(LineBuf), stdin);
                 if (index(LineBuf, '/')) {
                     char lc[100], *s, AnsBuf[100];
                     strcpy(lc, LineBuf);
                     for (s=lc; *s; ++s) {
                         if (isupper(*s)) *s = tolower(*s);
+			if (*s == '\n') *s = '\0';
                     }
                     if (!strcmp(lc, "text/plain")) break;
                     if (!strcmp(lc, "application/octet-stream")) break;
@@ -1809,7 +1826,7 @@ CreateNewPart() {
                     }
                     if (mc) break;
                     printf("The MIME content-type '%s' is not listed in your local mailcap files,\nand may not be a valid MIME type.  Do you want to use it anyway [no] ? ", LineBuf);
-                    s = gets(AnsBuf);
+		    s = fgets(AnsBuf, sizeof(AnsBuf), stdin);
                     while (s && *s && isspace((unsigned char) *s)) ++s;
                     if (s && (*s == 'y' || *s == 'Y')) break;
                     continue;
@@ -1866,7 +1883,8 @@ CreateNewPart() {
         mp->content_type = mc->contenttype;
         mp->encoding_type_needed = WhichEncodingForFile(mp->filename, mp->content_type);
     } else {
-        char c, LineBuf[1000], *colon, *ctypebuf = NULL, *cencbuf = NULL, *s, *newfilename;
+        char LineBuf[1000], *colon, *ctypebuf = NULL, *cencbuf = NULL, *s, *newfilename;
+        int c;
         FILE *fp, *fpout;
         int inheaders=1, bct=0;
 
@@ -1936,7 +1954,7 @@ CreateNewPart() {
             mp->encoding_type_needed = ENC_NONE;
         }
         newfilename = tmpname();
-        fpout = fopen(newfilename, "w");
+        fpout = fdopen(mkstemp(newfilename), "w");
         if (!fpout) {
             fprintf(stderr, "Cannot open file %s for writing, no data included.\n", newfilename);
             free(CmdBuf);
@@ -2073,6 +2091,7 @@ char *ctype, *pat;
     return(0);
 }
 
+void
 EditCurrentMessage(UseVisual)
 int UseVisual;
 {
@@ -2212,6 +2231,7 @@ int IsAndrew;
     }
 }
 
+void
 ProcessOneMailRC(fname, IsAndrew)
 char *fname;
 int IsAndrew;
@@ -2290,6 +2310,7 @@ struct alias {
     struct alias *next;
 } *FirstAlias = NULL;
 
+void
 HandleAliasCommand(aliasline)
 char *aliasline;
 {
@@ -2357,6 +2378,7 @@ char *s;
     return(NULL);
 }
 
+void
 HeaderFputs(s, fp, hdr)
 char *s;
 FILE *fp;
@@ -2436,6 +2458,7 @@ char *hdr;
     }
 }
 
+void
 DeAlias(name, fp, hdr)
 char *name;
 FILE *fp;
