2008-02-29  Juan Manuell Guerrero  <juan.guerrero@gmx.de>

	* Makefile.in (install-man): Replace $(PROG) with mktemp.
	Do not set file attributes.
	(install-binaries): Do not set file attributes.

	* config.h.in [MSDOS]: Define macro TEMPLATE_PATTERN to choose
	at runtime the template to be used to generate the filename
	depending if LFN support is available or not.
	Define new path handling macros IS_DRIVE_SPEC, IS_SLASH,
	IS_DIR_SEPARATOR and DIR_SEPARATOR.
	Define _PATH_TMP to "." as default temp dir prefix.

	* mktemp.c (main): Change hardcoded template string pattern with
	TEMPLATE_PATTERN.
	Change test for hardcoded slash char as dir separator char with
	IS_DIR_SEPARATOR.  For MSDOS this also checks for drive specifier
	prefix.
	(main) [MSDOS]: Check also for TMP and TEMP.  If one of them are set,
	check that the directory exists, if not default to cwd.
	Change test for hardcoded slash char as dir separator char with
	IS_SLASH.
	Change hardcoded slash char as dir separator char with DIR_SEPARATOR.

	* priv_mktemp.c (_gettemp): Change hardcoded slash char as dir
	separator char with DIR_SEPARATOR.

	* random.c: Fix syntax error.

	* mktemp.man: Add MSDOS/DJGPP specific information.

	* mktemp.mdoc: Add MSDOS/DJGPP specific information.



diff -aprNU5 mktemp-1.5.orig/Makefile.in mktemp-1.5/Makefile.in
--- mktemp-1.5.orig/Makefile.in	2003-03-24 01:09:56 +0000
+++ mktemp-1.5/Makefile.in	2008-02-29 22:25:06 +0000
@@ -106,14 +106,14 @@ install: install-dirs install-binaries i
 
 install-dirs:
 	$(SHELL) $(srcdir)/mkinstalldirs $(bindir) $(mandir)/man1
 
 install-binaries: $(PROG)
-	$(INSTALL) -m 0555 -s $(PROG) $(bindir)/$(PROG)
+	$(INSTALL) -s $(PROG) $(bindir)/$(PROG)
 
 install-man:
-	$(INSTALL) -m 0444 $(srcdir)/$(PROG).$(mantype) $(mandir)/man1/$(PROG).1
+	$(INSTALL) $(srcdir)/mktemp.$(mantype) $(mandir)/man1/mktemp.1
 
 check:
 	@echo nothing to check
 
 tags: $(SRCS)
diff -aprNU5 mktemp-1.5.orig/config.h.in mktemp-1.5/config.h.in
--- mktemp-1.5.orig/config.h.in	2003-03-24 01:06:56 +0000
+++ mktemp-1.5/config.h.in	2008-02-29 22:25:46 +0000
@@ -90,6 +90,25 @@
 #undef const
 
 /* Define to `int' if <sys/types.h> does not define. */
 #undef ssize_t
 
+#ifdef MSDOS
+#define TEMPLATE_PATTERN    ((pathconf(NULL, _PC_NAME_MAX) > 12) ? "tmp.XXXXXXXXXX" : "tpXXXXXX")
+#define IS_DRIVE_SPEC(n)    ((n)[0] >= 'A' && (n)[0] <= 'z' && (n)[1] == ':')
+#define IS_SLASH(n)         ((n) == '/' || (n) == '\\')
+#define IS_DIR_SEPARATOR(n) (IS_DRIVE_SPEC((n)) || strchr((n), '/') || strchr((n), '\\'))
+#ifdef DJGPP
+#define DIR_SEPARATOR       '/'
+#else
+#define DIR_SEPARATOR       '\\'
+#endif
+#define _PATH_TMP           "."
+#else
+#define TEMPLATE_PATTERN    "tmp.XXXXXXXXXX"
+#define IS_DRIVE_SPEC(n)    (0)
+#define IS_SLASH(n)         ((n) == '/')
+#define IS_DIR_SEPARATOR(n) (IS_DRIVE_SPEC((n)) || strchr((n), '/'))
+#define DIR_SEPARATOR       '/'
+#endif
+
 #endif /* _MKTEMP_CONFIG_H */
diff -aprNU5 mktemp-1.5.orig/mktemp.c mktemp-1.5/mktemp.c
--- mktemp-1.5.orig/mktemp.c	2003-03-22 21:54:08 +0000
+++ mktemp-1.5/mktemp.c	2008-02-29 22:25:06 +0000
@@ -116,41 +116,49 @@ main(argc, argv)
 	switch (argc - optind) {
 	case 1:
 		template = argv[optind];
 		break;
 	case 0:
-		template = "tmp.XXXXXXXXXX";
+		template = TEMPLATE_PATTERN;
 		tflag = 1;
 		break;
 	default:
 		usage();
 	}
 
 	if (tflag) {
-		if (strchr(template, '/')) {
+		if (IS_DIR_SEPARATOR(template)) {
 			if (!quiet)
 				(void)fprintf(stderr,
 				    "%s: template must not contain directory separators in -t mode\n", __progname);
 			exit(1);
 		}
 
 		cp = getenv("TMPDIR");
+#ifdef MSDOS
+		if (cp == NULL)
+			cp = getenv("TMP");
+		if (cp == NULL)
+			cp = getenv("TEMP");
+		if (access(cp, D_OK))
+			cp = ".";
+#endif
 		if (cp != NULL && *cp != '\0')
 			prefix = cp;
 		plen = strlen(prefix);
-		while (plen != 0 && prefix[plen - 1] == '/')
+		while (plen != 0 && IS_SLASH(prefix[plen - 1]))
 			plen--;
 
 		tempfile = (char *)malloc(plen + 1 + strlen(template) + 1);
 		if (tempfile == NULL) {
 			if (!quiet)
 				(void)fprintf(stderr,
 				    "%s: cannot allocate memory\n", __progname);
 			exit(1);
 		}
 		(void)memcpy(tempfile, prefix, plen);
-		tempfile[plen] = '/';
+		tempfile[plen] = DIR_SEPARATOR;
 		(void)strcpy(tempfile + plen + 1, template);	/* SAFE */
 	} else {
 		if ((tempfile = strdup(template)) == NULL) {
 			if (!quiet)
 				(void)fprintf(stderr,
diff -aprNU5 mktemp-1.5.orig/mktemp.man mktemp-1.5/mktemp.man
--- mktemp-1.5.orig/mktemp.man	2001-11-12 19:47:06 +0000
+++ mktemp-1.5/mktemp.man	2008-02-29 22:38:48 +0000
@@ -43,19 +43,26 @@ may be any filename with some number of
 .I /tmp/tfile.XXXXXXXXXX.
 If no
 .I template
 is specified a default of
 .I tmp.XXXXXXXXXX
+if LFN support is available and
+.I tpXXXXXX
+if LFN support is not available
 is used and the
 .B \-t
 flag is implied (see below).
 .PP
 The trailing `Xs' are replaced with a combination
 of the current process number and random letters.
 The name chosen depends both on the number of `Xs' in the
 .I template
 and the number of collisions with pre\-existing files.
+If LFN support is not available, like on plain DOS,
+the number of `Xs' in the
+.I template
+is restricted to 6.
 The number of unique filenames
 .B mktemp
 can return depends on the number of
 `Xs' provided; ten `Xs' will result in
 .B mktemp
@@ -98,10 +105,21 @@ as a prefix when generating the temporar
 The
 .I directory
 will be overridden by the user's
 .SM TMPDIR
 environment variable if it is set.
+On MSDOS and clones, if
+.SM TMPDIR
+is not set also the environment variables
+.SM TMP
+and
+.SM TEMP
+will be checked in that order.
+If one of the environment variables has been set,
+it will be checked if the directory contained therein can be accessed.
+If this is not possible then the current working directory
+will be used as prefix when generating the temporary filename.
 This option implies the
 .B \-t
 flag (see below).
 .TP
 .B \-q
@@ -115,24 +133,33 @@ This directory is chosen as follows:
 .RS
 .IP \(bu
 If the user's
 .SM TMPDIR
 environment variable is set, the directory contained therein is used.
+On MSDOS and clones also the environment variables
+.SM TMP
+and
+.SM TEMP
+will be checked in that order.
 .IP \(bu
 Otherwise, if the
 .B \-p
 flag was given the specified directory is used.
 .IP \(bu
 If none of the above apply,
 .I /tmp
+(on MSDOS
+.I ./
+)
 is used.
 .RE
 .PP
 In this mode, the
 .I template
 (if specified) should be a directory component (as opposed to a full path)
 and thus should not contain any forward slashes.
+On MSDOS also no back slashes.
 .TP
 .B \-u
 Operate in ``unsafe'' mode.
 The temp file will be unlinked before
 .B mktemp
@@ -186,10 +213,15 @@ other than
 .I /tmp.
 In this example the temporary file will be created in
 .I /extra/tmp
 unless the user's
 .SM TMPDIR
+(or
+.SM TMP
+and
+.SM TEMP
+on MSDOS and clones)
 environment variable specifies otherwise.
 .RS
 .nf
 
 TMPFILE=`mktemp \-p /extra/tmp example.XXXXXXXXXX` || exit 1
@@ -228,10 +260,15 @@ TMPFILE=`mktemp \-t example.XXXXXXXXXX` 
 
 .fi
 .RE
 .SH ENVIRONMENT
 .IP TMPDIR 8
+(or
+.IP TMP 8
+and
+.IP TEMP 8
+on MSDOS and clones)
 directory in which to place the temporary file when in
 .B \-t
 mode
 .SH SEE ALSO
 .BR mkdtemp (3),
diff -aprNU5 mktemp-1.5.orig/mktemp.mdoc mktemp-1.5/mktemp.mdoc
--- mktemp-1.5.orig/mktemp.mdoc	2001-11-12 19:45:18 +0000
+++ mktemp-1.5/mktemp.mdoc	2008-02-29 22:38:48 +0000
@@ -53,10 +53,13 @@ to it, for example
 .Pa /tmp/tfile.XXXXXXXXXX .
 If no
 .Ar template
 is specified a default of
 .Pa tmp.XXXXXXXXXX
+if LFN support is available and
+.Pa tpXXXXXX
+if LFN support is not available
 is used and the
 .Fl t
 flag is implied (see below).
 .Pp
 The trailing
@@ -66,10 +69,16 @@ random letters.
 The name chosen depends both on the number of
 .Ql X Ns s
 in the
 .Ar template
 and the number of collisions with pre-existing files.
+If LFN support is not available, like on plain DOS,
+the number of
+.Ql X Ns s
+in the
+.Ar template
+is restricted to 6.
 The number of unique filenames
 .Nm
 can return depends on the number of
 .Ql X Ns s
 provided; ten
@@ -114,10 +123,21 @@ as a prefix when generating the temporar
 The
 .Ar directory
 will be overridden by the user's
 .Ev TMPDIR
 environment variable if it is set.
+On MSDOS and clones, if
+.Ev TMPDIR
+is not set also the environment variables
+.Ev TMP
+and
+.Ev TEMP
+will be checked in that order.
+If one of the environment variables has been set,
+it will be checked if the directory contained therein can be accessed.
+If this is not possible then the current working directory
+will be used as prefix when generating the temporary filename.
 This option implies the
 .Fl t
 flag (see below).
 .It Fl q
 Fail silently if an error occurs.
@@ -129,24 +149,33 @@ This directory is chosen as follows:
 .Bl -bullet
 .It
 If the user's
 .Ev TMPDIR
 environment variable is set, the directory contained therein is used.
+On MSDOS and clones also the environment variables
+.Ev TMP
+and
+.Ev TEMP
+will be checked in that order.
 .It
 Otherwise, if the
 .Fl p
 flag was given the specified directory is used.
 .It
 If none of the above apply,
 .Pa /tmp
+(on MSDOS
+.Pa ./
+)
 is used.
 .El
 .Pp
 In this mode, the
 .Ar template
 (if specified) should be a directory component (as opposed to a full path)
 and thus should not contain any forward slashes.
+On MSDOS also no back slashes.
 .It Fl u
 Operate in
 .Dq unsafe
 mode.
 The temp file will be unlinked before
@@ -196,10 +225,15 @@ other than
 .Pa /tmp .
 In this example the temporary file will be created in
 .Pa /extra/tmp
 unless the user's
 .Ev TMPDIR
+(or
+.Ev TMP
+and
+.Ev TEMP
+on MSDOS and clones)
 environment variable specifies otherwise.
 .Bd -literal -offset indent
 TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1
 echo "program output" >> $TMPFILE
 .Ed
@@ -229,10 +263,15 @@ TMPFILE=`mktemp -q -t example.XXXXXXXXXX
 }
 .Ed
 .Sh ENVIRONMENT
 .Bl -tag -width TMPDIR
 .It Ev TMPDIR
+(or
+.Ev TMP
+and
+.Ev TEMP 
+on MSDOS and clones)
 directory in which to place the temporary file when in
 .Fl t
 mode
 .El
 .Sh SEE ALSO
diff -aprNU5 mktemp-1.5.orig/priv_mktemp.c mktemp-1.5/priv_mktemp.c
--- mktemp-1.5.orig/priv_mktemp.c	2003-03-24 01:08:22 +0000
+++ mktemp-1.5/priv_mktemp.c	2008-02-29 22:25:06 +0000
@@ -111,14 +111,14 @@ _gettemp(path, doopen, domkdir)
 	 */
 	if (doopen || domkdir) {
 		for (;; --trv) {
 			if (trv <= path)
 				break;
-			if (*trv == '/') {
+			if (*trv == DIR_SEPARATOR) {
 				*trv = '\0';
 				rval = stat(path, &sbuf);
-				*trv = '/';
+				*trv = DIR_SEPARATOR;
 				if (rval != 0)
 					return(0);
 				if (!S_ISDIR(sbuf.st_mode)) {
 					errno = ENOTDIR;
 					return(0);
diff -aprNU5 mktemp-1.5.orig/random.c mktemp-1.5/random.c
--- mktemp-1.5.orig/random.c	2001-10-01 00:21:34 +0000
+++ mktemp-1.5/random.c	2008-02-29 22:25:06 +0000
@@ -68,11 +68,11 @@ static const char rcsid[] = "$Id: random
 void
 seed_random()
 {
 	srandomdev();
 }
-#else !HAVE_SRANDOMDEV
+#else /* !HAVE_SRANDOMDEV */
 #ifdef _PATH_RANDOM
 static ssize_t
 read_loop(fd, buf, count)
 	int fd;
 	void *buf;
