1 /*========================================================================
5 // Implementation of ISO C rename(),remove(),tmpnam(),tmpfile() functions
7 //========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2004 eCosCentric Ltd.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //========================================================================
38 //#####DESCRIPTIONBEGIN####
40 // Author(s): jlarmour
43 // Purpose: Implementation of ISO C rename(),remove(),tmpnam(),tmpfile()
47 //####DESCRIPTIONEND####
48 //======================================================================*/
52 #include <pkgconf/system.h>
53 #include <pkgconf/isoinfra.h>
54 #include <pkgconf/libc_stdio.h> // Configuration header
58 #include <cyg/infra/cyg_type.h> // Common type definitions and support
59 #include <cyg/infra/cyg_ass.h> // Common assertion functions
60 #include <cyg/infra/cyg_trac.h> // Common tracing functions
61 #include <stdio.h> // Header for this file
62 #include <errno.h> // errno
63 #ifdef CYGPKG_LIBC_STDIO_FILEIO // unix-y functions, e.g. stat,rmdir,unlink,...
66 # include <sys/stat.h>
68 #include <cyg/libc/stdio/io.hxx> // I/O definitions
70 # include <pkgconf/posix.h>
71 # include <cyg/posix/export.h>
74 #if defined(CYGINT_ISO_EXIT) && (CYGINT_ISO_EXIT+0)
75 # include <stdlib.h> // used by tmpfile() for atexit()
81 # define CYG_STDIO_FUNCTION_START() CYG_POSIX_FUNCTION_START()
82 # define CYG_STDIO_FUNCTION_FINISH() CYG_POSIX_FUNCTION_FINISH()
84 # define CYG_STDIO_FUNCTION_START() CYG_EMPTY_STATEMENT
85 # define CYG_STDIO_FUNCTION_FINISH() CYG_EMPTY_STATEMENT
89 #define STDIO_ENTRY() \
90 CYG_REPORT_FUNCTYPE( "returning %d" ); \
91 CYG_STDIO_FUNCTION_START(); \
93 #define STDIO_RETURN(err) \
96 CYG_STDIO_FUNCTION_FINISH(); \
97 if( err != 0 ) __retval = -1, errno = err; \
98 CYG_REPORT_RETVAL( __retval ); \
102 #define STDIO_RETURN_VALUE(val) \
104 CYG_STDIO_FUNCTION_FINISH(); \
105 CYG_REPORT_RETVAL( val ); \
111 ///////////////////////////////////////////////////////////////////////////
114 __externC int remove( const char *path ) __THROW
118 CYG_CHECK_DATA_PTR( path, "path pointer invalid" );
120 #ifdef CYGPKG_LIBC_STDIO_FILEIO
122 ret = stat( path, &sbuf );
126 if ( S_ISDIR(sbuf.st_mode) )
130 ret = unlink( path );
133 #else // !defined(CYGPKG_LIBC_STDIO_FILEIO)
139 ///////////////////////////////////////////////////////////////////////////
142 // The File I/O package supplies its own complete version of this, so we
143 // only implement a dummy here.
145 #ifndef CYGPKG_LIBC_STDIO_FILEIO
146 __externC int rename( const char *oldname, const char *newname ) __THROW
149 CYG_CHECK_DATA_PTR(oldname, "oldname pointer invalid");
150 CYG_CHECK_DATA_PTR(newname, "newname pointer invalid");
151 STDIO_RETURN(ENOSYS);
153 #endif // ifndef CYGPKG_LIBC_STDIO_FILEIO
155 ///////////////////////////////////////////////////////////////////////////
158 __externC char *tmpnam( char *s ) __THROW
161 static char staticbuf[ L_tmpnam ];
163 typedef cyg_uint8 counttype;
164 #elif (TMP_MAX < 65536)
165 typedef cyg_uint16 counttype;
167 typedef cyg_ucount32 counttype;
169 static counttype count;
170 counttype totaliters=0;
174 CYG_CHECK_DATA_PTR( s, "supplied string pointer invalid" );
178 // start off by making it "tmp00000" etc. so we can fill backwards
179 // from end without spaces
180 s[0] = 't'; s[1] = 'm'; s[2] = 'p';
182 while (totaliters < TMP_MAX)
184 for (i=3; i < (L_tmpnam-1); i++)
190 counttype counttmp = count;
191 for (i=(L_tmpnam-1); i>2; i--)
193 const char tohex[] = "0123456789abcdef";
194 s[i] = tohex[counttmp & 0xf];
195 counttmp = counttmp >> 4;
198 count %= TMP_MAX; // cycle round
201 // s now points to a name candidate
202 #ifdef CYGPKG_LIBC_STDIO_FILEIO
203 int fd = open( s, O_RDONLY );
206 else if ( ENOENT == errno ) // we have a winner
209 break; // no real filesystem, so just go with what we've come up with
213 if ( totaliters == TMP_MAX ) // oops, looped right the way round
216 STDIO_RETURN_VALUE( s );
219 ///////////////////////////////////////////////////////////////////////////
222 __externC FILE *tmpfile( void ) __THROW
225 char fname[L_tmpnam];
235 // fname is now a valid name to use
236 f = fopen( fname, "wb+" );
237 #ifdef CYGPKG_LIBC_STDIO_FILEIO
238 // We can use remove which should mean the file is removed on program
239 // exit.We ignore the return code though - the standard seems to
240 // indicate that the return status from this function is solely
241 // dictated by whether the file could be created.
247 STDIO_RETURN_VALUE( f );
250 ///////////////////////////////////////////////////////////////////////////