| 1095 | */ |
| 1096 | |
| 1097 | static FILE * open_command_file( int const slot ) |
| 1098 | { |
| 1099 | string * const command_file = cmdtab[ slot ].command_file; |
| 1100 | |
| 1101 | /* If the temporary command file name has not already been prepared for this |
| 1102 | * slot number, prepare a new one containing a '##' place holder that will |
| 1103 | * be changed later and needs to be located at a fixed distance from the |
| 1104 | * end. |
| 1105 | */ |
| 1106 | if ( !command_file->value ) |
| 1107 | { |
| 1108 | DWORD const procID = GetCurrentProcessId(); |
| 1109 | string const * const tmpdir = path_tmpdir(); |
| 1110 | string_new( command_file ); |
| 1111 | string_reserve( command_file, tmpdir->size + 64 ); |
| 1112 | command_file->size = sprintf( command_file->value, |
| 1113 | "%s\\jam%d-%02d-##.bat", tmpdir->value, procID, slot ); |
| 1114 | } |
| 1115 | |
| 1116 | /* For some reason opening a command file can fail intermittently. But doing |
| 1117 | * some retries works. Most likely this is due to a previously existing file |
| 1118 | * of the same name that happens to still be opened by an active virus |
| 1119 | * scanner. Originally pointed out and fixed by Bronek Kozicki. |
| 1120 | * |
| 1121 | * We first try to open several differently named files to avoid having to |
| 1122 | * wait idly if not absolutely necessary. Our temporary command file names |
| 1123 | * contain a fixed position place holder we use for generating different |
| 1124 | * file names. |
| 1125 | */ |
| 1126 | { |
| 1127 | char * const index1 = command_file->value + command_file->size - 6; |
| 1128 | char * const index2 = index1 + 1; |
| 1129 | int waits_remaining; |
| 1130 | assert( command_file->value < index1 ); |
| 1131 | assert( index2 + 1 < command_file->value + command_file->size ); |
| 1132 | assert( index2[ 1 ] == '.' ); |
| 1133 | for ( waits_remaining = 3; ; --waits_remaining ) |
| 1134 | { |
| 1135 | int index; |
| 1136 | for ( index = 0; index != 20; ++index ) |
| 1137 | { |
| 1138 | FILE * f; |
| 1139 | *index1 = '0' + index / 10; |
| 1140 | *index2 = '0' + index % 10; |
| 1141 | f = fopen( command_file->value, "w" ); |
| 1142 | if ( f ) return f; |
| 1143 | } |
| 1144 | if ( !waits_remaining ) break; |
| 1145 | Sleep( 250 ); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | /* |
no test coverage detected