os/ossrv/genericopenlibs/cstdlib/LSTDIO/TMPNAM.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * FUNCTION
    16 * <<tmpnam>>, <<tempnam>>---name for a temporary file
    17 * INDEX
    18 * tmpnam
    19 * INDEX
    20 * tempnam
    21 * INDEX
    22 * _tmpnam_r
    23 * INDEX
    24 * _tempnam_r
    25 * ANSI_SYNOPSIS
    26 * #include <stdio.h>
    27 * char *tmpnam(char *<[s]>);
    28 * char *tempnam(char *<[dir]>, char *<[pfx]>);
    29 * char *_tmpnam_r(void *<[reent]>, char *<[s]>);
    30 * char *_tempnam_r(void *<[reent]>, char *<[dir]>, char *<[pfx]>);
    31 * TRAD_SYNOPSIS
    32 * #include <stdio.h>
    33 * char *tmpnam(<[s]>)
    34 * char *<[s]>;
    35 * char *tempnam(<[dir]>, <[pfx]>)
    36 * char *<[dir]>;
    37 * char *<[pfx]>;
    38 * char *_tmpnam_r(<[reent]>, <[s]>)
    39 * char *<[reent]>;
    40 * char *<[s]>;
    41 * char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
    42 * char *<[reent]>;
    43 * char *<[dir]>;
    44 * char *<[pfx]>;
    45 * Use either of these functions to generate a name for a temporary file.
    46 * The generated name is guaranteed to avoid collision with other files
    47 * (for up to <<TMP_MAX>> calls of either function).
    48 * <<tmpnam>> generates file names with the value of <<P_tmpdir>>
    49 * (defined in `<<stdio.h>>') as the leading directory component of the path.
    50 * You can use the <<tmpnam>> argument <[s]> to specify a suitable area
    51 * of memory for the generated filename; otherwise, you can call
    52 * <<tmpnam(NULL)>> to use an internal static buffer.
    53 * <<tempnam>> allows you more control over the generated filename: you
    54 * can use the argument <[dir]> to specify the path to a directory for
    55 * temporary files, and you can use the argument <[pfx]> to specify a
    56 * prefix for the base filename.
    57 * If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
    58 * environment variable <<TMPDIR>> instead; if there is no such value,
    59 * <<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
    60 * If you don't need any particular prefix to the basename of temporary
    61 * files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
    62 * <<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
    63 * and <<tempnam>> respectively.  The extra argument <[reent]> is a
    64 * pointer to a reentrancy structure.
    65 * WARNINGS
    66 * The generated filenames are suitable for temporary files, but do not
    67 * in themselves make files temporary.  Files with these names must still
    68 * be explicitly removed when you no longer want them.
    69 * If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
    70 * that it has room for at least <<L_tmpnam>> elements of type <<char>>.
    71 * RETURNS
    72 * Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
    73 * generated filename.
    74 * PORTABILITY
    75 * ANSI C requires <<tmpnam>>, but does not specify the use of
    76 * <<P_tmpdir>>.  The System V Interface Definition (Issue 2) requires
    77 * both <<tmpnam>> and <<tempnam>>.
    78 * Supporting OS subroutines required: <<close>>,  <<fstat>>, <<getpid>>,
    79 * <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
    80 * The global pointer <<environ>> is also required.
    81 * 
    82 *
    83 */
    84 
    85 
    86 
    87 #include <_ansi.h>
    88 #include <stdio_r.h>
    89 #include <stdlib.h>
    90 #include <string.h>
    91 #include <fcntl.h>
    92 #include <sys/stat.h>
    93 #include <unistd.h>
    94 #include <errno.h>
    95 
    96 
    97 /* Try to open the file specified, if it can be opened then try
    98    another one.  */
    99 
   100 #define MAXFILENAME 255
   101 
   102 static void worker (struct _reent *ptr,char *result,int part3,int *part4)
   103 {
   104   /*  Generate the filename and make sure that there isn't one called
   105       it already.  */
   106 
   107   for (;;)
   108     {
   109       int t;
   110       struct stat st;
   111       _sprintf_r (ptr, result, P_tmpdir "t%x.%x", part3, *part4);
   112       t = stat(result, &st);
   113       if (t == -1)
   114 	break;	/* file doesn't exist, so it's a plausible name */
   115       (*part4)++;
   116     }
   117 }
   118 
   119 /** A reentrant version of tmpnam(). 
   120 */
   121 EXPORT_C char * _tmpnam_r (struct _reent *p, char *s)
   122 {
   123   char *result;
   124   int pid;
   125 
   126   if (s == NULL)
   127       result = p->_tmpnam;
   128   else
   129       result = s;
   130   pid = getpid();
   131   worker (p, result, pid, &p->_inc);
   132   p->_inc++;
   133   return result;
   134 }
   135 
   136 /** A reentrant version of wtmpnam(). 
   137 */
   138 EXPORT_C wchar_t * _wtmpnam_r (struct _reent *p, wchar_t *s)
   139 	{
   140 	char *result;
   141 	int pid;
   142 	char temp1[MAXFILENAME];
   143 	wchar_t *target;
   144 
   145 
   146 	if (s == NULL)
   147 		result = p->_tmpnam;
   148 	else
   149 		result = temp1;
   150 
   151 	pid = getpid();
   152 	worker (p, result, pid, &p->_inc);
   153 	p->_inc++;
   154 
   155 	//store the wide result
   156 	target = s ? s : p->_wtmpnam;
   157 	if (mbstowcs(target, result, 37))
   158 		return target;
   159 
   160 	p->_errno=EILSEQ;
   161 	return 0;
   162 	}
   163 
   164 #ifndef _REENT_ONLY
   165 
   166 /**
   167 Generate a unique temporary filename.
   168 @return A pointer to the string containing the proposed name for a temporary file. 
   169 If NULL was specified as the buffer this points to an internal buffer 
   170 that will be overwritten the next time this function is called, 
   171 otherwise it returns the buffer parameter.
   172 If an error occurs this function returns NULL.
   173 @param s Pointer to an array of bytes, where the proposed tempname will be stored.
   174 */
   175 EXPORT_C char * tmpnam (char *s)
   176 {
   177   return _tmpnam_r (_REENT, s);
   178 }
   179 
   180 EXPORT_C wchar_t * wtmpnam (wchar_t *s)
   181 {
   182   return _wtmpnam_r (_REENT, s);
   183 }
   184 
   185 
   186 #endif