00001 #include "CommonLibraries.h"
00002
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <errno.h>
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008 #include <unistd.h>
00009 #include <time.h>
00010 #include <stdio.h>
00011
00012
00013
00014
00015 static const char padchar[] =
00016 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
00017
00018 static const unsigned int padcharlen = 62;
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 bool RIOmktemp( char *temp_file_template )
00032 {
00033 int len;
00034 int randnum;
00035 char *trv;
00036 char *random_file;
00037 char *temp_dir;
00038 char *endpath, *filename;
00039 struct stat mystat;
00040 bool status = true;
00041 bool remove_temp_dir;
00042
00043
00044 len = strlen( temp_file_template );
00045 if( len < 6 || strcmp( &temp_file_template[len - 6], "XXXXXX" ) )
00046 return false;
00047
00048
00049
00050 endpath = strrchr( temp_file_template, '/' );
00051 remove_temp_dir = false;
00052 if( endpath == NULL )
00053 {
00054
00055
00056 temp_dir = getenv( "TMPDIR" );
00057
00058 if( ( temp_dir == NULL ) || ( strlen( temp_dir ) == 0 ) )
00059 {
00060 temp_dir = strdup( "/tmp" );
00061 remove_temp_dir = true;
00062 }
00063 filename = temp_file_template;
00064 }
00065 else
00066 {
00067
00068
00069 *endpath = 0;
00070
00071 filename = endpath + 1;
00072 temp_dir = strdup( temp_file_template );
00073
00074 *endpath = '/';
00075 remove_temp_dir = true;
00076 }
00077
00078 random_file = (char *)malloc( sizeof( char ) *
00079 ( strlen( temp_dir ) + 2 +
00080 strlen( filename )
00081 )
00082 );
00083 while( status )
00084 {
00085 strcpy( random_file, temp_dir );
00086 strcat( random_file, "/" );
00087 strcat( random_file, filename );
00088
00089
00090 srandom( time(NULL) );
00091
00092 for( trv = random_file + strlen( random_file ) - 1;
00093 *trv == 'X';
00094 trv--
00095 )
00096 {
00097 randnum = random() % ( sizeof( padchar ) - 1 );
00098 *trv = padchar[ randnum ];
00099 }
00100
00101 if( lstat( random_file, &mystat ) < 0 )
00102 {
00103 if( errno == ENOENT )
00104 break;
00105 else
00106 status = false;
00107 }
00108 }
00109
00110 if( status )
00111 strcpy( temp_file_template, random_file );
00112
00113 free( random_file );
00114
00115 if( remove_temp_dir )
00116 free( temp_dir );
00117
00118 return status;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 bool RIOmktempAndID( char *temp_file_template,
00135 unsigned long long int *temp_file_id )
00136 {
00137 bool status;
00138 unsigned int TotalRandomChars;
00139 const char *PosRandomName, *PosRandomChar;
00140
00141
00142
00143
00144 TotalRandomChars = 0;
00145 for( int i = strlen( temp_file_template ) - 1; i >=0; i-- )
00146 {
00147 if( temp_file_template[ i ] != 'X' )
00148 break;
00149 TotalRandomChars++;
00150 }
00151
00152 if( ( TotalRandomChars < 6 ) || ( TotalRandomChars > 10 ) )
00153 return false;
00154
00155
00156 status = RIOmktemp( temp_file_template );
00157 if( status )
00158 {
00159 *temp_file_id = 0;
00160 PosRandomName = temp_file_template + strlen( temp_file_template ) - 1;
00161 for( unsigned int i = 0; i < TotalRandomChars; i++ )
00162 {
00163 *temp_file_id = *temp_file_id * padcharlen;
00164 PosRandomChar = strchr( padchar, *PosRandomName );
00165 if( PosRandomChar == NULL )
00166 {
00167
00168 *temp_file_id = 0;
00169 return false;
00170 }
00171 *temp_file_id = *temp_file_id + ( PosRandomChar - padchar );
00172 PosRandomName--;
00173 }
00174 }
00175 return true;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 bool IdRandomName( unsigned long long int NameId, char *RandomName,
00193 unsigned int RandomNameSize )
00194 {
00195
00196 unsigned long long int TempId;
00197 unsigned int RandomCharPos, TotalChars;
00198
00199
00200
00201
00202 for( unsigned int i = 0; i < RandomNameSize; i++ )
00203 RandomName[ i ] = padchar[ 0 ];
00204 RandomName[ RandomNameSize ] = 0;
00205
00206 TotalChars = 0;
00207 TempId = NameId;
00208
00209
00210 while( TempId )
00211 {
00212 if( TotalChars >= 10 )
00213 return false;
00214
00215 RandomCharPos = ( unsigned int ) ( TempId % padcharlen );
00216 RandomName[ TotalChars ] = padchar[ RandomCharPos ];
00217 TempId = TempId / padcharlen;
00218 TotalChars++;
00219 }
00220
00221
00222
00223 return true;
00224 }