[dvblast-devel] [PATCH] dvblastctl: Replace tmpnam(3) with mkstemp(3)

Georgi Chorbadzhiyski gf at unixsol.org
Fri Aug 19 14:41:37 CEST 2011


The tmpnam(3) is used correctly but in order to avoid stupid
GNU linker from complaining, rewrite the code to use mkstemp(3)

The patch corrects:
  cc   dvblastctl.o util.o  -lrt -o dvblastctl
  dvblastctl.o: In function `main':
  dvblastctl.c:120: warning: the use of `tmpnam' is dangerous, better use `mkstemp'

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/
-------------- next part --------------
From ecf46c86582334dab9a7b461653f404129881d9d Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Fri, 19 Aug 2011 15:34:07 +0300
Subject: [PATCH] dvblastctl: Replace tmpnam(3) with mkstemp(3)

The tmpnam(3) is used correctly but in order to avoid stupid
GNU linker from complaining, rewrite the code to use mkstemp(3)
---
 trunk/dvblastctl.c |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/trunk/dvblastctl.c b/trunk/dvblastctl.c
index 3ae22f2..30f0a02 100644
--- a/trunk/dvblastctl.c
+++ b/trunk/dvblastctl.c
@@ -54,7 +54,8 @@ void usage()
 
 int main( int i_argc, char **ppsz_argv )
 {
-    char psz_client_socket[L_tmpnam];
+    char psz_client_socket[PATH_MAX] = {0};
+    char *client_socket_tmpl = "dvblastctl.clientsock.XXXXXX";
     char *psz_srv_socket = NULL;
     int i_fd;
     int i = 65535;
@@ -116,10 +117,18 @@ int main( int i_argc, char **ppsz_argv )
           && ppsz_argv[optind + 2] == NULL )
         usage();
 
-#warning expect brain-dead gcc warning about tmpnam here
-    if ( tmpnam(psz_client_socket) == NULL )
-    {
-        msg_Err( NULL, "cannot build UNIX socket (%s)", strerror(errno) );
+    /* Create client socket name */
+    char *tmpdir = getenv("TMPDIR");
+    snprintf( psz_client_socket, PATH_MAX - 1, "%s/%s",
+       tmpdir ? tmpdir : "/tmp", client_socket_tmpl );
+    psz_client_socket[PATH_MAX - 1] = '\0';
+
+    int tmp_fd = mkstemp(psz_client_socket);
+    if ( tmp_fd > -1 ) {
+        close(tmp_fd);
+        unlink(psz_client_socket);
+    } else {
+        msg_Err( NULL, "cannot build UNIX socket %s (%s)", psz_client_socket, strerror(errno) );
         return -1;
     }
 
-- 
1.7.5.1


More information about the dvblast-devel mailing list