]> git.sesse.net Git - vlc/commitdiff
Added a utf8_mkstemp implementation.
authorLaurent Aimar <fenrir@videolan.org>
Mon, 17 Nov 2008 23:05:49 +0000 (00:05 +0100)
committerLaurent Aimar <fenrir@videolan.org>
Mon, 17 Nov 2008 23:05:49 +0000 (00:05 +0100)
include/vlc_charset.h
src/text/filesystem.c

index 69bdc178824914445eb9934aa13807060dbf4276..5d3a1b5c93c67c407eef22c1eab2ba3dcea9a163 100644 (file)
@@ -60,6 +60,8 @@ VLC_EXPORT( int, utf8_lstat, ( const char *filename, struct stat *buf ) );
 VLC_EXPORT( int, utf8_vfprintf, ( FILE *stream, const char *fmt, va_list ap ) );
 VLC_EXPORT( int, utf8_fprintf, ( FILE *, const char *, ... ) LIBVLC_FORMAT( 2, 3 ) );
 
+VLC_EXPORT( int, utf8_mkstemp, ( char * ) );
+
 VLC_EXPORT( char *, EnsureUTF8, ( char * ) );
 VLC_EXPORT( const char *, IsUTF8, ( const char * ) LIBVLC_USED );
 
index 8ef609a31288d0985849d7c2b50ba0df1d69eae2..304d7af6d637737f4a2ee629b1510b5297df0d17 100644 (file)
@@ -432,3 +432,46 @@ int utf8_unlink( const char *filename )
     LocaleFree( local_name );
     return ret;
 }
+
+int utf8_mkstemp( char *template )
+{
+    static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
+
+    /* */
+    assert( template );
+
+    /* Check template validity */
+    const size_t i_length = strlen( template );
+    char *psz_rand = &template[i_length-6];
+
+    if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    uint64_t i_rand = mdate();
+
+    /* */
+    for( int i = 0; i < 256; i++ )
+    {
+        /* Create a pseudo random file name */
+        for( int j = 0; j < 6; j++ )
+        {
+            i_rand = i_rand * UINT64_C(1103515245) + 12345;
+            psz_rand[j] = digits[((i_rand >> 16) & 0xffff) % i_digits];
+        }
+
+        /* */
+        int fd = utf8_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
+        if( fd >= 0 )
+            return fd;
+        if( errno != EEXIST )
+            return -1;
+    }
+
+    errno = EEXIST;
+    return -1;
+}
+