LLDB  mainline
Windows.cpp
Go to the documentation of this file.
1 //===-- Windows.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // This file provides Windows support functions
10 
11 #include "lldb/Host/PosixApi.h"
13 
14 #include "llvm/Support/ConvertUTF.h"
15 
16 #include <cassert>
17 #include <cctype>
18 #include <cerrno>
19 #include <cstdarg>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <io.h>
24 
25 int vasprintf(char **ret, const char *fmt, va_list ap) {
26  char *buf;
27  int len;
28  size_t buflen;
29  va_list ap2;
30 
31  va_copy(ap2, ap);
32  len = vsnprintf(NULL, 0, fmt, ap2);
33 
34  if (len >= 0 &&
35  (buf = (char *)malloc((buflen = (size_t)(len + 1)))) != NULL) {
36  len = vsnprintf(buf, buflen, fmt, ap);
37  *ret = buf;
38  } else {
39  *ret = NULL;
40  len = -1;
41  }
42 
43  va_end(ap2);
44  return len;
45 }
46 
47 char *strcasestr(const char *s, const char *find) {
48  char c, sc;
49  size_t len;
50 
51  if ((c = *find++) != 0) {
52  c = tolower((unsigned char)c);
53  len = strlen(find);
54  do {
55  do {
56  if ((sc = *s++) == 0)
57  return 0;
58  } while ((char)tolower((unsigned char)sc) != c);
59  } while (strncasecmp(s, find, len) != 0);
60  s--;
61  }
62  return const_cast<char *>(s);
63 }
64 
65 #ifdef _MSC_VER
66 
67 char *basename(char *path) {
68  char *l1 = strrchr(path, '\\');
69  char *l2 = strrchr(path, '/');
70  if (l2 > l1)
71  l1 = l2;
72  if (!l1)
73  return path; // no base name
74  return &l1[1];
75 }
76 
77 char *dirname(char *path) {
78  char *l1 = strrchr(path, '\\');
79  char *l2 = strrchr(path, '/');
80  if (l2 > l1)
81  l1 = l2;
82  if (!l1)
83  return NULL; // no dir name
84  *l1 = 0;
85  return path;
86 }
87 
88 int strcasecmp(const char *s1, const char *s2) { return stricmp(s1, s2); }
89 
90 int strncasecmp(const char *s1, const char *s2, size_t n) {
91  return strnicmp(s1, s2, n);
92 }
93 
94 #if _MSC_VER < 1900
95 namespace lldb_private {
96 int vsnprintf(char *buffer, size_t count, const char *format, va_list argptr) {
97  int old_errno = errno;
98  int r = ::vsnprintf(buffer, count, format, argptr);
99  int new_errno = errno;
100  buffer[count - 1] = '\0';
101  if (r == -1 || r == count) {
102  FILE *nul = fopen("nul", "w");
103  int bytes_written = ::vfprintf(nul, format, argptr);
104  fclose(nul);
105  if (bytes_written < count)
106  errno = new_errno;
107  else {
108  errno = old_errno;
109  r = bytes_written;
110  }
111  }
112  return r;
113 }
114 } // namespace lldb_private
115 #endif
116 
117 #endif // _MSC_VER
strcasestr
char * strcasestr(const char *s, const char *find)
Definition: Windows.cpp:47
vasprintf
int vasprintf(char **ret, const char *fmt, va_list ap)
Definition: Windows.cpp:25
windows.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
PosixApi.h