const char * path_tmpdir() { if (!path_tmpdir_result) { # ifdef OS_NT DWORD pathLength = 0; pathLength = GetTempPath(pathLength,NULL); string_new(path_tmpdir_buffer); string_reserve(path_tmpdir_buffer,pathLength); pathLength = GetTempPathA(pathLength,path_tmpdir_buffer[0].value); path_tmpdir_buffer[0].value[pathLength-1] = '\0'; path_tmpdir_buffer[0].size = pathLength-1; # else const char * t = getenv("TMPDIR"); if (!t) { t = "/tmp"; } string_new(path_tmpdir_buffer); string_append(path_tmpdir_buffer,t); # endif path_tmpdir_result = path_tmpdir_buffer[0].value; } return path_tmpdir_result; }
static FILE * open_command_file( int const slot ) { string * const command_file = cmdtab[ slot ].command_file; /* If the temporary command file name has not already been prepared for this * slot number, prepare a new one containing a '##' place holder that will * be changed later and needs to be located at a fixed distance from the * end. */ if ( !command_file->value ) { DWORD const procID = GetCurrentProcessId(); string const * const tmpdir = path_tmpdir(); string_new( command_file ); string_reserve( command_file, tmpdir->size + 64 ); command_file->size = sprintf( command_file->value, "%s\\jam%d-%02d-##.bat", tmpdir->value, procID, slot ); } /* For some reason opening a command file can fail intermittently. But doing * some retries works. Most likely this is due to a previously existing file * of the same name that happens to still be opened by an active virus * scanner. Originally pointed out and fixed by Bronek Kozicki. * * We first try to open several differently named files to avoid having to * wait idly if not absolutely necessary. Our temporary command file names * contain a fixed position place holder we use for generating different * file names. */ { char * const index1 = command_file->value + command_file->size - 6; char * const index2 = index1 + 1; int waits_remaining; assert( command_file->value < index1 ); assert( index2 + 1 < command_file->value + command_file->size ); assert( index2[ 1 ] == '.' ); for ( waits_remaining = 3; ; --waits_remaining ) { int index; for ( index = 0; index != 20; ++index ) { FILE * f; *index1 = '0' + index / 10; *index2 = '0' + index % 10; f = fopen( command_file->value, "w" ); if ( f ) return f; } if ( !waits_remaining ) break; Sleep( 250 ); } } return 0; }
void string_append(String* s, const char* text) { int text_length; assert(text != NULL); text_length = strlen(text); if (text_length == 0) return; string_reserve(s, s->length + text_length); memcpy(s->text+s->length, text, text_length+1); s->length += text_length; assert(strlen(s->text) == s->length); }
LIST * builtin_system_registry( FRAME * frame, int flags ) { char const* path = object_str( list_front( lol_get(frame->args, 0) ) ); LIST* result = L0; HKEY key = get_key(&path); if ( key != 0 && ERROR_SUCCESS == RegOpenKeyEx(key, path, 0, KEY_QUERY_VALUE, &key) ) { DWORD type; BYTE data[MAX_REGISTRY_DATA_LENGTH]; DWORD len = sizeof(data); LIST * const field = lol_get(frame->args, 1); if ( ERROR_SUCCESS == RegQueryValueEx(key, field ? object_str( list_front( field ) ) : 0, 0, &type, data, &len) ) { switch (type) { case REG_EXPAND_SZ: { long len; string expanded[1]; string_new(expanded); while ( (len = ExpandEnvironmentStrings( (LPCSTR)data, expanded->value, expanded->capacity)) > expanded->capacity ) string_reserve(expanded, len); expanded->size = len - 1; result = list_push_back( result, object_new(expanded->value) ); string_free( expanded ); } break; case REG_MULTI_SZ: { char* s; for (s = (char*)data; *s; s += strlen(s) + 1) result = list_push_back( result, object_new(s) ); } break; case REG_DWORD: { char buf[100]; sprintf( buf, "%u", *(PDWORD)data ); result = list_push_back( result, object_new(buf) ); } break; case REG_SZ: result = list_push_back( result, object_new( (const char *)data ) ); break; } } RegCloseKey(key); } return result; }
string const * path_tmpdir() { static string buffer[ 1 ]; static int have_result; if ( !have_result ) { string_new( buffer ); #ifdef OS_NT { DWORD pathLength = GetTempPathA( 0, NULL ); string_reserve( buffer, pathLength ); pathLength = GetTempPathA( pathLength, buffer->value ); buffer->value[ pathLength - 1 ] = '\0'; buffer->size = pathLength - 1; } #else { char const * t = getenv( "TMPDIR" ); if ( !t ) t = "/tmp"; string_append( buffer, t ); } #endif have_result = 1; } return buffer; }