Subject: The use of 'tempnam'

From: "S. Anderson" <[email-masked]>
Date: Fri, 16 Feb 2001 02:37:54 -0700

Hello,
When I just compiled a fresh cvs fvwm I noticed the following messages. Its 
probably nothing, but I thought I would let you know anyway.

...
session.o: In function `getUniqueStateFilename':
/home/sa/src/fvwm/fvwm/session.c:154: the use of `tempnam' is dangerous, better use `mkstemp'
...
FvwmCpp.o: In function `cpp_defs':
/home/sa/src/fvwm/modules/FvwmCpp/FvwmCpp.c:255: the use of `mktemp' is dangerous, better use `mkstemp'
...
FvwmM4.o: In function `m4_defs':
/home/sa/src/fvwm/modules/FvwmM4/FvwmM4.c:270: the use of `mktemp' is dangerous, better use `mkstemp'

thanks, 
Shawn Anderson

~ % gcc --version
2.95.3
~ % fvwm2 -version
FVWM version 2.3.29 compiled on Feb 16 2001 at 01:46:28
with support for: ReadLine, XPM, GNOME WM hints, SM

--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Michael Han <[email-masked]>
Date: Sat, 17 Feb 2001 14:48:23 -0800

> probably nothing, but I thought I would let you know anyway.

I made note of the mktemp() use a while back, but mkstemp() isn't
uniformly available.

But, maybe we could just crib the mkstemp() implementation from
glibc-2 and drop it in libfvwm.a. If people think that's okay to do,
I'd be willing to give a try to adding it, though my knowledge of
automake/conf is pretty weak.
-- 
[email-masked]                            http://www.mikehan.com/
coffee achiever                              San Francisco, California
"If you lend me some money, it'll be my treat."
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Mikhael Goikhman <[email-masked]>
Date: Mon, 19 Feb 2001 04:57:28 -0600

> automake/conf is pretty weak.

Yes, give it a try.

You may add libs/mkstemp.c and then add mkstemp to the AC_REPLACE_FUNCS
line in our configure.in. This automatically calls AC_CHECK_FUNCS(mkstemp),
which defines (or not) HAVE_MKSTEMP macro, and updates LIBOBJS if needed.
You should also add the function declaration using #ifndef in fvwmlib.h.

Regards,
Mikhael.

---

From: Michael Han <[email-masked]>
Date: Mon, 19 Feb 2001 15:37:18 -0800

> You should also add the function declaration using #ifndef in fvwmlib.h.

Well, I've got something I hope is workable. Shall I go ahead and
check it into CVS as libs/mkstemp.c? I got a little lost by
glibc-2.2.2's use of __set_errno(), which looks to be a macro to
errno=(). Also, WRT licensing, should I preserve the FSF copyright and
add some comment about how it's an adaptation? Or should we just let
it have a standard FVWM copyright and a mention of the original
source? Anyway, I can send what I've got so far here to the list, for
discussion, etc.
-- 
[email-masked]                            http://www.mikehan.com/
coffee achiever                              San Francisco, California
"Closed Mondays." The Walker Art Center
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Michael Han <[email-masked]>
Date: Mon, 19 Feb 2001 18:42:36 -0800

> source? Anyway, I can send what I've got so far here to the list, for

Well, here's what I came up with, in unified diff format. I can't
easily test this, as I don't have access to any hosts that don't
provide mkstemp() (Linux w/ libc6/glibc2, Solaris 2.[678], FreeBSD
2.2.7 and 4.2). But I *can* compile mkstemp.c into mkstemp.o via gcc
-Wall -ansi -pedantic -c mkstemp.c. It appears to be safely ignored on
my system with native mkstemp() though.
-- 
[email-masked]                            http://www.mikehan.com/
coffee achiever                              San Francisco, California
Unix *is* user friendly. It just happens to be very selective about
who its friends are.
Index: configure.in
===================================================================
RCS file: /home/cvs/fvwm/fvwm/configure.in,v
retrieving revision 1.130
diff -u -r1.130 configure.in
--- configure.in	2001/02/05 11:37:50	1.130
+++ configure.in	2001/02/20 02:29:53
@@ -406,7 +406,7 @@
 fi
 
 dnl Check for standard functions that we have replacements for.
-AC_REPLACE_FUNCS(gethostname strcasecmp strncasecmp strdup strerror usleep atexit)
+AC_REPLACE_FUNCS(gethostname strcasecmp strncasecmp strdup strerror usleep atexit mkstemp)
 AC_SUBST(LIBOBJS)
 
 dnl If we do not have atexit(), then check for on_exit()
Index: libs/fvwmlib.h
===================================================================
RCS file: /home/cvs/fvwm/fvwm/libs/fvwmlib.h,v
retrieving revision 1.85
diff -u -r1.85 fvwmlib.h
--- libs/fvwmlib.h	2001/02/19 00:07:16	1.85
+++ libs/fvwmlib.h	2001/02/20 02:29:54
@@ -117,6 +117,10 @@
 int usleep( unsigned long usec );
 #endif
 
+#ifndef HAVE_MKSTEMP
+int mkstemp( char* template );
+#endif
+
 
 #include "ClientMsg.h"
 #include "Grab.h"
Index: libs/mkstemp.c
===================================================================
RCS file: mkstemp.c
diff -N mkstemp.c
--- /dev/null	Mon Feb 19 20:03:49 2001
+++ mkstemp.c	Mon Feb 19 20:29:55 2001
@@ -0,0 +1,97 @@
+/* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/* This function is an adaptation of the mkstemp() function included
+ * in the Gnu C Library, version 2.2.2. The Gnu C library, in turn,
+ * adapted the function from OpenBSD.
+ * Michael Han <[email-masked]>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+#define __set_errno(val) errno = (val)
+
+/* These are the characters used in temporary filenames.  */
+static const char letters[] =
+"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+/* Generate a unique temporary file name from TEMPLATE.
+   The last six characters of TEMPLATE must be "XXXXXX";
+   they are replaced with a string that makes the filename unique.
+   Then open the file and return a fd. */
+
+int mkstemp (char *template)
+{
+  int len;
+  char *XXXXXX;
+  static uint64_t value;
+  struct timeval tv;
+  int count, fd = -1;
+  int save_errno = errno;
+
+  len = strlen (template);
+  if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  /* This is where the Xs start.  */
+  XXXXXX = &template[len - 6];
+
+  /* Get some more or less random data.  */
+  gettimeofday (&tv, NULL);
+  value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
+
+  for (count = 0; count < TMP_MAX; value += 7777, ++count)
+    {
+      uint64_t v = value;
+
+      /* Fill in the random bits.  */
+      XXXXXX[0] = letters[v % 62];
+      v /= 62;
+      XXXXXX[1] = letters[v % 62];
+      v /= 62;
+      XXXXXX[2] = letters[v % 62];
+      v /= 62;
+      XXXXXX[3] = letters[v % 62];
+      v /= 62;
+      XXXXXX[4] = letters[v % 62];
+      v /= 62;
+      XXXXXX[5] = letters[v % 62];
+
+      fd = open (template, O_RDWR | O_CREAT | O_EXCL, 0600);
+
+      if (fd >= 0)
+	{
+	  __set_errno (save_errno);
+	  return fd;
+	}
+      else if (errno != EEXIST)
+	return -1;
+    }
+
+  /* We got out of the loop because we ran out of combinations to try.  */
+  __set_errno (EEXIST);
+  return -1;
+}
+

---

From: "Paul D. Smith" <[email-masked]>
Date: 20 Feb 2001 00:04:50 -0500

I have an old SunOS 4.1.4 box I can test on.  It's simplest for me if
you check it in, then I can get it that way.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <[email-masked]>    HASMAT--HA Software Methods & Tools
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Michael Han <[email-masked]>
Date: Mon, 19 Feb 2001 21:19:10 -0800

> you check it in, then I can get it that way.

OK, I'll check it in tomorrow if no one objects.
-- 
[email-masked]                            http://www.mikehan.com/
coffee achiever                              San Francisco, California
"I will not be a snickerpuss." - Lisa
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]

---

From: Michael Han <[email-masked]>
Date: Tue, 20 Feb 2001 19:13:21 -0800

> OK, I'll check it in tomorrow if no one objects.

Well, I guess no one objected, so I committed the stuff. Paul, let me
know if it's broken. If things look good, I'll change out the two uses
of mkstemp() and try to remove tempnam() from fvwm/session.c.
-- 
[email-masked]                            http://www.mikehan.com/
coffee achiever                              San Francisco, California
A closed mouth gathers no foot
--
Visit the official FVWM web page at <URL:http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked]