int gen_tmp_file(const char *templ, char *path_out, FILE **fp_out) { int rc = -1; #ifdef _WIN32 // TODO: implement me #else int fd, i_XXXXXX, suffix_len; // TODO: secure this string crap char path[64]; get_tmp_path(path, sizeof(path)); strcat(path, "/"); strcat(path, templ); // find length of suffix for(i_XXXXXX=0; i_XXXXXX < strlen(path)-5; i_XXXXXX++) { //debug("at idx=%d looking at: %s\n", i_XXXXXX, path+i_XXXXXX); if(0 == strncmp(path+i_XXXXXX, "XXXXXX", 6)) { break; } } if(i_XXXXXX >= strlen(path)-5) { //debug("ERROR: couldn't find XXXXXX in \"%s\"\n", path); goto cleanup; } suffix_len = strlen(path) - (i_XXXXXX+6); //debug("sending mkstemp \"%s\" with suffix length %d\n", path, suffix_len); fd = mkstemps(path, suffix_len); if(fd < 0) { //debug("ERROR: mkstemp() returned %d\n", fd); goto cleanup; } strcpy(path_out, path); /* file descriptor is linux specific int inside proc_struct, convert this to stdlib file pointer */ *fp_out = fdopen(fd, "w"); if(*fp_out == NULL) { //debug("ERROR: fdopen()\n"); goto cleanup; } #endif rc = 0; cleanup: return rc; }
int main(int argc, char *argv[]) { char *tmp_path; // Find an untaken tmp path. tmp_path = get_tmp_path(); // Save STDIN input to that tmp file. save_input_to_tmp(tmp_path); // Open tmp file in Safari. open_input_file(tmp_path); // Cleanup and exit. free(tmp_path); return 0; }