Re: smtpd replace mkstemp+fdopen with tmpfile

2019-07-02 Thread Gilles Chehade
On Mon, Jul 01, 2019 at 08:25:02PM +0200, Martijn van Duren wrote:
> No functional change intended, just simplify the code.
> 
> OK?
> 

yes, ok gilles@

> Index: enqueue.c
> ===
> RCS file: /cvs/src/usr.sbin/smtpd/enqueue.c,v
> retrieving revision 1.115
> diff -u -p -r1.115 enqueue.c
> --- enqueue.c 31 May 2018 21:06:12 -  1.115
> +++ enqueue.c 1 Jul 2019 18:23:02 -
> @@ -171,8 +171,6 @@ enqueue(int argc, char *argv[], FILE *of
>   FILE*fp = NULL, *fout;
>   size_t   sz = 0, envid_sz = 0;
>   ssize_t  len;
> - int  fd;
> - char sfn[] = "/tmp/smtpd.XX";
>   char*line;
>   int  dotted;
>   int  inheaders = 1;
> @@ -269,16 +267,9 @@ enqueue(int argc, char *argv[], FILE *of
>   argc--;
>   }
>  
> - if ((fd = mkstemp(sfn)) == -1 ||
> - (fp = fdopen(fd, "w+")) == NULL) {
> - int saved_errno = errno;
> - if (fd != -1) {
> - unlink(sfn);
> - close(fd);
> - }
> - errc(EX_UNAVAILABLE, saved_errno, "mkstemp");
> - }
> - unlink(sfn);
> + if ((fp = tmpfile()) == NULL)
> + err(EX_UNAVAILABLE, "tmpfile");
> +
>   msg.noheader = parse_message(stdin, fake_from == NULL, tflag, fp);
>  
>   if (msg.rcpt_cnt == 0)
> Index: smtpc.c
> ===
> RCS file: /cvs/src/usr.sbin/smtpd/smtpc.c,v
> retrieving revision 1.5
> diff -u -p -r1.5 smtpc.c
> --- smtpc.c   15 May 2019 05:02:43 -  1.5
> +++ smtpc.c   1 Jul 2019 18:23:02 -
> @@ -263,19 +263,12 @@ parse_server(char *server)
>  void
>  parse_message(FILE *ifp)
>  {
> - char sfn[] = "/tmp/smtp.XX";
>   char *line = NULL;
>   size_t linesz = 0;
>   ssize_t len;
> - int fd;
>  
> - if ((fd = mkstemp(sfn)) == -1)
> - fatal("mkstemp");
> - unlink(sfn);
> -
> - mail.fp = fdopen(fd, "w+");
> - if ((mail.fp) == NULL)
> - fatal("fdopen");
> + if ((mail.fp = tmpfile()) == NULL)
> + fatal("tmpfile");
>  
>   for (;;) {
>   if ((len = getline(, , ifp)) == -1) {
> Index: smtpctl.c
> ===
> RCS file: /cvs/src/usr.sbin/smtpd/smtpctl.c,v
> retrieving revision 1.163
> diff -u -p -r1.163 smtpctl.c
> --- smtpctl.c 14 Jan 2019 09:37:40 -  1.163
> +++ smtpctl.c 1 Jul 2019 18:23:02 -
> @@ -1297,20 +1297,10 @@ display(const char *s)
>  
>   if (is_encrypted_fp(fp)) {
>   int i;
> - int fd;
>   FILE   *ofp = NULL;
> - charsfn[] = "/tmp/smtpd.XX";
>  
> - if ((fd = mkstemp(sfn)) == -1 ||
> - (ofp = fdopen(fd, "w+")) == NULL) {
> - int saved_errno = errno;
> - if (fd != -1) {
> - unlink(sfn);
> - close(fd);
> - }
> - errc(1, saved_errno, "mkstemp");
> - }
> - unlink(sfn);
> + if ((ofp = tmpfile()) == NULL)
> + err(1, "tmpfile");
>  
>   for (i = 0; i < 3; i++) {
>   key = getpass("key> ");
> 

-- 
Gilles Chehade @poolpOrg

https://www.poolp.orgpatreon: https://www.patreon.com/gilles



smtpd replace mkstemp+fdopen with tmpfile

2019-07-01 Thread Martijn van Duren
No functional change intended, just simplify the code.

OK?

martijn@

Index: enqueue.c
===
RCS file: /cvs/src/usr.sbin/smtpd/enqueue.c,v
retrieving revision 1.115
diff -u -p -r1.115 enqueue.c
--- enqueue.c   31 May 2018 21:06:12 -  1.115
+++ enqueue.c   1 Jul 2019 18:23:02 -
@@ -171,8 +171,6 @@ enqueue(int argc, char *argv[], FILE *of
FILE*fp = NULL, *fout;
size_t   sz = 0, envid_sz = 0;
ssize_t  len;
-   int  fd;
-   char sfn[] = "/tmp/smtpd.XX";
char*line;
int  dotted;
int  inheaders = 1;
@@ -269,16 +267,9 @@ enqueue(int argc, char *argv[], FILE *of
argc--;
}
 
-   if ((fd = mkstemp(sfn)) == -1 ||
-   (fp = fdopen(fd, "w+")) == NULL) {
-   int saved_errno = errno;
-   if (fd != -1) {
-   unlink(sfn);
-   close(fd);
-   }
-   errc(EX_UNAVAILABLE, saved_errno, "mkstemp");
-   }
-   unlink(sfn);
+   if ((fp = tmpfile()) == NULL)
+   err(EX_UNAVAILABLE, "tmpfile");
+
msg.noheader = parse_message(stdin, fake_from == NULL, tflag, fp);
 
if (msg.rcpt_cnt == 0)
Index: smtpc.c
===
RCS file: /cvs/src/usr.sbin/smtpd/smtpc.c,v
retrieving revision 1.5
diff -u -p -r1.5 smtpc.c
--- smtpc.c 15 May 2019 05:02:43 -  1.5
+++ smtpc.c 1 Jul 2019 18:23:02 -
@@ -263,19 +263,12 @@ parse_server(char *server)
 void
 parse_message(FILE *ifp)
 {
-   char sfn[] = "/tmp/smtp.XX";
char *line = NULL;
size_t linesz = 0;
ssize_t len;
-   int fd;
 
-   if ((fd = mkstemp(sfn)) == -1)
-   fatal("mkstemp");
-   unlink(sfn);
-
-   mail.fp = fdopen(fd, "w+");
-   if ((mail.fp) == NULL)
-   fatal("fdopen");
+   if ((mail.fp = tmpfile()) == NULL)
+   fatal("tmpfile");
 
for (;;) {
if ((len = getline(, , ifp)) == -1) {
Index: smtpctl.c
===
RCS file: /cvs/src/usr.sbin/smtpd/smtpctl.c,v
retrieving revision 1.163
diff -u -p -r1.163 smtpctl.c
--- smtpctl.c   14 Jan 2019 09:37:40 -  1.163
+++ smtpctl.c   1 Jul 2019 18:23:02 -
@@ -1297,20 +1297,10 @@ display(const char *s)
 
if (is_encrypted_fp(fp)) {
int i;
-   int fd;
FILE   *ofp = NULL;
-   charsfn[] = "/tmp/smtpd.XX";
 
-   if ((fd = mkstemp(sfn)) == -1 ||
-   (ofp = fdopen(fd, "w+")) == NULL) {
-   int saved_errno = errno;
-   if (fd != -1) {
-   unlink(sfn);
-   close(fd);
-   }
-   errc(1, saved_errno, "mkstemp");
-   }
-   unlink(sfn);
+   if ((ofp = tmpfile()) == NULL)
+   err(1, "tmpfile");
 
for (i = 0; i < 3; i++) {
key = getpass("key> ");