| 1205 | */ |
| 1206 | |
| 1207 | static FILE * open_command_file( int32_t const slot ) |
| 1208 | { |
| 1209 | string * const command_file = cmdtab[ slot ].command_file; |
| 1210 | |
| 1211 | /* If the temporary command file name has not already been prepared for this |
| 1212 | * slot number, prepare a new one containing a '##' place holder that will |
| 1213 | * be changed later and needs to be located at a fixed distance from the |
| 1214 | * end. |
| 1215 | */ |
| 1216 | if ( !command_file->value ) |
| 1217 | { |
| 1218 | DWORD const procID = GetCurrentProcessId(); |
| 1219 | string const * const tmpdir = path_tmpdir(); |
| 1220 | string_new( command_file ); |
| 1221 | string_reserve( command_file, tmpdir->size + 64 ); |
| 1222 | command_file->size = sprintf( command_file->value, |
| 1223 | "%s\\jam%lu-%02d-##.bat", tmpdir->value, procID, slot ); |
| 1224 | } |
| 1225 | |
| 1226 | /* For some reason opening a command file can fail intermittently. But doing |
| 1227 | * some retries works. Most likely this is due to a previously existing file |
| 1228 | * of the same name that happens to still be opened by an active virus |
| 1229 | * scanner. Originally pointed out and fixed by Bronek Kozicki. |
| 1230 | * |
| 1231 | * We first try to open several differently named files to avoid having to |
| 1232 | * wait idly if not absolutely necessary. Our temporary command file names |
| 1233 | * contain a fixed position place holder we use for generating different |
| 1234 | * file names. |
| 1235 | */ |
| 1236 | { |
| 1237 | char * const index1 = command_file->value + command_file->size - 6; |
| 1238 | char * const index2 = index1 + 1; |
| 1239 | int32_t waits_remaining; |
| 1240 | assert( command_file->value < index1 ); |
| 1241 | assert( index2 + 1 < command_file->value + command_file->size ); |
| 1242 | assert( index2[ 1 ] == '.' ); |
| 1243 | for ( waits_remaining = 3; ; --waits_remaining ) |
| 1244 | { |
| 1245 | int32_t index; |
| 1246 | for ( index = 0; index != 20; ++index ) |
| 1247 | { |
| 1248 | FILE * f; |
| 1249 | *index1 = '0' + index / 10; |
| 1250 | *index2 = '0' + index % 10; |
| 1251 | f = fopen( command_file->value, "w" ); |
| 1252 | if ( f ) return f; |
| 1253 | } |
| 1254 | if ( !waits_remaining ) break; |
| 1255 | Sleep( 250 ); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | /* |
no test coverage detected