summaryrefslogtreecommitdiffstats
path: root/term-utils
diff options
context:
space:
mode:
authorSami Kerola2012-02-29 15:59:42 +0100
committerSami Kerola2012-03-18 14:28:04 +0100
commita000bbb613346617d00009e3904ce7649cc64f87 (patch)
treef7c761a4ab2cdc834e3f964f48f03a91ea47b7fb /term-utils
parentlib: add fileutils function collection (diff)
downloadkernel-qcow2-util-linux-a000bbb613346617d00009e3904ce7649cc64f87.tar.gz
kernel-qcow2-util-linux-a000bbb613346617d00009e3904ce7649cc64f87.tar.xz
kernel-qcow2-util-linux-a000bbb613346617d00009e3904ce7649cc64f87.zip
wall: use xmkstemp for temporary file
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'term-utils')
-rw-r--r--term-utils/Makefile.am2
-rw-r--r--term-utils/wall.c19
2 files changed, 6 insertions, 15 deletions
diff --git a/term-utils/Makefile.am b/term-utils/Makefile.am
index 49ad5ff2f..378676f80 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 708982603..b9f53212a 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;
}