public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 10/17] wall: use xmkstemp for temporary file
Date: Mon,  5 Mar 2012 20:38:47 +0100	[thread overview]
Message-ID: <1330976334-10751-11-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1330976334-10751-1-git-send-email-kerolasa@iki.fi>

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/Makefile.am |    2 +-
 term-utils/wall.c      |   19 +++++--------------
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/term-utils/Makefile.am b/term-utils/Makefile.am
index 49ad5ff..378676f 100644
--- a/term-utils/Makefile.am
+++ b/term-utils/Makefile.am
@@ -51,7 +51,7 @@ endif
 
 if BUILD_WALL
 usrbin_exec_PROGRAMS += wall
-wall_SOURCES = wall.c ttymsg.c ttymsg.h $(top_srcdir)/lib/strutils.c
+wall_SOURCES = wall.c ttymsg.c ttymsg.h $(top_srcdir)/lib/strutils.c $(top_srcdir)/lib/fileutils.c
 dist_man_MANS += wall.1
 wall_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS)
 wall_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
diff --git a/term-utils/wall.c b/term-utils/wall.c
index 7089826..b9f5321 100644
--- a/term-utils/wall.c
+++ b/term-utils/wall.c
@@ -66,6 +66,7 @@
 #include "pathnames.h"
 #include "carefulputc.h"
 #include "c.h"
+#include "fileutils.h"
 
 #define	IGNOREUSER	"sleeper"
 #define WRITE_TIME_OUT	300		/* in seconds */
@@ -188,24 +189,15 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	struct stat sbuf;
 	time_t now;
 	FILE *fp;
-	int fd;
-	char *p, *whom, *where, *hostname, *lbuf, *tmpname, *tmpenv, *mbuf;
+	char *p, *whom, *where, *hostname, *lbuf, *tmpname, *mbuf;
 	long line_max;
 
 	hostname = xmalloc(sysconf(_SC_HOST_NAME_MAX) + 1);
 	line_max = sysconf(_SC_LINE_MAX);
 	lbuf = xmalloc(line_max);
 
-	tmpname = xmalloc(PATH_MAX);
-	tmpenv = getenv("TMPDIR");
-	if ((tmpenv))
-		snprintf(tmpname, PATH_MAX, "%s/%s.XXXXXX", tmpenv,
-			program_invocation_short_name);
-	else
-		snprintf(tmpname, PATH_MAX, "%s/%s.XXXXXX", _PATH_TMP,
-			program_invocation_short_name);
-	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
-		err(EXIT_FAILURE, _("can't open temporary file %s"), tmpname);
+	if ((fp = xmkstemp(&tmpname)) == NULL)
+		err(EXIT_FAILURE, _("can't open temporary file"));
 	unlink(tmpname);
 	free(tmpname);
 
@@ -280,7 +272,7 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	free(lbuf);
 	rewind(fp);
 
-	if (fstat(fd, &sbuf))
+	if (fstat(fileno(fp), &sbuf))
 		err(EXIT_FAILURE, _("fstat failed"));
 
 	*mbufsize = (size_t) sbuf.st_size;
@@ -289,7 +281,6 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	if (fread(mbuf, 1, *mbufsize, fp) != *mbufsize)
 		err(EXIT_FAILURE, _("fread failed"));
 
-	close(fd);
 	fclose(fp);
 	return mbuf;
 }
-- 
1.7.9.2


  parent reply	other threads:[~2012-03-05 19:39 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
2012-03-05 12:38 ` Karel Zak
2012-03-05 19:38   ` Sami Kerola
2012-03-05 19:38     ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
2012-03-05 19:38     ` [PATCH 02/17] docs: TODO removal, login-utils error printing Sami Kerola
2012-03-05 19:38     ` [PATCH 03/17] sfdisk: use rpmatch to yes/no question Sami Kerola
2012-03-05 19:38     ` [PATCH 04/17] mesg: " Sami Kerola
2012-03-05 19:38     ` [PATCH 05/17] vipw: " Sami Kerola
2012-03-05 19:38     ` [PATCH 06/17] docs: TODO removal, rpmatch task is done Sami Kerola
2012-03-05 19:38     ` [PATCH 07/17] chfn: use pathnames.h for paths Sami Kerola
2012-03-05 19:38     ` [PATCH 08/17] chsh: " Sami Kerola
2012-03-05 19:38     ` [PATCH 09/17] lib: add fileutils function collection Sami Kerola
     [not found]       ` <"1 330984305.2953.25.camel"@offbook>
     [not found]       ` <"1330984305.2953.25.camel"@offbook>
2012-03-05 21:51       ` Davidlohr Bueso
2012-03-09  7:56         ` Sami Kerola
2012-03-09 11:48           ` Davidlohr Bueso
2012-03-09 12:20             ` Karel Zak
2012-03-10 11:49               ` Sami Kerola
2012-03-05 19:38     ` Sami Kerola [this message]
2012-03-05 19:38     ` [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf() Sami Kerola
2012-03-05 19:38     ` [PATCH 12/17] vipw: " Sami Kerola
2012-03-05 19:38     ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
2012-03-05 20:44       ` Sami Kerola
2012-03-05 19:38     ` [PATCH 14/17] docs: TODO removal, ldattach usage is done Sami Kerola
2012-03-05 19:38     ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
2012-03-05 19:52       ` Dave Reisner
2012-03-05 20:47         ` Sami Kerola
2012-03-05 19:38     ` [PATCH 16/17] xalloc: use xasprintf in all files Sami Kerola
2012-03-05 19:38     ` [PATCH 17/17] build-sys: fix chkdupexe regression Sami Kerola
2012-03-20  8:07 ` [pull] fixes to todo items etc Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1330976334-10751-11-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox