blob: 3000f0029fe7e55d2617572031977638bbcd0760 [file] [log] [blame]
Julien Boeuf026a4172015-02-02 18:36:37 -08001/*
2 *
Craig Tiller19fa5402016-02-23 08:19:39 -08003 * Copyright 2015-2016, Google Inc.
Julien Boeuf026a4172015-02-02 18:36:37 -08004 * 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 Boeuf026a4172015-02-02 18:36:37 -080038#include <io.h>
39#include <stdio.h>
40#include <string.h>
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080041#include <tchar.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080042
Julien Boeuf05618962015-02-03 21:58:53 -080043#include <grpc/support/alloc.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080044#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070045#include <grpc/support/string_util.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080046
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080047#include "src/core/support/string_win32.h"
Craig Tiller732a8752016-02-22 15:59:19 -080048#include "src/core/support/tmpfile.h"
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080049
50FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) {
Julien Boeuf026a4172015-02-02 18:36:37 -080051 FILE *result = NULL;
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080052 LPTSTR template_string = NULL;
53 TCHAR tmp_path[MAX_PATH];
54 TCHAR tmp_filename[MAX_PATH];
55 DWORD status;
56 UINT success;
Julien Boeuf05618962015-02-03 21:58:53 -080057
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080058 if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
Julien Boeuf05618962015-02-03 21:58:53 -080059
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080060 /* Convert our prefix to TCHAR. */
61 template_string = gpr_char_to_tchar(prefix);
62 GPR_ASSERT(template_string);
Julien Boeuf026a4172015-02-02 18:36:37 -080063
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080064 /* 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 Boeuf05618962015-02-03 21:58:53 -080074
75end:
Nicolas Noblecfd60732015-03-18 16:27:43 -070076 if (result && tmp_filename_out) {
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080077 *tmp_filename_out = gpr_tchar_to_char(tmp_filename);
Julien Boeuf05618962015-02-03 21:58:53 -080078 }
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080079
Nicolas Nobledf80ba82015-02-11 16:05:29 -080080 gpr_free(template_string);
Julien Boeuf05618962015-02-03 21:58:53 -080081 return result;
Julien Boeuf026a4172015-02-02 18:36:37 -080082}
83
Craig Tiller190d3602015-02-18 09:23:38 -080084#endif /* GPR_WIN32 */