Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 19fa540 | 2016-02-23 08:19:39 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_WIN32 |
| 37 | |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 38 | #include <io.h> |
| 39 | #include <stdio.h> |
| 40 | #include <string.h> |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 41 | #include <tchar.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 42 | |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 45 | #include <grpc/support/string_util.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 46 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 47 | #include "src/core/support/string_win32.h" |
Craig Tiller | 732a875 | 2016-02-22 15:59:19 -0800 | [diff] [blame] | 48 | #include "src/core/support/tmpfile.h" |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 49 | |
| 50 | FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) { |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 51 | FILE *result = NULL; |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 52 | LPTSTR template_string = NULL; |
| 53 | TCHAR tmp_path[MAX_PATH]; |
| 54 | TCHAR tmp_filename[MAX_PATH]; |
| 55 | DWORD status; |
| 56 | UINT success; |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 57 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 58 | if (tmp_filename_out != NULL) *tmp_filename_out = NULL; |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 59 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 60 | /* Convert our prefix to TCHAR. */ |
| 61 | template_string = gpr_char_to_tchar(prefix); |
| 62 | GPR_ASSERT(template_string); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 63 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 64 | /* Get the path to the best temporary folder available. */ |
| 65 | status = GetTempPath(MAX_PATH, tmp_path); |
| 66 | if (status == 0 || status > MAX_PATH) goto end; |
| 67 | |
| 68 | /* Generate a unique filename with our template + temporary path. */ |
| 69 | success = GetTempFileName(tmp_path, template_string, 0, tmp_filename); |
| 70 | if (!success) goto end; |
| 71 | |
| 72 | /* Open a file there. */ |
| 73 | if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end; |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 74 | |
| 75 | end: |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 76 | if (result && tmp_filename_out) { |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 77 | *tmp_filename_out = gpr_tchar_to_char(tmp_filename); |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 78 | } |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 79 | |
Nicolas Noble | df80ba8 | 2015-02-11 16:05:29 -0800 | [diff] [blame] | 80 | gpr_free(template_string); |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 81 | return result; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 84 | #endif /* GPR_WIN32 */ |