[PATCH 10/17] wall: use xmkstemp for temporary file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux