os/ossrv/genericopenlibs/cstdlib/LSTDIO/TMPNAM.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/TMPNAM.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* FUNCTION
    1.19 +* <<tmpnam>>, <<tempnam>>---name for a temporary file
    1.20 +* INDEX
    1.21 +* tmpnam
    1.22 +* INDEX
    1.23 +* tempnam
    1.24 +* INDEX
    1.25 +* _tmpnam_r
    1.26 +* INDEX
    1.27 +* _tempnam_r
    1.28 +* ANSI_SYNOPSIS
    1.29 +* #include <stdio.h>
    1.30 +* char *tmpnam(char *<[s]>);
    1.31 +* char *tempnam(char *<[dir]>, char *<[pfx]>);
    1.32 +* char *_tmpnam_r(void *<[reent]>, char *<[s]>);
    1.33 +* char *_tempnam_r(void *<[reent]>, char *<[dir]>, char *<[pfx]>);
    1.34 +* TRAD_SYNOPSIS
    1.35 +* #include <stdio.h>
    1.36 +* char *tmpnam(<[s]>)
    1.37 +* char *<[s]>;
    1.38 +* char *tempnam(<[dir]>, <[pfx]>)
    1.39 +* char *<[dir]>;
    1.40 +* char *<[pfx]>;
    1.41 +* char *_tmpnam_r(<[reent]>, <[s]>)
    1.42 +* char *<[reent]>;
    1.43 +* char *<[s]>;
    1.44 +* char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
    1.45 +* char *<[reent]>;
    1.46 +* char *<[dir]>;
    1.47 +* char *<[pfx]>;
    1.48 +* Use either of these functions to generate a name for a temporary file.
    1.49 +* The generated name is guaranteed to avoid collision with other files
    1.50 +* (for up to <<TMP_MAX>> calls of either function).
    1.51 +* <<tmpnam>> generates file names with the value of <<P_tmpdir>>
    1.52 +* (defined in `<<stdio.h>>') as the leading directory component of the path.
    1.53 +* You can use the <<tmpnam>> argument <[s]> to specify a suitable area
    1.54 +* of memory for the generated filename; otherwise, you can call
    1.55 +* <<tmpnam(NULL)>> to use an internal static buffer.
    1.56 +* <<tempnam>> allows you more control over the generated filename: you
    1.57 +* can use the argument <[dir]> to specify the path to a directory for
    1.58 +* temporary files, and you can use the argument <[pfx]> to specify a
    1.59 +* prefix for the base filename.
    1.60 +* If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
    1.61 +* environment variable <<TMPDIR>> instead; if there is no such value,
    1.62 +* <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
    1.63 +* If you don't need any particular prefix to the basename of temporary
    1.64 +* files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
    1.65 +* <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
    1.66 +* and <<tempnam>> respectively.  The extra argument <[reent]> is a
    1.67 +* pointer to a reentrancy structure.
    1.68 +* WARNINGS
    1.69 +* The generated filenames are suitable for temporary files, but do not
    1.70 +* in themselves make files temporary.  Files with these names must still
    1.71 +* be explicitly removed when you no longer want them.
    1.72 +* If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
    1.73 +* that it has room for at least <<L_tmpnam>> elements of type <<char>>.
    1.74 +* RETURNS
    1.75 +* Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
    1.76 +* generated filename.
    1.77 +* PORTABILITY
    1.78 +* ANSI C requires <<tmpnam>>, but does not specify the use of
    1.79 +* <<P_tmpdir>>.  The System V Interface Definition (Issue 2) requires
    1.80 +* both <<tmpnam>> and <<tempnam>>.
    1.81 +* Supporting OS subroutines required: <<close>>,  <<fstat>>, <<getpid>>,
    1.82 +* <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
    1.83 +* The global pointer <<environ>> is also required.
    1.84 +* 
    1.85 +*
    1.86 +*/
    1.87 +
    1.88 +
    1.89 +
    1.90 +#include <_ansi.h>
    1.91 +#include <stdio_r.h>
    1.92 +#include <stdlib.h>
    1.93 +#include <string.h>
    1.94 +#include <fcntl.h>
    1.95 +#include <sys/stat.h>
    1.96 +#include <unistd.h>
    1.97 +#include <errno.h>
    1.98 +
    1.99 +
   1.100 +/* Try to open the file specified, if it can be opened then try
   1.101 +   another one.  */
   1.102 +
   1.103 +#define MAXFILENAME 255
   1.104 +
   1.105 +static void worker (struct _reent *ptr,char *result,int part3,int *part4)
   1.106 +{
   1.107 +  /*  Generate the filename and make sure that there isn't one called
   1.108 +      it already.  */
   1.109 +
   1.110 +  for (;;)
   1.111 +    {
   1.112 +      int t;
   1.113 +      struct stat st;
   1.114 +      _sprintf_r (ptr, result, P_tmpdir "t%x.%x", part3, *part4);
   1.115 +      t = stat(result, &st);
   1.116 +      if (t == -1)
   1.117 +	break;	/* file doesn't exist, so it's a plausible name */
   1.118 +      (*part4)++;
   1.119 +    }
   1.120 +}
   1.121 +
   1.122 +/** A reentrant version of tmpnam(). 
   1.123 +*/
   1.124 +EXPORT_C char * _tmpnam_r (struct _reent *p, char *s)
   1.125 +{
   1.126 +  char *result;
   1.127 +  int pid;
   1.128 +
   1.129 +  if (s == NULL)
   1.130 +      result = p->_tmpnam;
   1.131 +  else
   1.132 +      result = s;
   1.133 +  pid = getpid();
   1.134 +  worker (p, result, pid, &p->_inc);
   1.135 +  p->_inc++;
   1.136 +  return result;
   1.137 +}
   1.138 +
   1.139 +/** A reentrant version of wtmpnam(). 
   1.140 +*/
   1.141 +EXPORT_C wchar_t * _wtmpnam_r (struct _reent *p, wchar_t *s)
   1.142 +	{
   1.143 +	char *result;
   1.144 +	int pid;
   1.145 +	char temp1[MAXFILENAME];
   1.146 +	wchar_t *target;
   1.147 +
   1.148 +
   1.149 +	if (s == NULL)
   1.150 +		result = p->_tmpnam;
   1.151 +	else
   1.152 +		result = temp1;
   1.153 +
   1.154 +	pid = getpid();
   1.155 +	worker (p, result, pid, &p->_inc);
   1.156 +	p->_inc++;
   1.157 +
   1.158 +	//store the wide result
   1.159 +	target = s ? s : p->_wtmpnam;
   1.160 +	if (mbstowcs(target, result, 37))
   1.161 +		return target;
   1.162 +
   1.163 +	p->_errno=EILSEQ;
   1.164 +	return 0;
   1.165 +	}
   1.166 +
   1.167 +#ifndef _REENT_ONLY
   1.168 +
   1.169 +/**
   1.170 +Generate a unique temporary filename.
   1.171 +@return A pointer to the string containing the proposed name for a temporary file. 
   1.172 +If NULL was specified as the buffer this points to an internal buffer 
   1.173 +that will be overwritten the next time this function is called, 
   1.174 +otherwise it returns the buffer parameter.
   1.175 +If an error occurs this function returns NULL.
   1.176 +@param s Pointer to an array of bytes, where the proposed tempname will be stored.
   1.177 +*/
   1.178 +EXPORT_C char * tmpnam (char *s)
   1.179 +{
   1.180 +  return _tmpnam_r (_REENT, s);
   1.181 +}
   1.182 +
   1.183 +EXPORT_C wchar_t * wtmpnam (wchar_t *s)
   1.184 +{
   1.185 +  return _wtmpnam_r (_REENT, s);
   1.186 +}
   1.187 +
   1.188 +
   1.189 +#endif