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