• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "msvc-posix.h"
16 
17 // TODO(joshuaduong): Support unicode (b/117322783)
18 //#include "android/base/system/Win32UnicodeString.h"
19 
20 #include <io.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 
mkstemp(char * t)25 int mkstemp(char* t) {
26     // TODO(joshuaduong): Support unicode (b/117322783)
27     int len = strlen(t) + 1;
28     errno_t err = _mktemp_s(t, len);
29 
30     if (err != 0) {
31         return -1;
32     }
33 
34     return _sopen(t, _O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY, _SH_DENYRW,
35                   _S_IREAD | _S_IWRITE);
36 }
37 
38 // From https://msdn.microsoft.com/en-us/library/28d5ce15.aspx
asprintf(char ** buf,const char * format,...)39 int asprintf(char** buf, const char* format, ...) {
40     va_list args;
41     int len;
42 
43     if (buf == NULL) {
44         return -1;
45     }
46 
47     // retrieve the variable arguments
48     va_start(args, format);
49 
50     len = _vscprintf(format, args)  // _vscprintf doesn't count
51           + 1;                      // terminating '\0'
52 
53     if (len <= 0) {
54         return len;
55     }
56 
57     *buf = (char*)malloc(len * sizeof(char));
58 
59     vsprintf(*buf, format, args);  // C4996
60     // Note: vsprintf is deprecated; consider using vsprintf_s instead
61     return len;
62 }
63 
64 // From https://msdn.microsoft.com/en-us/library/28d5ce15.aspx
vasprintf(char ** buf,const char * format,va_list args)65 static int vasprintf(char** buf, const char* format, va_list args) {
66     int len;
67 
68     if (buf == NULL) {
69         return -1;
70     }
71 
72     len = _vscprintf(format, args)  // _vscprintf doesn't count
73           + 1;                      // terminating '\0'
74 
75     if (len <= 0) {
76         return len;
77     }
78 
79     *buf = (char*)malloc(len * sizeof(char));
80 
81     vsprintf(*buf, format, args);  // C4996
82     // Note: vsprintf is deprecated; consider using vsprintf_s instead
83     return len;
84 }
85