delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/09/11/15:25:10

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <003501befc8b$a18bcce0$0100000a@cstreet>
From: "John Poplett" <jpoplett AT mindspring DOT com>
To: <cygwin AT sourceware DOT cygnus DOT com>
Subject: B20.1: bug in mkstemp
Date: Sat, 11 Sep 1999 14:27:03 -0500
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3

------=_NextPart_000_0031_01BEFC61.B76A5920
Content-Type: multipart/alternative;
	boundary="----=_NextPart_001_0032_01BEFC61.B76A5920"


------=_NextPart_001_0032_01BEFC61.B76A5920
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

mkstemp fails with an access errror when an attempt is made to create =
more than one file using the same mkstemp template. This problem was =
reported previously by Rob Mayoff.
=20
In Rob's original report, he gives the following program that =
demonstrates the problem:

In this program, the second call to mkstemp will fail:
=20
#include <stdio.h>
#include <sys/param.h>
=20
main()
{
  char path[MAXPATHLEN];
        int fd, rc;
=20
        sprintf(path, "/tmp/test.XXXXXX");
        fd =3D mkstemp(path);
        if (fd < 0) { perror("mkstemp 1"); exit(1); }
        rc =3D unlink(path);
        if (rc < 0) { perror("unlink"); exit(1); }
        sprintf(path, "/tmp/test.XXXXXX");
        fd =3D mkstemp(path);
        if (fd < 0) { perror("mkstemp 2"); exit(1); }
        rc =3D unlink(path);
        if (rc < 0) { perror("unlink"); exit(1); }
=20
        exit(0);
}
=20
The fix should be simply to treat EACCES just like EEXIST in mkstemp.
=20
I have applied a patch to the runtime source file, mktemp.c, as Rob =
suggests and=20
verified the fix. I include the patch as an attachment.
=20
John
=20
John Poplett
jpoplett AT mindspring DOT com


------=_NextPart_001_0032_01BEFC61.B76A5920
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type><!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 =
HTML//EN">
<META content=3D'"MSHTML 4.72.3110.7"' name=3DGENERATOR>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>mkstemp fails with an access errror when an attempt =
is made to=20
create more than one file using the same mkstemp template. This problem =
was=20
reported previously by Rob Mayoff.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>In Rob's original report, he gives the following =
program that=20
demonstrates the problem:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>In this program, the second call to mkstemp will=20
fail:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>#include &lt;stdio.h&gt;<BR>#include=20
&lt;sys/param.h&gt;</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>main()<BR>{<BR>&nbsp; char=20
path[MAXPATHLEN];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int fd,=20
rc;</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
sprintf(path,=20
&quot;/tmp/test.XXXXXX&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; fd=20
=3D mkstemp(path);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fd =
&lt; 0) {=20
perror(&quot;mkstemp 1&quot;); exit(1);=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rc =3D=20
unlink(path);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (rc &lt; =
0) {=20
perror(&quot;unlink&quot;); exit(1);=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf(path,=20
&quot;/tmp/test.XXXXXX&quot;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; fd=20
=3D mkstemp(path);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (fd =
&lt; 0) {=20
perror(&quot;mkstemp 2&quot;); exit(1);=20
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rc =3D=20
unlink(path);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (rc &lt; =
0) {=20
perror(&quot;unlink&quot;); exit(1); }</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
exit(0);<BR>}</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>The fix should be simply to treat EACCES just like =
EEXIST in=20
mkstemp.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I have applied a patch to the runtime source file, =
mktemp.c,=20
as Rob suggests and </FONT></DIV>
<DIV><FONT size=3D2>verified the fix. I include the patch as an=20
attachment.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>John</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>John Poplett</FONT></DIV>
<DIV><FONT size=3D2><A=20
href=3D"mailto:jpoplett AT mindspring DOT com">jpoplett AT mindspring DOT com</A><BR></=
FONT></DIV></BODY></HTML>

------=_NextPart_001_0032_01BEFC61.B76A5920--

------=_NextPart_000_0031_01BEFC61.B76A5920
Content-Type: application/octet-stream;
	name="mktemp.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mktemp.c"

/* This is file MKTEMP.C */
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
** Rochester NH, 03867-2954, USA.
*/

/*
 * Copyright (c) 1987 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that: (1) source distributions retain this entire copyright
 * notice and comment, and (2) distributions including binaries display
 * the following acknowledgement:  ``This product includes software
 * developed by the University of California, Berkeley and its =
contributors''
 * in the documentation or other materials provided with the =
distribution
 * and in all advertising materials mentioning features or use of this
 * software. Neither the name of the University nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/*
FUNCTION
<<mktemp>>, <<mkstemp>>---generate unused file name

INDEX
	mktemp
INDEX
	mkstemp
INDEX
	_mktemp_r
INDEX
	_mkstemp_r

ANSI_SYNOPSIS
	#include <stdio.h>
	char *mktemp(char *<[path]>);
	int mkstemp(char *<[path]>);

	char *_mktemp_r(void *<[reent]>, char *<[path]>);
	int *_mkstemp_r(void *<[reent]>, char *<[path]>);

TRAD_SYNOPSIS
	#include <stdio.h>
	char *mktemp(<[path]>)
	char *<[path]>;

	int mkstemp(<[path]>)
	char *<[path]>;

	char *_mktemp_r(<[reent]>, <[path]>)
	char *<[reent]>;
	char *<[path]>;

	int _mkstemp_r(<[reent]>, <[path]>)
	char *<[reent]>;
	char *<[path]>;

DESCRIPTION
<<mktemp>> and <<mkstemp>> attempt to generate a file name that is not
yet in use for any existing file.  <<mkstemp>> creates the file and=20
opens it for reading and writing; <<mktemp>> simply generates the file =
name.

You supply a simple pattern for the generated file name, as the string
at <[path]>.  The pattern should be a valid filename (including path
information if you wish) ending with some number of `<<X>>'
characters.  The generated filename will match the leading part of the
name you supply, with the trailing `<<X>>' characters replaced by some
combination of digits and letters.

The alternate functions <<_mktemp_r>> and <<_mkstemp_r>> are reentrant
versions.  The extra argument <[reent]> is a pointer to a reentrancy
structure.

RETURNS
<<mktemp>> returns the pointer <[path]> to the modified string
representing an unused filename, unless it could not generate one, or
the pattern you provided is not suitable for a filename; in that case,
it returns <<NULL>>.

<<mkstemp>> returns a file descriptor to the newly created file,
unless it could not generate an unused filename, or the pattern you
provided is not suitable for a filename; in that case, it returns
<<-1>>.

PORTABILITY
ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System
V Interface Definition requires <<mktemp>> as of Issue 2.

Supporting OS subroutines required: <<getpid>>, <<open>>, <<stat>>.
*/

#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <reent.h>

#undef _getpid_r
#undef _open_r
#undef _stat_r

#define _getpid_r(a) getpid()
#define _open_r(a,b,c,d) open(b,c,d)
#define _stat_r(a,b,c) _stat(b,c)

static
_DEFUN (_gettemp, (ptr, path, doopen),
	struct _reent *ptr _AND
	char *path _AND
	register int *doopen)
{
  register char *start, *trv;
  struct stat sbuf;
  unsigned int pid;

  pid =3D _getpid_r (ptr);
  for (trv =3D path; *trv; ++trv)		/* extra X's get set to 0's */
    continue;
  while (*--trv =3D=3D 'X')
    {
      *trv =3D (pid % 10) + '0';
      pid /=3D 10;
    }

  /*
   * Check the target directory; if you have six X's and it
   * doesn't exist this runs for a *very* long time.
   */

  for (start =3D trv + 1;; --trv)
    {
      if (trv <=3D path)
	break;
      if (*trv =3D=3D '/')
	{
	  *trv =3D '\0';
	  if (_stat_r (ptr, path, &sbuf))
	    return (0);
	  if (!(sbuf.st_mode & S_IFDIR))
	    {
	      ptr->_errno =3D ENOTDIR;
	      return (0);
	    }
	  *trv =3D '/';
	  break;
	}
    }

  for (;;)
    {
      if (doopen)
	{
	  if ((*doopen =3D _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR, =
0600))
	      >=3D 0)
	    return 1;
#ifdef __CYGWIN32__
	  if (ptr->_errno !=3D EEXIST && ptr->_errno !=3D EACCES)
#else
	  if (ptr->_errno !=3D EEXIST)
#endif
	    return 0;
	}
      else if (_stat_r (ptr, path, &sbuf))
	return (ptr->_errno =3D=3D ENOENT ? 1 : 0);

      /* tricky little algorithm for backward compatibility */
      for (trv =3D start;;)
	{
	  if (!*trv)
	    return 0;
	  if (*trv =3D=3D 'z')
	    *trv++ =3D 'a';
	  else
	    {
	      if (isdigit (*trv))
		*trv =3D 'a';
	      else
		++ * trv;
	      break;
	    }
	}
    }
  /*NOTREACHED*/
}

_DEFUN (_mkstemp_r, (ptr, path),
	struct _reent *ptr _AND
	char *path)
{
  int fd;

  return (_gettemp (ptr, path, &fd) ? fd : -1);
}

char *
_DEFUN (_mktemp_r, (ptr, path),
	struct _reent *ptr _AND
	char *path)
{
  return (_gettemp (ptr, path, (int *) NULL) ? path : (char *) NULL);
}

#ifndef _REENT_ONLY

_DEFUN (mkstemp, (path),
	char *path)
{
  int fd;

  return (_gettemp (_REENT, path, &fd) ? fd : -1);
}

char *
_DEFUN (mktemp, (path),
	char *path)
{
  return (_gettemp (_REENT, path, (int *) NULL) ? path : (char *) NULL);
}

#endif /* ! defined (_REENT_ONLY) */


------=_NextPart_000_0031_01BEFC61.B76A5920
Content-Type: text/plain; charset=us-ascii

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com
------=_NextPart_000_0031_01BEFC61.B76A5920--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019